1: <?php
2:
3: namespace Scopus\Util;
4:
5: use SimpleXMLElement;
6:
7: class XmlUtil
8: {
9: public static function toArray(SimpleXMLElement $xml, array $options = [])
10: {
11: $defaults = array(
12: 'namespaceSeparator' => ':', //you may want this to be something other than a colon
13: 'attributePrefix' => '@', //to distinguish between attributes and nodes with the same name
14: 'alwaysArray' => array(), //array of xml tag names which should always become arrays
15: 'autoArray' => true, //only create arrays for tags which appear more than once
16: 'textContent' => '$', //key used for the text content of elements
17: 'autoText' => true, //skip textContent key if node has no attributes or child nodes
18: 'keySearch' => false, //optional search and replace on tag and attribute names
19: 'keyReplace' => false //replace values for above search values (as passed to str_replace())
20: );
21: $options = array_merge($defaults, $options);
22: $namespaces = $xml->getDocNamespaces();
23: $namespaces[''] = null; //add base (empty) namespace
24:
25: //get attributes from all namespaces
26: $attributesArray = array();
27: foreach ($namespaces as $prefix => $namespace) {
28: foreach ($xml->attributes($namespace) as $attributeName => $attribute) {
29: //replace characters in attribute name
30: if ($options['keySearch']) $attributeName =
31: str_replace($options['keySearch'], $options['keyReplace'], $attributeName);
32: $attributeKey = $options['attributePrefix']
33: . ($prefix ? $prefix . $options['namespaceSeparator'] : '')
34: . $attributeName;
35: $attributesArray[$attributeKey] = (string) $attribute;
36: }
37: }
38:
39: //get child nodes from all namespaces
40: $tagsArray = array();
41: foreach ($namespaces as $prefix => $namespace) {
42: foreach ($xml->children($namespace) as $childXml) {
43: //recurse into child nodes
44: $childArray = self::toArray($childXml, $options);
45: $childTagName = key($childArray);
46: $childProperties = current($childArray);
47:
48: //replace characters in tag name
49: if ($options['keySearch']) $childTagName =
50: str_replace($options['keySearch'], $options['keyReplace'], $childTagName);
51: //add namespace prefix, if any
52: if ($prefix) $childTagName = $prefix . $options['namespaceSeparator'] . $childTagName;
53:
54: if (!isset($tagsArray[$childTagName])) {
55: //only entry with this key
56: //test if tags of this type should always be arrays, no matter the element count
57: $tagsArray[$childTagName] =
58: in_array($childTagName, $options['alwaysArray']) || !$options['autoArray']
59: ? array($childProperties) : $childProperties;
60: } elseif (
61: is_array($tagsArray[$childTagName]) && array_keys($tagsArray[$childTagName])
62: === range(0, count($tagsArray[$childTagName]) - 1)
63: ) {
64: //key already exists and is integer indexed array
65: $tagsArray[$childTagName][] = $childProperties;
66: } else {
67: //key exists so convert to integer indexed array with previous value in position 0
68: $tagsArray[$childTagName] = array($tagsArray[$childTagName], $childProperties);
69: }
70: }
71: }
72:
73: //get text content of node
74: $textContentArray = array();
75: $plainText = trim((string) $xml);
76: if ($plainText !== '') $textContentArray[$options['textContent']] = $plainText;
77:
78: //stick it all together
79: $propertiesArray = !$options['autoText'] || $attributesArray || $tagsArray || ($plainText === '')
80: ? array_merge($attributesArray, $tagsArray, $textContentArray) : $plainText;
81:
82: //return node as array
83: return [$xml->getName() => $propertiesArray];
84: }
85: }
86: