No Description

XML.php 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. namespace PhpParser\Unserializer;
  3. use XMLReader;
  4. use DomainException;
  5. use PhpParser\Unserializer;
  6. class XML implements Unserializer
  7. {
  8. protected $reader;
  9. public function __construct() {
  10. $this->reader = new XMLReader;
  11. }
  12. public function unserialize($string) {
  13. $this->reader->XML($string);
  14. $this->reader->read();
  15. if ('AST' !== $this->reader->name) {
  16. throw new DomainException('AST root element not found');
  17. }
  18. return $this->read($this->reader->depth);
  19. }
  20. protected function read($depthLimit, $throw = true, &$nodeFound = null) {
  21. $nodeFound = true;
  22. while ($this->reader->read() && $depthLimit < $this->reader->depth) {
  23. if (XMLReader::ELEMENT !== $this->reader->nodeType) {
  24. continue;
  25. }
  26. if ('node' === $this->reader->prefix) {
  27. return $this->readNode();
  28. } elseif ('scalar' === $this->reader->prefix) {
  29. return $this->readScalar();
  30. } elseif ('comment' === $this->reader->name) {
  31. return $this->readComment();
  32. } else {
  33. throw new DomainException(sprintf('Unexpected node of type "%s"', $this->reader->name));
  34. }
  35. }
  36. $nodeFound = false;
  37. if ($throw) {
  38. throw new DomainException('Expected node or scalar');
  39. }
  40. }
  41. protected function readNode() {
  42. $className = $this->getClassNameFromType($this->reader->localName);
  43. // create the node without calling it's constructor
  44. $node = unserialize(
  45. sprintf(
  46. "O:%d:\"%s\":2:{s:11:\"\0*\0subNodes\";a:0:{}s:13:\"\0*\0attributes\";a:0:{}}",
  47. strlen($className), $className
  48. )
  49. );
  50. $depthLimit = $this->reader->depth;
  51. while ($this->reader->read() && $depthLimit < $this->reader->depth) {
  52. if (XMLReader::ELEMENT !== $this->reader->nodeType) {
  53. continue;
  54. }
  55. $type = $this->reader->prefix;
  56. if ('subNode' !== $type && 'attribute' !== $type) {
  57. throw new DomainException(
  58. sprintf('Expected sub node or attribute, got node of type "%s"', $this->reader->name)
  59. );
  60. }
  61. $name = $this->reader->localName;
  62. $value = $this->read($this->reader->depth);
  63. if ('subNode' === $type) {
  64. $node->$name = $value;
  65. } else {
  66. $node->setAttribute($name, $value);
  67. }
  68. }
  69. return $node;
  70. }
  71. protected function readScalar() {
  72. switch ($name = $this->reader->localName) {
  73. case 'array':
  74. $depth = $this->reader->depth;
  75. $array = array();
  76. while (true) {
  77. $node = $this->read($depth, false, $nodeFound);
  78. if (!$nodeFound) {
  79. break;
  80. }
  81. $array[] = $node;
  82. }
  83. return $array;
  84. case 'string':
  85. return $this->reader->readString();
  86. case 'int':
  87. $text = $this->reader->readString();
  88. if (false === $int = filter_var($text, FILTER_VALIDATE_INT)) {
  89. throw new DomainException(sprintf('"%s" is not a valid integer', $text));
  90. }
  91. return $int;
  92. case 'float':
  93. $text = $this->reader->readString();
  94. if (false === $float = filter_var($text, FILTER_VALIDATE_FLOAT)) {
  95. throw new DomainException(sprintf('"%s" is not a valid float', $text));
  96. }
  97. return $float;
  98. case 'true':
  99. case 'false':
  100. case 'null':
  101. if (!$this->reader->isEmptyElement) {
  102. throw new DomainException(sprintf('"%s" scalar must be empty', $name));
  103. }
  104. return constant($name);
  105. default:
  106. throw new DomainException(sprintf('Unknown scalar type "%s"', $name));
  107. }
  108. }
  109. protected function readComment() {
  110. $className = $this->reader->getAttribute('isDocComment') === 'true'
  111. ? 'PhpParser\Comment\Doc'
  112. : 'PhpParser\Comment'
  113. ;
  114. return new $className(
  115. $this->reader->readString(),
  116. $this->reader->getAttribute('line')
  117. );
  118. }
  119. protected function getClassNameFromType($type) {
  120. $className = 'PhpParser\\Node\\' . strtr($type, '_', '\\');
  121. if (!class_exists($className)) {
  122. $className .= '_';
  123. }
  124. if (!class_exists($className)) {
  125. throw new DomainException(sprintf('Unknown node type "%s"', $type));
  126. }
  127. return $className;
  128. }
  129. }