1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
|
<?php
class XmlSerializer extends Serializer
{
protected $header = array('<?xml version="1.0" encoding="UTF-8"?>', '<javascript>');
protected $footer = array('</javascript>');
protected function lineStarts($line) {
if (preg_match('%^\t<object [^>]*location="([^"]+)"%', $line, $match)) {
return $match[1];
}
}
protected function lineEnds($line) {
if (preg_match('%^\t</object>$%', $line, $match)) {
return true;
}
}
protected function linesToRaw($lines) {
return DOMDocument::loadXML(implode("\n", $lines));
}
public function toObject($raw, $id=null) {
return $this->ascend($raw, $raw->firstChild);
}
public function ascend($document, $node) {
$object = array();
if ($node->hasAttributes()) {
if ($node->attributes) {
foreach ($node->attributes as $attribute) {
$value = $attribute->value;
if ($value == 'true') {
$value = true;
}
elseif ($value == 'false') {
$value = false;
}
$object['@' . $attribute->name] = $value;
}
}
}
if ($node->childNodes) {
foreach ($node->childNodes as $child_node) {
if ($child_node->tagName) {
$object['#' . $child_node->tagName][] = $this->ascend($document, $child_node);
}
else {
// Text node
$object['content'] = $node->nodeValue;
}
}
}
return $object;
}
public function toString($raw, $id=null) {
if (!$id) {
if (!($id = $raw->firstChild->getAttribute('location'))) {
throw new Exception('toString must be passed an ID or raw object must contain and ID');
}
}
$lines = explode("\n", str_replace('<?xml version="1.0" encoding="UTF-8"?>' . "\n", '', $raw->saveXML()));
foreach ($lines as $i => $line) {
$indent = 0;
while (substr($line, 0, 2) == ' ') {
++$indent;
$line = substr($line, 2);
}
$lines[$i] = str_repeat("\t", $indent) . $line;
}
if (count($lines) && substr($lines[0], -2) == '/>') {
$lines[0] = substr($lines[0], 0, -2) . '>';
array_splice($lines, 1, 0, array('</object>'));
}
return implode("\n", $lines);
}
protected function descend($document, $node, $object) {
foreach ($object as $key => $value) {
if (is_bool($value)) {
$value = $value ? 'true' : 'false';
}
switch ($key{0}) {
case '@':
$node->setAttribute(substr($key, 1), $value);
break;
case '#':
foreach ($value as $child) {
$this->descend($document, $node->appendChild($document->createElement(substr($key, 1))), $child);
}
break;
default:
if ($key === 'content') {
$node->appendChild($document->createTextNode($value));
}
}
}
}
public function convertToRaw($object) {
$document = new DOMDocument('1.0', 'UTF-8');
$document->preserveWhiteSpace = true;
$document->formatOutput = true;
$this->descend($document, $document->appendChild($document->createElement('object')), $object);
return $document;
}
}
?>
|