XML.php 3.1 KB

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