2011-11-06 17:07:38 +01:00
|
|
|
<?php
|
|
|
|
|
2014-02-06 14:44:16 +01:00
|
|
|
namespace PhpParser\Serializer;
|
|
|
|
|
|
|
|
use PhpParser\Comment;
|
2016-11-23 22:58:18 +01:00
|
|
|
use PhpParser\Node;
|
2014-02-06 14:44:16 +01:00
|
|
|
use PhpParser\Serializer;
|
2016-11-23 22:58:18 +01:00
|
|
|
use XMLWriter;
|
2014-02-06 14:44:16 +01:00
|
|
|
|
|
|
|
class XML implements Serializer
|
2011-11-06 17:07:38 +01:00
|
|
|
{
|
|
|
|
protected $writer;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructs a XML serializer.
|
|
|
|
*/
|
|
|
|
public function __construct() {
|
|
|
|
$this->writer = new XMLWriter;
|
|
|
|
$this->writer->openMemory();
|
|
|
|
$this->writer->setIndent(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function serialize(array $nodes) {
|
|
|
|
$this->writer->flush();
|
|
|
|
$this->writer->startDocument('1.0', 'UTF-8');
|
|
|
|
|
|
|
|
$this->writer->startElement('AST');
|
2012-04-29 23:32:09 +02:00
|
|
|
$this->writer->writeAttribute('xmlns:node', 'http://nikic.github.com/PHPParser/XML/node');
|
|
|
|
$this->writer->writeAttribute('xmlns:subNode', 'http://nikic.github.com/PHPParser/XML/subNode');
|
|
|
|
$this->writer->writeAttribute('xmlns:attribute', 'http://nikic.github.com/PHPParser/XML/attribute');
|
|
|
|
$this->writer->writeAttribute('xmlns:scalar', 'http://nikic.github.com/PHPParser/XML/scalar');
|
2011-11-06 17:07:38 +01:00
|
|
|
|
|
|
|
$this->_serialize($nodes);
|
|
|
|
|
|
|
|
$this->writer->endElement();
|
|
|
|
|
|
|
|
return $this->writer->outputMemory();
|
|
|
|
}
|
|
|
|
|
2012-04-19 00:25:13 +02:00
|
|
|
protected function _serialize($node) {
|
2014-02-06 14:44:16 +01:00
|
|
|
if ($node instanceof Node) {
|
2011-11-06 17:07:38 +01:00
|
|
|
$this->writer->startElement('node:' . $node->getType());
|
|
|
|
|
2012-04-29 23:32:09 +02:00
|
|
|
foreach ($node->getAttributes() as $name => $value) {
|
|
|
|
$this->writer->startElement('attribute:' . $name);
|
|
|
|
$this->_serialize($value);
|
|
|
|
$this->writer->endElement();
|
2011-11-06 17:07:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($node as $name => $subNode) {
|
|
|
|
$this->writer->startElement('subNode:' . $name);
|
|
|
|
$this->_serialize($subNode);
|
|
|
|
$this->writer->endElement();
|
|
|
|
}
|
|
|
|
|
2012-05-06 17:49:04 +02:00
|
|
|
$this->writer->endElement();
|
2014-02-06 14:44:16 +01:00
|
|
|
} elseif ($node instanceof Comment) {
|
2012-05-06 17:49:04 +02:00
|
|
|
$this->writer->startElement('comment');
|
2014-02-06 14:44:16 +01:00
|
|
|
$this->writer->writeAttribute('isDocComment', $node instanceof Comment\Doc ? 'true' : 'false');
|
2015-03-23 11:43:22 +01:00
|
|
|
$this->writer->writeAttribute('line', (string) $node->getLine());
|
2012-05-06 17:49:04 +02:00
|
|
|
$this->writer->text($node->getText());
|
2011-11-06 17:07:38 +01:00
|
|
|
$this->writer->endElement();
|
|
|
|
} elseif (is_array($node)) {
|
|
|
|
$this->writer->startElement('scalar:array');
|
|
|
|
foreach ($node as $subNode) {
|
|
|
|
$this->_serialize($subNode);
|
|
|
|
}
|
|
|
|
$this->writer->endElement();
|
|
|
|
} elseif (is_string($node)) {
|
|
|
|
$this->writer->writeElement('scalar:string', $node);
|
|
|
|
} elseif (is_int($node)) {
|
2015-03-23 11:43:22 +01:00
|
|
|
$this->writer->writeElement('scalar:int', (string) $node);
|
2011-11-06 17:07:38 +01:00
|
|
|
} elseif (is_float($node)) {
|
2015-03-23 11:43:22 +01:00
|
|
|
// TODO Higher precision conversion?
|
|
|
|
$this->writer->writeElement('scalar:float', (string) $node);
|
2011-11-06 17:07:38 +01:00
|
|
|
} elseif (true === $node) {
|
|
|
|
$this->writer->writeElement('scalar:true');
|
|
|
|
} elseif (false === $node) {
|
|
|
|
$this->writer->writeElement('scalar:false');
|
|
|
|
} elseif (null === $node) {
|
|
|
|
$this->writer->writeElement('scalar:null');
|
|
|
|
} else {
|
2014-02-06 14:44:16 +01:00
|
|
|
throw new \InvalidArgumentException('Unexpected node type');
|
2011-11-06 17:07:38 +01:00
|
|
|
}
|
|
|
|
}
|
2015-03-23 11:43:22 +01:00
|
|
|
}
|