@@ -77,10 +77,6 @@ public static function castException(\DOMException|\Dom\Exception $e, array $a,
77
77
78
78
public static function castLength ($ dom , array $ a , Stub $ stub , bool $ isNested ): array
79
79
{
80
- $ a += [
81
- 'length ' => $ dom ->length ,
82
- ];
83
-
84
80
return $ a ;
85
81
}
86
82
@@ -96,69 +92,16 @@ public static function castImplementation(\DOMImplementation|\Dom\Implementation
96
92
97
93
public static function castNode (\DOMNode |\Dom \Node $ dom , array $ a , Stub $ stub , bool $ isNested ): array
98
94
{
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 );
124
96
}
125
97
126
98
public static function castNameSpaceNode (\DOMNameSpaceNode $ dom , array $ a , Stub $ stub , bool $ isNested ): array
127
99
{
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 );
140
101
}
141
102
142
103
public static function castDocument (\DOMDocument $ dom , array $ a , Stub $ stub , bool $ isNested , int $ filter = 0 ): array
143
104
{
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
-
162
105
if (!($ filter & Caster::EXCLUDE_VERBOSE )) {
163
106
$ formatOutput = $ dom ->formatOutput ;
164
107
$ dom ->formatOutput = true ;
@@ -171,18 +114,6 @@ public static function castDocument(\DOMDocument $dom, array $a, Stub $stub, boo
171
114
172
115
public static function castXMLDocument (\Dom \XMLDocument $ dom , array $ a , Stub $ stub , bool $ isNested , int $ filter = 0 ): array
173
116
{
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
-
186
117
if (!($ filter & Caster::EXCLUDE_VERBOSE )) {
187
118
$ formatOutput = $ dom ->formatOutput ;
188
119
$ dom ->formatOutput = true ;
@@ -195,14 +126,6 @@ public static function castXMLDocument(\Dom\XMLDocument $dom, array $a, Stub $st
195
126
196
127
public static function castHTMLDocument (\Dom \HTMLDocument $ dom , array $ a , Stub $ stub , bool $ isNested , int $ filter = 0 ): array
197
128
{
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
-
206
129
if (!($ filter & Caster::EXCLUDE_VERBOSE )) {
207
130
$ a += [Caster::PREFIX_VIRTUAL .'html ' => $ dom ->saveHTML ()];
208
131
}
@@ -212,106 +135,74 @@ public static function castHTMLDocument(\Dom\HTMLDocument $dom, array $a, Stub $
212
135
213
136
public static function castCharacterData (\DOMCharacterData |\Dom \CharacterData $ dom , array $ a , Stub $ stub , bool $ isNested ): array
214
137
{
215
- $ a += [
216
- 'data ' => $ dom ->data ,
217
- 'length ' => $ dom ->length ,
218
- ];
219
-
220
138
return $ a ;
221
139
}
222
140
223
141
public static function castAttr (\DOMAttr |\Dom \Attr $ dom , array $ a , Stub $ stub , bool $ isNested ): array
224
142
{
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
-
238
143
return $ a ;
239
144
}
240
145
241
146
public static function castElement (\DOMElement |\Dom \Element $ dom , array $ a , Stub $ stub , bool $ isNested ): array
242
147
{
243
- $ a += [
244
- 'tagName ' => $ dom ->tagName ,
245
- ];
246
-
247
- if ($ dom instanceof \DOMElement) {
248
- $ a += [
249
- 'schemaTypeInfo ' => $ dom ->schemaTypeInfo ,
250
- ];
251
- }
252
-
253
148
return $ a ;
254
149
}
255
150
256
151
public static function castText (\DOMText |\Dom \Text $ dom , array $ a , Stub $ stub , bool $ isNested ): array
257
152
{
258
- $ a += [
259
- 'wholeText ' => $ dom ->wholeText ,
260
- ];
261
-
262
153
return $ a ;
263
154
}
264
155
265
156
public static function castDocumentType (\DOMDocumentType |\Dom \DocumentType $ dom , array $ a , Stub $ stub , bool $ isNested ): array
266
157
{
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
-
276
158
return $ a ;
277
159
}
278
160
279
161
public static function castNotation (\DOMNotation |\Dom \Notation $ dom , array $ a , Stub $ stub , bool $ isNested ): array
280
162
{
281
- $ a += [
282
- 'publicId ' => $ dom ->publicId ,
283
- 'systemId ' => $ dom ->systemId ,
284
- ];
285
-
286
163
return $ a ;
287
164
}
288
165
289
166
public static function castEntity (\DOMEntity |\Dom \Entity $ dom , array $ a , Stub $ stub , bool $ isNested ): array
290
167
{
291
- $ a += [
292
- 'publicId ' => $ dom ->publicId ,
293
- 'systemId ' => $ dom ->systemId ,
294
- 'notationName ' => $ dom ->notationName ,
295
- ];
296
-
297
168
return $ a ;
298
169
}
299
170
300
171
public static function castProcessingInstruction (\DOMProcessingInstruction |\Dom \ProcessingInstruction $ dom , array $ a , Stub $ stub , bool $ isNested ): array
301
172
{
302
- $ a += [
303
- 'target ' => $ dom ->target ,
304
- 'data ' => $ dom ->data ,
305
- ];
306
-
307
173
return $ a ;
308
174
}
309
175
310
176
public static function castXPath (\DOMXPath |\Dom \XPath $ dom , array $ a , Stub $ stub , bool $ isNested ): array
311
177
{
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
+ }
315
206
316
207
return $ a ;
317
208
}
0 commit comments