Created
July 20, 2011 03:48
-
-
Save wahyusumartha/1094295 to your computer and use it in GitHub Desktop.
Sphinx XML Feed Generator Class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class SphinxXMLFeed extends XMLWriter | |
{ | |
private $fields = array(); | |
private $attributes = array(); | |
public function __construct($options = array()) | |
{ | |
$defaults = array( | |
'indent' => false, | |
); | |
$options = array_merge($defaults, $options); | |
// Store the xml tree in memory | |
$this->openMemory(); | |
if($options['indent']) { | |
$this->setIndent(true); | |
} | |
} | |
public function setFields($fields) { | |
$this->fields = $fields; | |
} | |
public function setAttributes($attributes) { | |
$this->attributes = $attributes; | |
} | |
public function addDocument($doc) { | |
$this->startElement('sphinx:document'); | |
$this->writeAttribute('id', $doc['id']); | |
foreach($doc as $key => $value) { | |
// Skip the id key since that is an element attribute | |
if($key == 'id') continue; | |
$this->startElement($key); | |
$this->text($value); | |
$this->endElement(); | |
} | |
$this->endElement(); | |
print $this->outputMemory(); | |
} | |
public function beginOutput() { | |
$this->startDocument('1.0', 'UTF-8'); | |
$this->startElement('sphinx:docset'); | |
$this->startElement('sphinx:schema'); | |
// add fields to the schema | |
foreach($this->fields as $field) { | |
$this->startElement('sphinx:field'); | |
$this->writeAttribute('name', $field); | |
$this->endElement(); | |
} | |
// add attributes to the schema | |
foreach($this->attributes as $attributes) { | |
$this->startElement('sphinx:attr'); | |
foreach($attributes as $key => $value) { | |
$this->writeAttribute($key, $value); | |
} | |
$this->endElement(); | |
} | |
// end sphinx:schema | |
$this->endElement(); | |
print $this->outputMemory(); | |
} | |
public function endOutput() | |
{ | |
// end sphinx:docset | |
$this->endElement(); | |
print $this->outputMemory(); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment