@@ -63,7 +63,7 @@ class DOMCaster
6363 \XML_NAMESPACE_DECL_NODE => 'XML_NAMESPACE_DECL_NODE ' ,
6464 ];
6565
66- public static function castException (\DOMException $ e , array $ a , Stub $ stub , bool $ isNested ): array
66+ public static function castException (\DOMException | \ DOM \ Exception $ e , array $ a , Stub $ stub , bool $ isNested ): array
6767 {
6868 $ k = Caster::PREFIX_PROTECTED .'code ' ;
6969 if (isset ($ a [$ k ], self ::ERROR_CODES [$ a [$ k ]])) {
@@ -82,7 +82,7 @@ public static function castLength($dom, array $a, Stub $stub, bool $isNested): a
8282 return $ a ;
8383 }
8484
85- public static function castImplementation (\DOMImplementation $ dom , array $ a , Stub $ stub , bool $ isNested ): array
85+ public static function castImplementation (\DOMImplementation | \ DOM \ Implementation $ dom , array $ a , Stub $ stub , bool $ isNested ): array
8686 {
8787 $ a += [
8888 Caster::PREFIX_VIRTUAL .'Core ' => '1.0 ' ,
@@ -92,7 +92,7 @@ public static function castImplementation(\DOMImplementation $dom, array $a, Stu
9292 return $ a ;
9393 }
9494
95- public static function castNode (\DOMNode $ dom , array $ a , Stub $ stub , bool $ isNested ): array
95+ public static function castNode (\DOMNode | \ DOM \ Node $ dom , array $ a , Stub $ stub , bool $ isNested ): array
9696 {
9797 $ a += [
9898 'nodeName ' => $ dom ->nodeName ,
@@ -104,15 +104,20 @@ public static function castNode(\DOMNode $dom, array $a, Stub $stub, bool $isNes
104104 'lastChild ' => new CutStub ($ dom ->lastChild ),
105105 'previousSibling ' => new CutStub ($ dom ->previousSibling ),
106106 'nextSibling ' => new CutStub ($ dom ->nextSibling ),
107- 'attributes ' => $ dom ->attributes ,
108107 'ownerDocument ' => new CutStub ($ dom ->ownerDocument ),
109- 'namespaceURI ' => $ dom ->namespaceURI ,
110- 'prefix ' => $ dom ->prefix ,
111- 'localName ' => $ dom ->localName ,
112108 'baseURI ' => $ dom ->baseURI ? new LinkStub ($ dom ->baseURI ) : $ dom ->baseURI ,
113109 'textContent ' => new CutStub ($ dom ->textContent ),
114110 ];
115111
112+ if ($ dom instanceof \DOMNode || $ dom instanceof \DOM \Element) {
113+ $ a += [
114+ 'attributes ' => $ dom ->attributes ,
115+ 'namespaceURI ' => $ dom ->namespaceURI ,
116+ 'prefix ' => $ dom ->prefix ,
117+ 'localName ' => $ dom ->localName ,
118+ ];
119+ }
120+
116121 return $ a ;
117122 }
118123
@@ -166,7 +171,48 @@ public static function castDocument(\DOMDocument $dom, array $a, Stub $stub, boo
166171 return $ a ;
167172 }
168173
169- public static function castCharacterData (\DOMCharacterData $ dom , array $ a , Stub $ stub , bool $ isNested ): array
174+ public static function castXMLDocument (\DOM \XMLDocument $ dom , array $ a , Stub $ stub , bool $ isNested , int $ filter = 0 ): array
175+ {
176+ $ a += [
177+ 'doctype ' => $ dom ->doctype ,
178+ 'implementation ' => $ dom ->implementation ,
179+ 'documentElement ' => new CutStub ($ dom ->documentElement ),
180+ 'inputEncoding ' => $ dom ->inputEncoding ,
181+ 'xmlEncoding ' => $ dom ->xmlEncoding ,
182+ 'xmlStandalone ' => $ dom ->xmlStandalone ,
183+ 'xmlVersion ' => $ dom ->xmlVersion ,
184+ 'documentURI ' => $ dom ->documentURI ? new LinkStub ($ dom ->documentURI ) : $ dom ->documentURI ,
185+ 'formatOutput ' => $ dom ->formatOutput ,
186+ ];
187+
188+ if (!($ filter & Caster::EXCLUDE_VERBOSE )) {
189+ $ formatOutput = $ dom ->formatOutput ;
190+ $ dom ->formatOutput = true ;
191+ $ a += [Caster::PREFIX_VIRTUAL .'xml ' => $ dom ->saveXML ()];
192+ $ dom ->formatOutput = $ formatOutput ;
193+ }
194+
195+ return $ a ;
196+ }
197+
198+ public static function castHTMLDocument (\DOM \HTMLDocument $ dom , array $ a , Stub $ stub , bool $ isNested , int $ filter = 0 ): array
199+ {
200+ $ a += [
201+ 'doctype ' => $ dom ->doctype ,
202+ 'implementation ' => $ dom ->implementation ,
203+ 'documentElement ' => new CutStub ($ dom ->documentElement ),
204+ 'inputEncoding ' => $ dom ->inputEncoding ,
205+ 'documentURI ' => $ dom ->documentURI ? new LinkStub ($ dom ->documentURI ) : $ dom ->documentURI ,
206+ ];
207+
208+ if (!($ filter & Caster::EXCLUDE_VERBOSE )) {
209+ $ a += [Caster::PREFIX_VIRTUAL .'html ' => $ dom ->saveHTML ()];
210+ }
211+
212+ return $ a ;
213+ }
214+
215+ public static function castCharacterData (\DOMCharacterData |\DOM \CharacterData $ dom , array $ a , Stub $ stub , bool $ isNested ): array
170216 {
171217 $ a += [
172218 'data ' => $ dom ->data ,
@@ -176,30 +222,40 @@ public static function castCharacterData(\DOMCharacterData $dom, array $a, Stub
176222 return $ a ;
177223 }
178224
179- public static function castAttr (\DOMAttr $ dom , array $ a , Stub $ stub , bool $ isNested ): array
225+ public static function castAttr (\DOMAttr | \ DOM \ Attr $ dom , array $ a , Stub $ stub , bool $ isNested ): array
180226 {
181227 $ a += [
182228 'name ' => $ dom ->name ,
183229 'specified ' => $ dom ->specified ,
184230 'value ' => $ dom ->value ,
185231 'ownerElement ' => $ dom ->ownerElement ,
186- 'schemaTypeInfo ' => $ dom ->schemaTypeInfo ,
187232 ];
188233
234+ if ($ dom instanceof \DOMAttr) {
235+ $ a += [
236+ 'schemaTypeInfo ' => $ dom ->schemaTypeInfo ,
237+ ];
238+ }
239+
189240 return $ a ;
190241 }
191242
192- public static function castElement (\DOMElement $ dom , array $ a , Stub $ stub , bool $ isNested ): array
243+ public static function castElement (\DOMElement | \ DOM \ Element $ dom , array $ a , Stub $ stub , bool $ isNested ): array
193244 {
194245 $ a += [
195246 'tagName ' => $ dom ->tagName ,
196- 'schemaTypeInfo ' => $ dom ->schemaTypeInfo ,
197247 ];
198248
249+ if ($ dom instanceof \DOMElement) {
250+ $ a += [
251+ 'schemaTypeInfo ' => $ dom ->schemaTypeInfo ,
252+ ];
253+ }
254+
199255 return $ a ;
200256 }
201257
202- public static function castText (\DOMText $ dom , array $ a , Stub $ stub , bool $ isNested ): array
258+ public static function castText (\DOMText | \ DOM \ Text $ dom , array $ a , Stub $ stub , bool $ isNested ): array
203259 {
204260 $ a += [
205261 'wholeText ' => $ dom ->wholeText ,
@@ -208,7 +264,7 @@ public static function castText(\DOMText $dom, array $a, Stub $stub, bool $isNes
208264 return $ a ;
209265 }
210266
211- public static function castDocumentType (\DOMDocumentType $ dom , array $ a , Stub $ stub , bool $ isNested ): array
267+ public static function castDocumentType (\DOMDocumentType | \ DOM \ DocumentType $ dom , array $ a , Stub $ stub , bool $ isNested ): array
212268 {
213269 $ a += [
214270 'name ' => $ dom ->name ,
@@ -222,7 +278,7 @@ public static function castDocumentType(\DOMDocumentType $dom, array $a, Stub $s
222278 return $ a ;
223279 }
224280
225- public static function castNotation (\DOMNotation $ dom , array $ a , Stub $ stub , bool $ isNested ): array
281+ public static function castNotation (\DOMNotation | \ DOM \ Notation $ dom , array $ a , Stub $ stub , bool $ isNested ): array
226282 {
227283 $ a += [
228284 'publicId ' => $ dom ->publicId ,
@@ -232,7 +288,7 @@ public static function castNotation(\DOMNotation $dom, array $a, Stub $stub, boo
232288 return $ a ;
233289 }
234290
235- public static function castEntity (\DOMEntity $ dom , array $ a , Stub $ stub , bool $ isNested ): array
291+ public static function castEntity (\DOMEntity | \ DOM \ Entity $ dom , array $ a , Stub $ stub , bool $ isNested ): array
236292 {
237293 $ a += [
238294 'publicId ' => $ dom ->publicId ,
@@ -246,7 +302,7 @@ public static function castEntity(\DOMEntity $dom, array $a, Stub $stub, bool $i
246302 return $ a ;
247303 }
248304
249- public static function castProcessingInstruction (\DOMProcessingInstruction $ dom , array $ a , Stub $ stub , bool $ isNested ): array
305+ public static function castProcessingInstruction (\DOMProcessingInstruction | \ DOM \ ProcessingInstruction $ dom , array $ a , Stub $ stub , bool $ isNested ): array
250306 {
251307 $ a += [
252308 'target ' => $ dom ->target ,
@@ -256,7 +312,7 @@ public static function castProcessingInstruction(\DOMProcessingInstruction $dom,
256312 return $ a ;
257313 }
258314
259- public static function castXPath (\DOMXPath $ dom , array $ a , Stub $ stub , bool $ isNested ): array
315+ public static function castXPath (\DOMXPath | \ DOM \ XPath $ dom , array $ a , Stub $ stub , bool $ isNested ): array
260316 {
261317 $ a += [
262318 'document ' => $ dom ->document ,
0 commit comments