1<?php
2/**
3 *
4 * @package    solr
5 * @author     Gabriel Birke <birke@d-scribe.de>
6 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
7 */
8
9/**
10 * This class takes creates an XML document for adding documents to Solr.
11 */
12class Solr_AddDocument {
13
14  protected $writer;
15
16  public function __construct(XMLWriter $writer){
17    $this->writer = $writer;
18  }
19
20  public function start($commitWithin=0){
21    $this->writer->startDocument();
22    $this->writer->startElement('add');
23    if($commitWithin > 0) {
24      $this->writer->writeAttribute('commitWithin', $commitWithin);
25    }
26  }
27
28  public function end(){
29    $this->writer->endElement();
30  }
31
32  public function addPage($fields){
33    $writer = $this->writer;
34    $writer->startElement("doc");
35    foreach($fields as $name => $value) {
36      if(is_array($value)) {
37        foreach($value as $v) {
38          $this->_outputField($name, $v);
39        }
40      }
41      else {
42        $this->_outputField($name, $value);
43      }
44    }
45    $writer->endElement();
46  }
47
48  public function getWriter(){
49    return $this->writer;
50  }
51
52  protected function _outputField($name, $content){
53    $this->writer->startElement("field");
54    $this->writer->writeAttribute('name', $name);
55    $this->writer->text($content);
56    $this->writer->endElement();
57  }
58
59
60}
61