/** * Serialize * * @param var $payload * @param io.streams.OutputStream $out * @return void */ public function serialize($payload, $out) { $t = new Tree(); $t->setEncoding('UTF-8'); if ($payload instanceof Payload) { $root = isset($payload->properties['name']) ? $payload->properties['name'] : 'root'; $val = $payload->value; } else { $root = 'root'; $val = $payload; } $out->write($t->getDeclaration() . "\n"); if ($val instanceof \Traversable) { $i = 0; $map = null; foreach ($val as $key => $element) { if (0 === $i++) { $out->write('<' . $root . '>'); $map = 0 !== $key; } $out->write($this->node($map ? $key : $root, $element)->getSource(INDENT_NONE, 'utf-8')); } if (null === $map) { $out->write('<' . $root . '/>'); } else { $out->write('</' . $root . '>'); } } else { $out->write($t->withRoot($this->node($root, $val))->getSource(INDENT_NONE)); } }
/** * Calculate payload * * @param com.google.gsa.feed.XmlFeed feed * @return string */ public function payload($feed) { $tree = new Tree('gsafeed'); $tree->addChild((new Node('header'))->withChild(new Node('datasource', $feed->dataSource()))->withChild(new Node('feedtype', $feed->feedType()->name()))); $group = $tree->addChild(new Node('group')); foreach ($feed->getRecords() as $record) { $record->visit($group->addChild(new Node('record'))); } return $tree->getDeclaration() . "\n" . self::FEED_DOC_TYPE . "\n" . $tree->getSource(INDENT_DEFAULT); }
/** * Set XML from a tree * * @param xml.Tree xml */ public function setXMLTree(Tree $xml) { libxml_get_last_error() && libxml_clear_errors(); $this->document = new \DOMDocument(); $this->document->loadXML($xml->getDeclaration() . $xml->getSource(INDENT_NONE)); $this->_checkErrors($xml); }