8
8
* XML_Core class.
9
9
*/
10
10
11
- class XML_Core
12
- {
11
+ class XML_Core {
13
12
/**
14
13
* @var string XML document version
15
14
*/
@@ -77,8 +76,8 @@ public static function factory($driver = NULL, $root_node = NULL, $element = NUL
77
76
return new XML ($ element , $ root_node );
78
77
}
79
78
}
80
-
81
-
79
+
80
+
82
81
/**
83
82
* Class constructor. You should use the factory instead.
84
83
* @param string $element [optional] What to construct from. Could be some xml string, a file name, or a DOMNode
@@ -136,10 +135,10 @@ public function __construct($element = NULL, $root_node = NULL)
136
135
// Create the root node
137
136
$ root_node = $ this ->dom_doc ->createElement ($ this ->root_node );
138
137
}
139
-
138
+
140
139
// Append the root node to the object DOMDocument, and set the resulting DOMNode as it's node
141
140
$ this ->dom_node = $ this ->dom_doc ->appendChild ($ root_node );
142
-
141
+
143
142
// Add other attributes
144
143
$ this ->add_attributes ($ this ->dom_node );
145
144
}
@@ -159,31 +158,24 @@ public function __construct($element = NULL, $root_node = NULL)
159
158
* @return XML instance for the node that's been added.
160
159
*/
161
160
public function add_node ($ name , $ value = NULL , $ attributes = array ())
162
- {
161
+ {
163
162
// Trim the name
164
163
$ name = trim ($ name );
165
-
164
+
166
165
// Create the element
167
166
$ node = $ this ->create_element ($ name );
168
167
169
168
// Add the attributes
170
169
$ this ->add_attributes ($ node , $ attributes );
171
-
170
+
172
171
// Add the value if provided
173
172
if ($ value !== NULL )
174
- {
175
- $ value = strval ($ this ->filter ($ name , $ value , &$ node ));
176
-
177
- if (str_replace (array ('< ' , '> ' , '& ' ), "" , $ value ) === $ value )
178
- {
179
- // Value is valid CDATA, let's add it as a new text node
180
- $ value = $ this ->dom_doc ->createTextNode ($ value );
181
- }
182
- else
183
- {
184
- // We shall create a CDATA section to wrap the text provided
185
- $ value = $ this ->dom_doc ->createCDATASection ($ value );
186
- }
173
+ {
174
+ $ value = strval ($ this ->filter ($ name , $ value , $ node ));
175
+
176
+ // Value is valid CDATA, let's add it as a new text node
177
+ $ value = $ this ->dom_doc ->createTextNode ($ value );
178
+
187
179
$ node ->appendChild ($ value );
188
180
}
189
181
@@ -207,7 +199,7 @@ public function __get($value)
207
199
if ( ! isset ($ this ->$ value ))
208
200
{
209
201
$ node = current ($ this ->get ($ value ));
210
-
202
+
211
203
if ($ node instanceof XML )
212
204
{
213
205
// Return the whole XML document
@@ -231,7 +223,7 @@ public function __get($value)
231
223
public function get ($ value , $ as_array = FALSE )
232
224
{
233
225
$ return = array ();
234
-
226
+
235
227
$ value = $ this ->meta ()->alias ($ value );
236
228
237
229
foreach ($ this ->dom_node ->getElementsByTagName ($ value ) as $ item )
@@ -264,9 +256,9 @@ public function get($value, $as_array = FALSE)
264
256
public function xpath ($ query , $ as_array = TRUE )
265
257
{
266
258
$ return = array ();
267
-
259
+
268
260
$ xpath = new DOMXPath ($ this ->dom_doc );
269
-
261
+
270
262
foreach ($ xpath ->query ($ query ) as $ item )
271
263
{
272
264
if ($ as_array )
@@ -291,9 +283,9 @@ public function xpath($query, $as_array = TRUE)
291
283
/**
292
284
* Exports the document as a multi-dimensional array.
293
285
* Handles element with the same name.
294
- *
286
+ *
295
287
* Root node is ignored, as it is known and available in the driver.
296
- * Example :
288
+ * Example :
297
289
* <node_name attr_name="val">
298
290
* <child_node_name>
299
291
* value1
@@ -302,17 +294,17 @@ public function xpath($query, $as_array = TRUE)
302
294
* value2
303
295
* </child_node_name>
304
296
* </node_name>
305
- *
297
+ *
306
298
* Here's the resulting array structure :
307
299
* array ("node_name" => array(
308
- * // array of nodes called "node_name"
300
+ * // array of nodes called "node_name"
309
301
* 0 => array(
310
302
* // Attributes of that node
311
303
* "xml_attributes" => array(
312
304
* "attr_name" => "val",
313
305
* )
314
306
* // node contents
315
- * "child_node_name" => array(
307
+ * "child_node_name" => array(
316
308
* // array of nodes called "child_node_name"
317
309
* 0 => value1,
318
310
* 1 => value2,
@@ -323,9 +315,9 @@ public function xpath($query, $as_array = TRUE)
323
315
public function as_array ()
324
316
{
325
317
$ dom_element = $ this ->dom_node ;
326
-
318
+
327
319
$ return = array ();
328
-
320
+
329
321
// This function is run on a whole XML document and this is the root node.
330
322
// That root node shall be ignored in the array as it driven by the driver and handles document namespaces.
331
323
foreach ($ dom_element ->childNodes as $ dom_child )
@@ -341,7 +333,7 @@ public function as_array()
341
333
}
342
334
}
343
335
}
344
-
336
+
345
337
return $ return ;
346
338
}
347
339
@@ -374,7 +366,7 @@ private function _as_array(DOMNode $dom_node)
374
366
if ($ dom_child ->nodeType === XML_ELEMENT_NODE )
375
367
{
376
368
$ child = $ this ->_as_array ($ dom_child );
377
-
369
+
378
370
foreach ($ child as $ key =>$ val )
379
371
{
380
372
$ object_element [$ node_name ][$ key ][]=$ val ;
@@ -386,7 +378,7 @@ private function _as_array(DOMNode $dom_node)
386
378
// Get attributes
387
379
if ($ dom_node ->hasAttributes ())
388
380
{
389
- $ object_element [$ dom_node -> nodeName ][ 'xml_attributes ' ] = array ();
381
+ $ object_element [$ node_name ] = array ( 'xml_attributes ' => array () );
390
382
foreach ($ dom_node ->attributes as $ att_name => $ dom_attribute )
391
383
{
392
384
// Get the desired name for this attribute
@@ -451,13 +443,13 @@ protected function _from_array($mixed, DOMElement $dom_element)
451
443
// Create a new element with the key as the element name.
452
444
// Create the element corresponding to the key
453
445
$ node = $ this ->create_element ($ index );
454
-
446
+
455
447
// Add the driver attributes
456
448
$ this ->add_attributes ($ node );
457
449
458
450
// Append it
459
451
$ dom_element ->appendChild ($ node );
460
-
452
+
461
453
// Treat the array by recursion
462
454
$ this ->_from_array ($ mixed_element , $ node );
463
455
}
@@ -466,7 +458,7 @@ protected function _from_array($mixed, DOMElement $dom_element)
466
458
elseif ($ mixed )
467
459
{
468
460
// This is a string value that shall be appended as such
469
- $ mixed = $ this ->filter ($ dom_element ->tagName , $ mixed , & $ dom_element );
461
+ $ mixed = $ this ->filter ($ dom_element ->tagName , $ mixed , $ dom_element );
470
462
$ dom_element ->appendChild ($ this ->dom_doc ->createTextNode ($ mixed ));
471
463
}
472
464
}
@@ -499,7 +491,7 @@ public function import($xml)
499
491
// Import the node, and all its children, to the document
500
492
$ node = $ this ->dom_doc ->importNode ($ xml ->dom_node , TRUE );
501
493
$ this ->dom_node ->appendChild ($ node );
502
-
494
+
503
495
return $ this ;
504
496
}
505
497
@@ -605,7 +597,7 @@ private function add_attributes(DOMNode $node, $attributes = array())
605
597
protected function filter ($ name , $ value , &$ node )
606
598
{
607
599
$ name = $ this ->meta ()->alias ($ name );
608
-
600
+
609
601
if ($ this ->meta ()->get ("filter " , $ name ))
610
602
{
611
603
return call_user_func (array ($ this , $ this ->meta ()->get ("filter " , $ name )), $ value , $ node );
0 commit comments