4b16dd6fc83b5b588a2ffeac5e7d0305acc595c4
[yaffs-website] / vendor / nikic / php-parser / lib / PhpParser / Serializer / XML.php
1 <?php
2
3 namespace PhpParser\Serializer;
4
5 use PhpParser\Comment;
6 use PhpParser\Node;
7 use PhpParser\Serializer;
8 use XMLWriter;
9
10 /**
11  * @deprecated
12  */
13 class XML implements Serializer
14 {
15     protected $writer;
16
17     /**
18      * Constructs a XML serializer.
19      */
20     public function __construct() {
21         $this->writer = new XMLWriter;
22         $this->writer->openMemory();
23         $this->writer->setIndent(true);
24     }
25
26     public function serialize(array $nodes) {
27         $this->writer->flush();
28         $this->writer->startDocument('1.0', 'UTF-8');
29
30         $this->writer->startElement('AST');
31         $this->writer->writeAttribute('xmlns:node',      'http://nikic.github.com/PHPParser/XML/node');
32         $this->writer->writeAttribute('xmlns:subNode',   'http://nikic.github.com/PHPParser/XML/subNode');
33         $this->writer->writeAttribute('xmlns:attribute', 'http://nikic.github.com/PHPParser/XML/attribute');
34         $this->writer->writeAttribute('xmlns:scalar',    'http://nikic.github.com/PHPParser/XML/scalar');
35
36         $this->_serialize($nodes);
37
38         $this->writer->endElement();
39
40         return $this->writer->outputMemory();
41     }
42
43     protected function _serialize($node) {
44         if ($node instanceof Node) {
45             $this->writer->startElement('node:' . $node->getType());
46
47             foreach ($node->getAttributes() as $name => $value) {
48                 $this->writer->startElement('attribute:' . $name);
49                 $this->_serialize($value);
50                 $this->writer->endElement();
51             }
52
53             foreach ($node as $name => $subNode) {
54                 $this->writer->startElement('subNode:' . $name);
55                 $this->_serialize($subNode);
56                 $this->writer->endElement();
57             }
58
59             $this->writer->endElement();
60         } elseif ($node instanceof Comment) {
61             $this->writer->startElement('comment');
62             $this->writer->writeAttribute('isDocComment', $node instanceof Comment\Doc ? 'true' : 'false');
63             $this->writer->writeAttribute('line', (string) $node->getLine());
64             $this->writer->text($node->getText());
65             $this->writer->endElement();
66         } elseif (is_array($node)) {
67             $this->writer->startElement('scalar:array');
68             foreach ($node as $subNode) {
69                 $this->_serialize($subNode);
70             }
71             $this->writer->endElement();
72         } elseif (is_string($node)) {
73             $this->writer->writeElement('scalar:string', $node);
74         } elseif (is_int($node)) {
75             $this->writer->writeElement('scalar:int', (string) $node);
76         } elseif (is_float($node)) {
77             // TODO Higher precision conversion?
78             $this->writer->writeElement('scalar:float', (string) $node);
79         } elseif (true === $node) {
80             $this->writer->writeElement('scalar:true');
81         } elseif (false === $node) {
82             $this->writer->writeElement('scalar:false');
83         } elseif (null === $node) {
84             $this->writer->writeElement('scalar:null');
85         } else {
86             throw new \InvalidArgumentException('Unexpected node type');
87         }
88     }
89 }