1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpParser\Serializer; |
4
|
|
|
|
5
|
|
|
use XMLWriter; |
6
|
|
|
use PhpParser\Node; |
7
|
|
|
use PhpParser\Comment; |
8
|
|
|
use PhpParser\Serializer; |
9
|
|
|
|
10
|
|
|
class XML implements Serializer |
11
|
|
|
{ |
12
|
|
|
protected $writer; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Constructs a XML serializer. |
16
|
|
|
*/ |
17
|
|
|
public function __construct() { |
18
|
|
|
$this->writer = new XMLWriter; |
19
|
|
|
$this->writer->openMemory(); |
20
|
|
|
$this->writer->setIndent(true); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function serialize(array $nodes) { |
24
|
|
|
$this->writer->flush(); |
25
|
|
|
$this->writer->startDocument('1.0', 'UTF-8'); |
26
|
|
|
|
27
|
|
|
$this->writer->startElement('AST'); |
28
|
|
|
$this->writer->writeAttribute('xmlns:node', 'http://nikic.github.com/PHPParser/XML/node'); |
29
|
|
|
$this->writer->writeAttribute('xmlns:subNode', 'http://nikic.github.com/PHPParser/XML/subNode'); |
30
|
|
|
$this->writer->writeAttribute('xmlns:attribute', 'http://nikic.github.com/PHPParser/XML/attribute'); |
31
|
|
|
$this->writer->writeAttribute('xmlns:scalar', 'http://nikic.github.com/PHPParser/XML/scalar'); |
32
|
|
|
|
33
|
|
|
$this->_serialize($nodes); |
34
|
|
|
|
35
|
|
|
$this->writer->endElement(); |
36
|
|
|
|
37
|
|
|
return $this->writer->outputMemory(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
protected function _serialize($node) { |
41
|
|
|
if ($node instanceof Node) { |
42
|
|
|
$this->writer->startElement('node:' . $node->getType()); |
43
|
|
|
|
44
|
|
View Code Duplication |
foreach ($node->getAttributes() as $name => $value) { |
|
|
|
|
45
|
|
|
$this->writer->startElement('attribute:' . $name); |
46
|
|
|
$this->_serialize($value); |
47
|
|
|
$this->writer->endElement(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
View Code Duplication |
foreach ($node as $name => $subNode) { |
|
|
|
|
51
|
|
|
$this->writer->startElement('subNode:' . $name); |
52
|
|
|
$this->_serialize($subNode); |
53
|
|
|
$this->writer->endElement(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$this->writer->endElement(); |
57
|
|
|
} elseif ($node instanceof Comment) { |
58
|
|
|
$this->writer->startElement('comment'); |
59
|
|
|
$this->writer->writeAttribute('isDocComment', $node instanceof Comment\Doc ? 'true' : 'false'); |
60
|
|
|
$this->writer->writeAttribute('line', (string) $node->getLine()); |
61
|
|
|
$this->writer->text($node->getText()); |
62
|
|
|
$this->writer->endElement(); |
63
|
|
|
} elseif (is_array($node)) { |
64
|
|
|
$this->writer->startElement('scalar:array'); |
65
|
|
|
foreach ($node as $subNode) { |
66
|
|
|
$this->_serialize($subNode); |
67
|
|
|
} |
68
|
|
|
$this->writer->endElement(); |
69
|
|
|
} elseif (is_string($node)) { |
70
|
|
|
$this->writer->writeElement('scalar:string', $node); |
71
|
|
|
} elseif (is_int($node)) { |
72
|
|
|
$this->writer->writeElement('scalar:int', (string) $node); |
73
|
|
|
} elseif (is_float($node)) { |
74
|
|
|
// TODO Higher precision conversion? |
75
|
|
|
$this->writer->writeElement('scalar:float', (string) $node); |
76
|
|
|
} elseif (true === $node) { |
77
|
|
|
$this->writer->writeElement('scalar:true'); |
78
|
|
|
} elseif (false === $node) { |
79
|
|
|
$this->writer->writeElement('scalar:false'); |
80
|
|
|
} elseif (null === $node) { |
81
|
|
|
$this->writer->writeElement('scalar:null'); |
82
|
|
|
} else { |
83
|
|
|
throw new \InvalidArgumentException('Unexpected node type'); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.