PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 4.9.3
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v4.9.3
4.9.3 4.9.2 4.9.1 4.9.0 4.8.2 4.8.1 4.8.0 4.7.0 4.6.2 4.6.1 4.6.0 4.5.6 4.5.5 4.5.4 4.5.3 4.5.2 4.5.1 4.5.0 4.4.1 4.4.0 3.3.4 3.4.0 3.4.1 3.4.2 3.5.0 All 201 releases
betterdocs / includes / Dependencies / PhpParser / Unserializer / XML.php

XML.php in BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot 4.9.3, at includes/Dependencies/PhpParser/Unserializer/XML.php

157 lines 5.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 // phpcs:ignoreFile -- Bundled third-party (Mozart) dependency; exempt from plugin coding standards.
3
4 namespace WPDeveloper\BetterDocs\Dependencies\PhpParser\Unserializer;
5
6 use DomainException;
7 use WPDeveloper\BetterDocs\Dependencies\PhpParser\Unserializer;
8 use XMLReader;
9
10 /**
11 * @deprecated
12 */
13 class XML implements Unserializer
14 {
15 protected $reader;
16
17 public function __construct() {
18 $this->reader = new XMLReader;
19 }
20
21 public function unserialize($string) {
22 $this->reader->XML($string);
23
24 $this->reader->read();
25 if ('AST' !== $this->reader->name) {
26 throw new DomainException('AST root element not found');
27 }
28
29 return $this->read($this->reader->depth);
30 }
31
32 protected function read($depthLimit, $throw = true, &$nodeFound = null) {
33 $nodeFound = true;
34 while ($this->reader->read() && $depthLimit < $this->reader->depth) {
35 if (XMLReader::ELEMENT !== $this->reader->nodeType) {
36 continue;
37 }
38
39 if ('node' === $this->reader->prefix) {
40 return $this->readNode();
41 } elseif ('scalar' === $this->reader->prefix) {
42 return $this->readScalar();
43 } elseif ('comment' === $this->reader->name) {
44 return $this->readComment();
45 } else {
46 throw new DomainException(sprintf('Unexpected node of type "%s"', $this->reader->name));
47 }
48 }
49
50 $nodeFound = false;
51 if ($throw) {
52 throw new DomainException('Expected node or scalar');
53 }
54 }
55
56 protected function readNode() {
57 $className = $this->getClassNameFromType($this->reader->localName);
58
59 // create the node without calling it's constructor
60 $node = unserialize(
61 sprintf(
62 "O:%d:\"%s\":1:{s:13:\"\0*\0attributes\";a:0:{}}",
63 strlen($className), $className
64 )
65 );
66
67 $depthLimit = $this->reader->depth;
68 while ($this->reader->read() && $depthLimit < $this->reader->depth) {
69 if (XMLReader::ELEMENT !== $this->reader->nodeType) {
70 continue;
71 }
72
73 $type = $this->reader->prefix;
74 if ('subNode' !== $type && 'attribute' !== $type) {
75 throw new DomainException(
76 sprintf('Expected sub node or attribute, got node of type "%s"', $this->reader->name)
77 );
78 }
79
80 $name = $this->reader->localName;
81 $value = $this->read($this->reader->depth);
82
83 if ('subNode' === $type) {
84 $node->$name = $value;
85 } else {
86 $node->setAttribute($name, $value);
87 }
88 }
89
90 return $node;
91 }
92
93 protected function readScalar() {
94 switch ($name = $this->reader->localName) {
95 case 'array':
96 $depth = $this->reader->depth;
97 $array = array();
98 while (true) {
99 $node = $this->read($depth, false, $nodeFound);
100 if (!$nodeFound) {
101 break;
102 }
103 $array[] = $node;
104 }
105 return $array;
106 case 'string':
107 return $this->reader->readString();
108 case 'int':
109 return $this->parseInt($this->reader->readString());
110 case 'float':
111 $text = $this->reader->readString();
112 if (false === $float = filter_var($text, FILTER_VALIDATE_FLOAT)) {
113 throw new DomainException(sprintf('"%s" is not a valid float', $text));
114 }
115 return $float;
116 case 'true':
117 case 'false':
118 case 'null':
119 if (!$this->reader->isEmptyElement) {
120 throw new DomainException(sprintf('"%s" scalar must be empty', $name));
121 }
122 return constant($name);
123 default:
124 throw new DomainException(sprintf('Unknown scalar type "%s"', $name));
125 }
126 }
127
128 private function parseInt($text) {
129 if (false === $int = filter_var($text, FILTER_VALIDATE_INT)) {
130 throw new DomainException(sprintf('"%s" is not a valid integer', $text));
131 }
132 return $int;
133 }
134
135 protected function readComment() {
136 $className = $this->reader->getAttribute('isDocComment') === 'true'
137 ? 'WPDeveloper\BetterDocs\Dependencies\PhpParser\Comment\Doc'
138 : 'WPDeveloper\BetterDocs\Dependencies\PhpParser\Comment'
139 ;
140 return new $className(
141 $this->reader->readString(),
142 $this->parseInt($this->reader->getAttribute('line'))
143 );
144 }
145
146 protected function getClassNameFromType($type) {
147 $className = 'WPDeveloper\BetterDocs\Dependencies\PhpParser\\Node\\' . strtr($type, '_', '\\');
148 if (!class_exists($className)) {
149 $className .= '_';
150 }
151 if (!class_exists($className)) {
152 throw new DomainException(sprintf('Unknown node type "%s"', $type));
153 }
154 return $className;
155 }
156 }
157