mirror of
https://github.com/danog/PHP-Parser.git
synced 2024-12-04 02:07:59 +01:00
74 lines
2.4 KiB
PHP
74 lines
2.4 KiB
PHP
|
<?php
|
||
|
|
||
|
class PHPParser_Serializer_XML implements PHPParser_Serializer
|
||
|
{
|
||
|
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');
|
||
|
$this->writer->writeAttribute('xmlns:node', 'PHPParser/node');
|
||
|
$this->writer->writeAttribute('xmlns:subNode', 'PHPParser/subNode');
|
||
|
$this->writer->writeAttribute('xmlns:scalar', 'PHPParser/scalar');
|
||
|
|
||
|
$this->_serialize($nodes);
|
||
|
|
||
|
$this->writer->endElement();
|
||
|
|
||
|
return $this->writer->outputMemory();
|
||
|
}
|
||
|
|
||
|
public function _serialize($node) {
|
||
|
if ($node instanceof PHPParser_Node) {
|
||
|
$this->writer->startElement('node:' . $node->getType());
|
||
|
|
||
|
if (-1 !== $line = $node->getLine()) {
|
||
|
$this->writer->writeAttribute('line', $line);
|
||
|
}
|
||
|
|
||
|
if (null !== $docComment = $node->getDocComment()) {
|
||
|
$this->writer->writeAttribute('docComment', $docComment);
|
||
|
}
|
||
|
|
||
|
foreach ($node as $name => $subNode) {
|
||
|
$this->writer->startElement('subNode:' . $name);
|
||
|
$this->_serialize($subNode);
|
||
|
|
||
|
$this->writer->endElement();
|
||
|
}
|
||
|
|
||
|
$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)) {
|
||
|
$this->writer->writeElement('scalar:int', $node);
|
||
|
} elseif (is_float($node)) {
|
||
|
$this->writer->writeElement('scalar:float', $node);
|
||
|
} 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 {
|
||
|
throw new Exception('Unexpected node type');
|
||
|
}
|
||
|
}
|
||
|
}
|