2011-11-06 17:07:38 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class PHPParser_Unserializer_XML implements PHPParser_Unserializer
|
|
|
|
{
|
|
|
|
protected $reader;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
$this->reader = new XMLReader;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function unserialize($string) {
|
|
|
|
$this->reader->XML($string);
|
|
|
|
|
|
|
|
$this->reader->read();
|
|
|
|
if ('AST' !== $this->reader->name) {
|
2011-11-27 21:43:27 +01:00
|
|
|
throw new DomainException('AST root element not found');
|
2011-11-06 17:07:38 +01:00
|
|
|
}
|
|
|
|
|
2011-12-03 15:15:20 +01:00
|
|
|
return $this->read($this->reader->depth);
|
2011-11-06 17:07:38 +01:00
|
|
|
}
|
|
|
|
|
2011-12-03 15:15:20 +01:00
|
|
|
protected function read($depthLimit, $throw = true, &$nodeFound = null) {
|
|
|
|
$nodeFound = true;
|
|
|
|
while ($this->reader->read() && $depthLimit < $this->reader->depth) {
|
2011-11-06 17:07:38 +01:00
|
|
|
if (XMLReader::ELEMENT !== $this->reader->nodeType) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ('node' === $this->reader->prefix) {
|
2011-12-03 15:15:20 +01:00
|
|
|
return $this->readNode();
|
|
|
|
} elseif ('scalar' === $this->reader->prefix) {
|
|
|
|
return $this->readScalar();
|
|
|
|
} else {
|
|
|
|
throw new DomainException(sprintf('Unexpected node of type "%s"', $this->reader->name));
|
|
|
|
}
|
|
|
|
}
|
2011-11-06 17:07:38 +01:00
|
|
|
|
2011-12-03 15:15:20 +01:00
|
|
|
$nodeFound = false;
|
|
|
|
if ($throw) {
|
|
|
|
throw new DomainException('Expected node or scalar');
|
|
|
|
}
|
|
|
|
}
|
2011-11-06 17:07:38 +01:00
|
|
|
|
2011-12-03 15:15:20 +01:00
|
|
|
protected function readNode()
|
|
|
|
{
|
|
|
|
$className = 'PHPParser_Node_' . $this->reader->localName;
|
2011-11-06 17:07:38 +01:00
|
|
|
|
2011-12-03 15:15:20 +01:00
|
|
|
// create the node without calling it's constructor
|
|
|
|
$node = unserialize(
|
2012-04-04 14:06:08 +02:00
|
|
|
sprintf(
|
|
|
|
"O:%d:\"%s\":2:{s:11:\"\0*\0subNodes\";a:0:{}s:13:\"\0*\0attributes\";a:0:{}}",
|
|
|
|
strlen($className), $className
|
|
|
|
)
|
2011-12-03 15:15:20 +01:00
|
|
|
);
|
2011-11-06 17:07:38 +01:00
|
|
|
|
2011-12-03 15:15:20 +01:00
|
|
|
$depthLimit = $this->reader->depth;
|
|
|
|
while ($this->reader->read() && $depthLimit < $this->reader->depth) {
|
|
|
|
if (XMLReader::ELEMENT !== $this->reader->nodeType) {
|
|
|
|
continue;
|
|
|
|
}
|
2011-11-06 17:07:38 +01:00
|
|
|
|
2012-04-29 23:32:09 +02:00
|
|
|
$type = $this->reader->prefix;
|
|
|
|
if ('subNode' !== $type && 'attribute' !== $type) {
|
2011-12-03 15:15:20 +01:00
|
|
|
throw new DomainException(
|
2012-04-29 23:32:09 +02:00
|
|
|
sprintf('Expected sub node or attribute, got node of type "%s"', $this->reader->name)
|
2011-12-03 15:15:20 +01:00
|
|
|
);
|
|
|
|
}
|
2011-11-06 17:07:38 +01:00
|
|
|
|
2012-04-29 23:32:09 +02:00
|
|
|
$name = $this->reader->localName;
|
|
|
|
$value = $this->read($this->reader->depth);
|
2011-11-06 17:07:38 +01:00
|
|
|
|
2012-04-29 23:32:09 +02:00
|
|
|
if ('subNode' === $type) {
|
|
|
|
$node->$name = $value;
|
|
|
|
} else {
|
|
|
|
$node->setAttribute($name, $value);
|
|
|
|
}
|
2011-11-06 17:07:38 +01:00
|
|
|
}
|
2011-12-03 15:15:20 +01:00
|
|
|
|
|
|
|
return $node;
|
2011-11-06 17:07:38 +01:00
|
|
|
}
|
|
|
|
|
2011-12-03 15:15:20 +01:00
|
|
|
protected function readScalar() {
|
|
|
|
switch ($name = $this->reader->localName) {
|
|
|
|
case 'array':
|
|
|
|
$depth = $this->reader->depth;
|
|
|
|
$array = array();
|
|
|
|
while (true) {
|
|
|
|
$node = $this->read($depth, false, $nodeFound);
|
|
|
|
if (!$nodeFound) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
$array[] = $node;
|
|
|
|
}
|
|
|
|
return $array;
|
|
|
|
case 'string':
|
|
|
|
return $this->reader->readString();
|
|
|
|
case 'int':
|
|
|
|
$text = $this->reader->readString();
|
|
|
|
if (false === $int = filter_var($text, FILTER_VALIDATE_INT)) {
|
|
|
|
throw new DomainException(sprintf('"%s" is not a valid integer', $text));
|
|
|
|
}
|
|
|
|
return $int;
|
|
|
|
case 'float':
|
|
|
|
$text = $this->reader->readString();
|
|
|
|
if (false === $float = filter_var($text, FILTER_VALIDATE_FLOAT)) {
|
|
|
|
throw new DomainException(sprintf('"%s" is not a valid float', $text));
|
|
|
|
}
|
|
|
|
return $float;
|
|
|
|
case 'true':
|
|
|
|
case 'false':
|
|
|
|
case 'null':
|
|
|
|
if (!$this->reader->isEmptyElement) {
|
|
|
|
throw new DomainException(sprintf('"%s" scalar must be empty', $name));
|
|
|
|
}
|
|
|
|
return constant($name);
|
|
|
|
default:
|
|
|
|
throw new DomainException(sprintf('Unknown scalar type "%s"', $name));
|
2011-11-06 17:07:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|