Exemplo n.º 1
0
 /**
  * Converts a simpleXML element into an array. Preserves attributes and everything.
  * You can choose to get your elements either flattened, or stored in a custom index that
  * you define.
  * For example, for a given element
  * <field name="someName" type="someType"/>
  * if you choose to flatten attributes, you would get:
  * $array['field']['name'] = 'someName';
  * $array['field']['type'] = 'someType';
  * If you choose not to flatten, you get:
  * $array['field']['@attributes']['name'] = 'someName';
  * _____________________________________
  * Repeating fields are stored in indexed arrays. so for a markup such as:
  * <parent>
  * <child>a</child>
  * <child>b</child>
  * <child>c</child>
  * </parent>
  * you array would be:
  * $array['parent']['child'][0] = 'a';
  * $array['parent']['child'][1] = 'b';
  * ...And so on.
  * _____________________________________
  * @param simpleXMLElement $xml the XML to convert
  * @param bool $flattenValues    Choose wether to flatten values
  *                                    or to set them under a particular index.
  *                                    defaults to true;
  * @param bool $flattenAttributes Choose wether to flatten attributes
  *                                    or to set them under a particular index.
  *                                    Defaults to true;
  * @param bool $flattenChildren    Choose wether to flatten children
  *                                    or to set them under a particular index.
  *                                    Defaults to true;
  * @param string $valueKey            index for values, in case $flattenValues was set to
  *                            false. Defaults to "@value"
  * @param string $attributesKey        index for attributes, in case $flattenAttributes was set to
  *                            false. Defaults to "@attributes"
  * @param string $childrenKey        index for children, in case $flattenChildren was set to
  *                            false. Defaults to "@children"
  * @return array the resulting array.
  */
 public static function simpleXMLToArray($xml, $flattenValues = true, $flattenAttributes = true, $flattenChildren = true, $valueKey = '@value', $attributesKey = '@attributes', $childrenKey = '@children')
 {
     $return = array();
     if (!$xml instanceof SimpleXMLElement) {
         return $return;
     }
     $name = $xml->getName();
     $_value = trim((string) $xml);
     if (strlen($_value) == 0) {
         $_value = null;
     }
     if ($_value !== null) {
         if (!$flattenValues) {
             $return[$valueKey] = $_value;
         } else {
             $return = $_value;
         }
     }
     $children = array();
     $first = true;
     foreach ($xml->children() as $elementName => $child) {
         $value = ToolsInstall::simpleXMLToArray($child, $flattenValues, $flattenAttributes, $flattenChildren, $valueKey, $attributesKey, $childrenKey);
         if (isset($children[$elementName])) {
             if ($first) {
                 $temp = $children[$elementName];
                 unset($children[$elementName]);
                 $children[$elementName][] = $temp;
                 $first = false;
             }
             $children[$elementName][] = $value;
         } else {
             $children[$elementName] = $value;
         }
     }
     if (count($children) > 0) {
         if (!$flattenChildren) {
             $return[$childrenKey] = $children;
         } else {
             $return = array_merge($return, $children);
         }
     }
     $attributes = array();
     foreach ($xml->attributes() as $name => $value) {
         $attributes[$name] = trim($value);
     }
     if (count($attributes) > 0) {
         if (!$flattenAttributes) {
             $return[$attributesKey] = $attributes;
         } else {
             $return = array_merge($return, $attributes);
         }
     }
     return $return;
 }
Exemplo n.º 2
0
// - include : variable $result available after inclusion
// 3) file_get_contents()
// - eval : $res = eval(file_get_contents());
if (empty($return_type) || $return_type == 'xml') {
    header('Content-Type: text/xml');
    echo $result;
} else {
    // result in xml to array
    $result = simplexml_load_string($result);
    if (!class_exists('ToolsInstall', false)) {
        if (file_exists(_PS_INSTALL_PATH_ . '/upgrade/classes/ToolsInstall.php')) {
            include_once _PS_INSTALL_PATH_ . '/upgrade/classes/ToolsInstall.php';
        }
    }
    if (class_exists('ToolsInstall', false)) {
        $result = ToolsInstall::simpleXMLToArray($result);
        switch ($return_type) {
            case 'json':
                header('Content-Type: application/json');
                if (function_exists('json_encode')) {
                    $result = json_encode($result);
                } else {
                    include_once INSTALL_PATH . '/../tools/json/json.php';
                    $pearJson = new Services_JSON();
                    $result = $pearJson->encode($result);
                }
                echo $result;
                break;
            case 'eval':
                return $result;
            case 'include':