<' . $key . '>' . $input[$key]['@'] . ''); unset($input[$key]['@']); } else { $simpleXml = new SimpleXMLElement('<' . '?xml version="1.0"?' . '><' . $key . ' />'); } self::_fromArrayRecursive($simpleXml, $input[$key], $format); } else { $simpleXml = new SimpleXMLElement('<' . '?xml version="1.0"?' . '><' . $key . '>' . $input[$key] . ''); } return $simpleXml; } /** * Recursive method to create SimpleXMLElement from array * * @param object $node Handler to SimpleXMLElement * @param array $array * @param string $format * @return void */ protected static function _fromArrayRecursive(&$node, &$array, $format = 'attribute') { if (empty($array) || !is_array($array)) { return; } foreach ($array as $key => $value) { if (is_string($key)) { if (!is_array($value)) { if (is_bool($value)) { $value = (int)$value; } elseif ($value === null) { $value = ''; } if ($key[0] !== '@' && $format === 'tags') { $node->addChild($key, $value); } else { if ($key[0] === '@') { $key = substr($key, 1); } $node->addAttribute($key, $value); } } else { if ($key[0] === '@') { throw new Exception(__('Invalid array')); } if (array_keys($value) === range(0, count($value) - 1)) { // List foreach ($value as $item) { if (array_key_exists('@', $item)) { $child = $node->addChild($key, (string)$item['@']); unset($item['@']); } else { $child = $node->addChild($key); } self::_fromArrayRecursive($child, $item, $format); } } else { // Struct if (array_key_exists('@', $value)) { $child = $node->addChild($key, (string)$value['@']); unset($value['@']); } else { $child = $node->addChild($key); } self::_fromArrayRecursive($child, $value, $format); } } } else { throw new Exception(__('Invalid array')); } } } /** * Returns this XML structure as a array. * * @param object $simpleXML SimpleXMLElement instance * @return array Array representation of the XML structure. */ public static function toArray($simpleXML) { if (!($simpleXML instanceof SimpleXMLElement)) { throw new Exception(__('The input is not instance of SimpleXMLElement.')); } $result = array(); $namespaces = array_merge(array('' => ''), $simpleXML->getNamespaces(true)); self::_toArray($simpleXML, $result, array_keys($namespaces)); return $result; } /** * Recursive method to toArray * * @param object $xml SimpleXMLElement object * @param array $parentData Parent array with data * @param array $namespaces List of namespaces in XML * @return void */ protected static function _toArray($xml, &$parentData, $namespaces) { $data = array(); foreach ($namespaces as $namespace) { foreach ($xml->attributes($namespace, true) as $key => $value) { $data['@' . $key] = (string)$value; } foreach ($xml->children($namespace, true) as $child) { self::_toArray($child, $data, $namespaces); } } $asString = trim((string)$xml); if (empty($data)) { $data = $asString; } elseif (!empty($asString)) { $data['@'] = $asString; } $name = $xml->getName(); if (isset($parentData[$name])) { if (!is_array($parentData[$name]) || !isset($parentData[$name][0])) { $parentData[$name] = array($parentData[$name]); } $parentData[$name][] = $data; } else { $parentData[$name] = $data; } } }