@@ -63,7 +63,7 @@ class DOMCaster
63
63
\XML_NAMESPACE_DECL_NODE => 'XML_NAMESPACE_DECL_NODE ' ,
64
64
];
65
65
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
67
67
{
68
68
$ k = Caster::PREFIX_PROTECTED .'code ' ;
69
69
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
82
82
return $ a ;
83
83
}
84
84
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
86
86
{
87
87
$ a += [
88
88
Caster::PREFIX_VIRTUAL .'Core ' => '1.0 ' ,
@@ -92,7 +92,7 @@ public static function castImplementation(\DOMImplementation $dom, array $a, Stu
92
92
return $ a ;
93
93
}
94
94
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
96
96
{
97
97
$ a += [
98
98
'nodeName ' => $ dom ->nodeName ,
@@ -104,15 +104,21 @@ public static function castNode(\DOMNode $dom, array $a, Stub $stub, bool $isNes
104
104
'lastChild ' => new CutStub ($ dom ->lastChild ),
105
105
'previousSibling ' => new CutStub ($ dom ->previousSibling ),
106
106
'nextSibling ' => new CutStub ($ dom ->nextSibling ),
107
- 'attributes ' => $ dom ->attributes ,
108
107
'ownerDocument ' => new CutStub ($ dom ->ownerDocument ),
109
- 'namespaceURI ' => $ dom ->namespaceURI ,
110
- 'prefix ' => $ dom ->prefix ,
111
- 'localName ' => $ dom ->localName ,
112
108
'baseURI ' => $ dom ->baseURI ? new LinkStub ($ dom ->baseURI ) : $ dom ->baseURI ,
113
109
'textContent ' => new CutStub ($ dom ->textContent ),
114
110
];
115
111
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
+ }
121
+
116
122
return $ a ;
117
123
}
118
124
@@ -166,7 +172,48 @@ public static function castDocument(\DOMDocument $dom, array $a, Stub $stub, boo
166
172
return $ a ;
167
173
}
168
174
169
- public static function castCharacterData (\DOMCharacterData $ dom , array $ a , Stub $ stub , bool $ isNested ): array
175
+ public static function castXMLDocument (\DOM \XMLDocument $ dom , array $ a , Stub $ stub , bool $ isNested , int $ filter = 0 ): array
176
+ {
177
+ $ a += [
178
+ 'doctype ' => $ dom ->doctype ,
179
+ 'implementation ' => $ dom ->implementation ,
180
+ 'documentElement ' => new CutStub ($ dom ->documentElement ),
181
+ 'inputEncoding ' => $ dom ->inputEncoding ,
182
+ 'xmlEncoding ' => $ dom ->xmlEncoding ,
183
+ 'xmlStandalone ' => $ dom ->xmlStandalone ,
184
+ 'xmlVersion ' => $ dom ->xmlVersion ,
185
+ 'documentURI ' => $ dom ->documentURI ? new LinkStub ($ dom ->documentURI ) : $ dom ->documentURI ,
186
+ 'formatOutput ' => $ dom ->formatOutput ,
187
+ ];
188
+
189
+ if (!($ filter & Caster::EXCLUDE_VERBOSE )) {
190
+ $ formatOutput = $ dom ->formatOutput ;
191
+ $ dom ->formatOutput = true ;
192
+ $ a += [Caster::PREFIX_VIRTUAL .'xml ' => $ dom ->saveXML ()];
193
+ $ dom ->formatOutput = $ formatOutput ;
194
+ }
195
+
196
+ return $ a ;
197
+ }
198
+
199
+ public static function castHTMLDocument (\DOM \HTMLDocument $ dom , array $ a , Stub $ stub , bool $ isNested , int $ filter = 0 ): array
200
+ {
201
+ $ a += [
202
+ 'doctype ' => $ dom ->doctype ,
203
+ 'implementation ' => $ dom ->implementation ,
204
+ 'documentElement ' => new CutStub ($ dom ->documentElement ),
205
+ 'inputEncoding ' => $ dom ->inputEncoding ,
206
+ 'documentURI ' => $ dom ->documentURI ? new LinkStub ($ dom ->documentURI ) : $ dom ->documentURI ,
207
+ ];
208
+
209
+ if (!($ filter & Caster::EXCLUDE_VERBOSE )) {
210
+ $ a += [Caster::PREFIX_VIRTUAL .'html ' => $ dom ->saveHTML ()];
211
+ }
212
+
213
+ return $ a ;
214
+ }
215
+
216
+ public static function castCharacterData (\DOMCharacterData |\DOM \CharacterData $ dom , array $ a , Stub $ stub , bool $ isNested ): array
170
217
{
171
218
$ a += [
172
219
'data ' => $ dom ->data ,
@@ -176,30 +223,40 @@ public static function castCharacterData(\DOMCharacterData $dom, array $a, Stub
176
223
return $ a ;
177
224
}
178
225
179
- public static function castAttr (\DOMAttr $ dom , array $ a , Stub $ stub , bool $ isNested ): array
226
+ public static function castAttr (\DOMAttr | \ DOM \ Attr $ dom , array $ a , Stub $ stub , bool $ isNested ): array
180
227
{
181
228
$ a += [
182
229
'name ' => $ dom ->name ,
183
230
'specified ' => $ dom ->specified ,
184
231
'value ' => $ dom ->value ,
185
232
'ownerElement ' => $ dom ->ownerElement ,
186
- 'schemaTypeInfo ' => $ dom ->schemaTypeInfo ,
187
233
];
188
234
235
+ if ($ dom instanceof \DOMAttr) {
236
+ $ a += [
237
+ 'schemaTypeInfo ' => $ dom ->schemaTypeInfo ,
238
+ ];
239
+ }
240
+
189
241
return $ a ;
190
242
}
191
243
192
- public static function castElement (\DOMElement $ dom , array $ a , Stub $ stub , bool $ isNested ): array
244
+ public static function castElement (\DOMElement | \ DOM \ Element $ dom , array $ a , Stub $ stub , bool $ isNested ): array
193
245
{
194
246
$ a += [
195
247
'tagName ' => $ dom ->tagName ,
196
- 'schemaTypeInfo ' => $ dom ->schemaTypeInfo ,
197
248
];
198
249
250
+ if ($ dom instanceof \DOMElement) {
251
+ $ a += [
252
+ 'schemaTypeInfo ' => $ dom ->schemaTypeInfo ,
253
+ ];
254
+ }
255
+
199
256
return $ a ;
200
257
}
201
258
202
- public static function castText (\DOMText $ dom , array $ a , Stub $ stub , bool $ isNested ): array
259
+ public static function castText (\DOMText | \ DOM \ Text $ dom , array $ a , Stub $ stub , bool $ isNested ): array
203
260
{
204
261
$ a += [
205
262
'wholeText ' => $ dom ->wholeText ,
@@ -208,7 +265,7 @@ public static function castText(\DOMText $dom, array $a, Stub $stub, bool $isNes
208
265
return $ a ;
209
266
}
210
267
211
- public static function castDocumentType (\DOMDocumentType $ dom , array $ a , Stub $ stub , bool $ isNested ): array
268
+ public static function castDocumentType (\DOMDocumentType | \ DOM \ DocumentType $ dom , array $ a , Stub $ stub , bool $ isNested ): array
212
269
{
213
270
$ a += [
214
271
'name ' => $ dom ->name ,
@@ -222,7 +279,7 @@ public static function castDocumentType(\DOMDocumentType $dom, array $a, Stub $s
222
279
return $ a ;
223
280
}
224
281
225
- public static function castNotation (\DOMNotation $ dom , array $ a , Stub $ stub , bool $ isNested ): array
282
+ public static function castNotation (\DOMNotation | \ DOM \ Notation $ dom , array $ a , Stub $ stub , bool $ isNested ): array
226
283
{
227
284
$ a += [
228
285
'publicId ' => $ dom ->publicId ,
@@ -232,7 +289,7 @@ public static function castNotation(\DOMNotation $dom, array $a, Stub $stub, boo
232
289
return $ a ;
233
290
}
234
291
235
- public static function castEntity (\DOMEntity $ dom , array $ a , Stub $ stub , bool $ isNested ): array
292
+ public static function castEntity (\DOMEntity | \ DOM \ Entity $ dom , array $ a , Stub $ stub , bool $ isNested ): array
236
293
{
237
294
$ a += [
238
295
'publicId ' => $ dom ->publicId ,
@@ -246,7 +303,7 @@ public static function castEntity(\DOMEntity $dom, array $a, Stub $stub, bool $i
246
303
return $ a ;
247
304
}
248
305
249
- public static function castProcessingInstruction (\DOMProcessingInstruction $ dom , array $ a , Stub $ stub , bool $ isNested ): array
306
+ public static function castProcessingInstruction (\DOMProcessingInstruction | \ DOM \ ProcessingInstruction $ dom , array $ a , Stub $ stub , bool $ isNested ): array
250
307
{
251
308
$ a += [
252
309
'target ' => $ dom ->target ,
@@ -256,7 +313,7 @@ public static function castProcessingInstruction(\DOMProcessingInstruction $dom,
256
313
return $ a ;
257
314
}
258
315
259
- public static function castXPath (\DOMXPath $ dom , array $ a , Stub $ stub , bool $ isNested ): array
316
+ public static function castXPath (\DOMXPath | \ DOM \ XPath $ dom , array $ a , Stub $ stub , bool $ isNested ): array
260
317
{
261
318
$ a += [
262
319
'document ' => $ dom ->document ,
0 commit comments