@@ -77,10 +77,6 @@ public static function castException(\DOMException|\Dom\Exception $e, array $a,
7777
7878 public static function castLength ($ dom , array $ a , Stub $ stub , bool $ isNested ): array
7979 {
80- $ a += [
81- 'length ' => $ dom ->length ,
82- ];
83-
8480 return $ a ;
8581 }
8682
@@ -96,69 +92,16 @@ public static function castImplementation(\DOMImplementation|\Dom\Implementation
9692
9793 public static function castNode (\DOMNode |\Dom \Node $ dom , array $ a , Stub $ stub , bool $ isNested ): array
9894 {
99- $ a += [
100- 'nodeName ' => $ dom ->nodeName ,
101- 'nodeValue ' => new CutStub ($ dom ->nodeValue ),
102- 'nodeType ' => new ConstStub (self ::NODE_TYPES [$ dom ->nodeType ], $ dom ->nodeType ),
103- 'parentNode ' => new CutStub ($ dom ->parentNode ),
104- 'childNodes ' => $ dom ->childNodes ,
105- 'firstChild ' => new CutStub ($ dom ->firstChild ),
106- 'lastChild ' => new CutStub ($ dom ->lastChild ),
107- 'previousSibling ' => new CutStub ($ dom ->previousSibling ),
108- 'nextSibling ' => new CutStub ($ dom ->nextSibling ),
109- 'ownerDocument ' => new CutStub ($ dom ->ownerDocument ),
110- 'baseURI ' => $ dom ->baseURI ? new LinkStub ($ dom ->baseURI ) : $ dom ->baseURI ,
111- 'textContent ' => new CutStub ($ dom ->textContent ),
112- ];
113-
114- if ($ dom instanceof \DOMNode || $ dom instanceof \Dom \Element) {
115- $ a += [
116- 'attributes ' => $ dom ->attributes ,
117- 'namespaceURI ' => $ dom ->namespaceURI ,
118- 'prefix ' => $ dom ->prefix ,
119- 'localName ' => $ dom ->localName ,
120- ];
121- }
122-
123- return $ a ;
95+ return self ::castDom ($ dom , $ a , $ stub , $ isNested );
12496 }
12597
12698 public static function castNameSpaceNode (\DOMNameSpaceNode $ dom , array $ a , Stub $ stub , bool $ isNested ): array
12799 {
128- $ a += [
129- 'nodeName ' => $ dom ->nodeName ,
130- 'nodeValue ' => new CutStub ($ dom ->nodeValue ),
131- 'nodeType ' => new ConstStub (self ::NODE_TYPES [$ dom ->nodeType ], $ dom ->nodeType ),
132- 'prefix ' => $ dom ->prefix ,
133- 'localName ' => $ dom ->localName ,
134- 'namespaceURI ' => $ dom ->namespaceURI ,
135- 'ownerDocument ' => new CutStub ($ dom ->ownerDocument ),
136- 'parentNode ' => new CutStub ($ dom ->parentNode ),
137- ];
138-
139- return $ a ;
100+ return self ::castDom ($ dom , $ a , $ stub , $ isNested );
140101 }
141102
142103 public static function castDocument (\DOMDocument $ dom , array $ a , Stub $ stub , bool $ isNested , int $ filter = 0 ): array
143104 {
144- $ a += [
145- 'doctype ' => $ dom ->doctype ,
146- 'implementation ' => $ dom ->implementation ,
147- 'documentElement ' => new CutStub ($ dom ->documentElement ),
148- 'encoding ' => $ dom ->encoding ,
149- 'xmlEncoding ' => $ dom ->xmlEncoding ,
150- 'xmlStandalone ' => $ dom ->xmlStandalone ,
151- 'xmlVersion ' => $ dom ->xmlVersion ,
152- 'strictErrorChecking ' => $ dom ->strictErrorChecking ,
153- 'documentURI ' => $ dom ->documentURI ? new LinkStub ($ dom ->documentURI ) : $ dom ->documentURI ,
154- 'formatOutput ' => $ dom ->formatOutput ,
155- 'validateOnParse ' => $ dom ->validateOnParse ,
156- 'resolveExternals ' => $ dom ->resolveExternals ,
157- 'preserveWhiteSpace ' => $ dom ->preserveWhiteSpace ,
158- 'recover ' => $ dom ->recover ,
159- 'substituteEntities ' => $ dom ->substituteEntities ,
160- ];
161-
162105 if (!($ filter & Caster::EXCLUDE_VERBOSE )) {
163106 $ formatOutput = $ dom ->formatOutput ;
164107 $ dom ->formatOutput = true ;
@@ -171,18 +114,6 @@ public static function castDocument(\DOMDocument $dom, array $a, Stub $stub, boo
171114
172115 public static function castXMLDocument (\Dom \XMLDocument $ dom , array $ a , Stub $ stub , bool $ isNested , int $ filter = 0 ): array
173116 {
174- $ a += [
175- 'doctype ' => $ dom ->doctype ,
176- 'implementation ' => $ dom ->implementation ,
177- 'documentElement ' => new CutStub ($ dom ->documentElement ),
178- 'inputEncoding ' => $ dom ->inputEncoding ,
179- 'xmlEncoding ' => $ dom ->xmlEncoding ,
180- 'xmlStandalone ' => $ dom ->xmlStandalone ,
181- 'xmlVersion ' => $ dom ->xmlVersion ,
182- 'documentURI ' => $ dom ->documentURI ? new LinkStub ($ dom ->documentURI ) : $ dom ->documentURI ,
183- 'formatOutput ' => $ dom ->formatOutput ,
184- ];
185-
186117 if (!($ filter & Caster::EXCLUDE_VERBOSE )) {
187118 $ formatOutput = $ dom ->formatOutput ;
188119 $ dom ->formatOutput = true ;
@@ -195,14 +126,6 @@ public static function castXMLDocument(\Dom\XMLDocument $dom, array $a, Stub $st
195126
196127 public static function castHTMLDocument (\Dom \HTMLDocument $ dom , array $ a , Stub $ stub , bool $ isNested , int $ filter = 0 ): array
197128 {
198- $ a += [
199- 'doctype ' => $ dom ->doctype ,
200- 'implementation ' => $ dom ->implementation ,
201- 'documentElement ' => new CutStub ($ dom ->documentElement ),
202- 'inputEncoding ' => $ dom ->inputEncoding ,
203- 'documentURI ' => $ dom ->documentURI ? new LinkStub ($ dom ->documentURI ) : $ dom ->documentURI ,
204- ];
205-
206129 if (!($ filter & Caster::EXCLUDE_VERBOSE )) {
207130 $ a += [Caster::PREFIX_VIRTUAL .'html ' => $ dom ->saveHTML ()];
208131 }
@@ -212,106 +135,74 @@ public static function castHTMLDocument(\Dom\HTMLDocument $dom, array $a, Stub $
212135
213136 public static function castCharacterData (\DOMCharacterData |\Dom \CharacterData $ dom , array $ a , Stub $ stub , bool $ isNested ): array
214137 {
215- $ a += [
216- 'data ' => $ dom ->data ,
217- 'length ' => $ dom ->length ,
218- ];
219-
220138 return $ a ;
221139 }
222140
223141 public static function castAttr (\DOMAttr |\Dom \Attr $ dom , array $ a , Stub $ stub , bool $ isNested ): array
224142 {
225- $ a += [
226- 'name ' => $ dom ->name ,
227- 'specified ' => $ dom ->specified ,
228- 'value ' => $ dom ->value ,
229- 'ownerElement ' => $ dom ->ownerElement ,
230- ];
231-
232- if ($ dom instanceof \DOMAttr) {
233- $ a += [
234- 'schemaTypeInfo ' => $ dom ->schemaTypeInfo ,
235- ];
236- }
237-
238143 return $ a ;
239144 }
240145
241146 public static function castElement (\DOMElement |\Dom \Element $ dom , array $ a , Stub $ stub , bool $ isNested ): array
242147 {
243- $ a += [
244- 'tagName ' => $ dom ->tagName ,
245- ];
246-
247- if ($ dom instanceof \DOMElement) {
248- $ a += [
249- 'schemaTypeInfo ' => $ dom ->schemaTypeInfo ,
250- ];
251- }
252-
253148 return $ a ;
254149 }
255150
256151 public static function castText (\DOMText |\Dom \Text $ dom , array $ a , Stub $ stub , bool $ isNested ): array
257152 {
258- $ a += [
259- 'wholeText ' => $ dom ->wholeText ,
260- ];
261-
262153 return $ a ;
263154 }
264155
265156 public static function castDocumentType (\DOMDocumentType |\Dom \DocumentType $ dom , array $ a , Stub $ stub , bool $ isNested ): array
266157 {
267- $ a += [
268- 'name ' => $ dom ->name ,
269- 'entities ' => $ dom ->entities ,
270- 'notations ' => $ dom ->notations ,
271- 'publicId ' => $ dom ->publicId ,
272- 'systemId ' => $ dom ->systemId ,
273- 'internalSubset ' => $ dom ->internalSubset ,
274- ];
275-
276158 return $ a ;
277159 }
278160
279161 public static function castNotation (\DOMNotation |\Dom \Notation $ dom , array $ a , Stub $ stub , bool $ isNested ): array
280162 {
281- $ a += [
282- 'publicId ' => $ dom ->publicId ,
283- 'systemId ' => $ dom ->systemId ,
284- ];
285-
286163 return $ a ;
287164 }
288165
289166 public static function castEntity (\DOMEntity |\Dom \Entity $ dom , array $ a , Stub $ stub , bool $ isNested ): array
290167 {
291- $ a += [
292- 'publicId ' => $ dom ->publicId ,
293- 'systemId ' => $ dom ->systemId ,
294- 'notationName ' => $ dom ->notationName ,
295- ];
296-
297168 return $ a ;
298169 }
299170
300171 public static function castProcessingInstruction (\DOMProcessingInstruction |\Dom \ProcessingInstruction $ dom , array $ a , Stub $ stub , bool $ isNested ): array
301172 {
302- $ a += [
303- 'target ' => $ dom ->target ,
304- 'data ' => $ dom ->data ,
305- ];
306-
307173 return $ a ;
308174 }
309175
310176 public static function castXPath (\DOMXPath |\Dom \XPath $ dom , array $ a , Stub $ stub , bool $ isNested ): array
311177 {
312- $ a += [
313- 'document ' => $ dom ->document ,
314- ];
178+ return self ::castDom ($ dom , $ a , $ stub , $ isNested );
179+ }
180+
181+ public static function castDom ($ dom , array $ a , Stub $ stub , bool $ isNested , int $ filter = 0 ): array
182+ {
183+ foreach ($ a as $ k => $ v ) {
184+ if ('encoding ' === $ k && $ dom instanceof \DOMEntity
185+ || \in_array ($ k , ['actualEncoding ' , 'config ' , 'standalone ' , 'version ' ], true )
186+ ) {
187+ continue ; // deprecated properties
188+ }
189+
190+ $ v = $ dom ->$ k ;
191+
192+ $ a [$ k ] = match (true ) {
193+ $ v instanceof \DOMNode || $ v instanceof \Dom \Node => new CutStub ($ v ),
194+ 'nodeType ' === $ k => new ConstStub (self ::NODE_TYPES [$ v ], $ v ),
195+ 'baseURI ' === $ k && $ v ,
196+ 'documentURI ' === $ k && $ v => new LinkStub ($ v ),
197+ default => $ v ,
198+ };
199+ }
200+
201+ if ($ dom instanceof \IteratorAggregate) {
202+ foreach ($ dom as $ k => $ v ) {
203+ $ a [Caster::PREFIX_VIRTUAL .$ k ] = $ v ;
204+ }
205+ }
315206
316207 return $ a ;
317208 }
0 commit comments