菜谱项目

XML.php 3.1KB

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