| 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' => ':', |
| 13: | 'attributePrefix' => '@', |
| 14: | 'alwaysArray' => array(), |
| 15: | 'autoArray' => true, |
| 16: | 'textContent' => '$', |
| 17: | 'autoText' => true, |
| 18: | 'keySearch' => false, |
| 19: | 'keyReplace' => false |
| 20: | ); |
| 21: | $options = array_merge($defaults, $options); |
| 22: | $namespaces = $xml->getDocNamespaces(); |
| 23: | $namespaces[''] = null; |
| 24: | |
| 25: | |
| 26: | $attributesArray = array(); |
| 27: | foreach ($namespaces as $prefix => $namespace) { |
| 28: | foreach ($xml->attributes($namespace) as $attributeName => $attribute) { |
| 29: | |
| 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: | |
| 40: | $tagsArray = array(); |
| 41: | foreach ($namespaces as $prefix => $namespace) { |
| 42: | foreach ($xml->children($namespace) as $childXml) { |
| 43: | |
| 44: | $childArray = self::toArray($childXml, $options); |
| 45: | list($childTagName, $childProperties) = each($childArray); |
| 46: | |
| 47: | |
| 48: | if ($options['keySearch']) $childTagName = |
| 49: | str_replace($options['keySearch'], $options['keyReplace'], $childTagName); |
| 50: | |
| 51: | if ($prefix) $childTagName = $prefix . $options['namespaceSeparator'] . $childTagName; |
| 52: | |
| 53: | if (!isset($tagsArray[$childTagName])) { |
| 54: | |
| 55: | |
| 56: | $tagsArray[$childTagName] = |
| 57: | in_array($childTagName, $options['alwaysArray']) || !$options['autoArray'] |
| 58: | ? array($childProperties) : $childProperties; |
| 59: | } elseif ( |
| 60: | is_array($tagsArray[$childTagName]) && array_keys($tagsArray[$childTagName]) |
| 61: | === range(0, count($tagsArray[$childTagName]) - 1) |
| 62: | ) { |
| 63: | |
| 64: | $tagsArray[$childTagName][] = $childProperties; |
| 65: | } else { |
| 66: | |
| 67: | $tagsArray[$childTagName] = array($tagsArray[$childTagName], $childProperties); |
| 68: | } |
| 69: | } |
| 70: | } |
| 71: | |
| 72: | |
| 73: | $textContentArray = array(); |
| 74: | $plainText = trim((string) $xml); |
| 75: | if ($plainText !== '') $textContentArray[$options['textContent']] = $plainText; |
| 76: | |
| 77: | |
| 78: | $propertiesArray = !$options['autoText'] || $attributesArray || $tagsArray || ($plainText === '') |
| 79: | ? array_merge($attributesArray, $tagsArray, $textContentArray) : $plainText; |
| 80: | |
| 81: | |
| 82: | return [$xml->getName() => $propertiesArray]; |
| 83: | } |
| 84: | } |
| 85: | |