/** * convert xml to array * @param SimpleXMLElement $xml * @param $singular_tag * @param $plural_tag * @return array */ public static function xml2array($xml, $singular_tag = 'param', $plural_tag = 'params') { $arr = array(); if ($xml instanceof SimpleXMLElement) { foreach ($xml as $element) { $tag = $element->getName(); if ($tag !== $singular_tag && $tag !== $plural_tag) { continue; } $atts = $element->attributes(); $key = isset($atts['name']) ? (string) $atts['name'] : ''; $val = isset($atts['value']) ? (string) $atts['value'] : HW_String::truncate_empty_lines((string) $element); if (isset($atts['type']) && $atts['type'] == 'bool') { $val = $val ? true : false; } $e = get_object_vars($element); if (!empty($e)) { $val = count($element->children()) && $tag == $plural_tag ? self::xml2array($element) : $val; if ($key !== '') { $arr[$key] = $val; } else { $arr[] = $val; } } else { if ($key !== '') { $arr[$key] = trim($element); } else { $arr[] = trim($element); } } } } return $arr; }
/** * get params * extend from HW_XML::xml2array * @param $dom start from this dom * @return SimpleXMLElement[] */ public function extract_params($dom = null) { #if(empty($this->xml)) return; $data = array(); //if(!is_object($dom) && isset($this->xml->params)) $dom = $this->xml->params; #if(!is_object($dom) && isset($this->xml->params)) $dom = $this->xml->params; //validation #if(!is_object($dom) || !$dom instanceof SimpleXMLElement) return ; /*if(!empty($dom)) { foreach ($dom->children() as $param) { $value = (string) $param['value']; //valid field type if(isset($param['type'])) { if($param['type'] == 'bool') $value = ($value!=='false')? true : false; } $data[(string)$param['name']] = $value; } } return $data;*/ $arr = array(); foreach ($dom as $element) { $tag = $element->getName(); if ($tag !== 'param' && $tag !== 'params') { continue; } $atts = $element->attributes(); $key = isset($atts['name']) ? (string) $atts['name'] : ''; $val = isset($atts['value']) ? (string) $atts['value'] : HW_String::truncate_empty_lines((string) $element); if (isset($atts['type']) && $atts['type'] == 'bool') { $val = $val ? true : false; } $e = get_object_vars($element); if (!empty($e)) { $val = count($element->children()) && $tag == 'params' ? $this->extract_params($element) : $val; if ($key !== '') { $arr[$key] = $val; } else { $arr[] = $val; } } else { if ($key !== '') { $arr[$key] = trim($element); } else { $arr[] = trim($element); } } } return $arr; }
/** * @param $xml * @param $prefix * @param $count * @return array */ public function recursive_option_data($xml, $prefix = '', $count = 0) { //return HW_XML::xml2array($item, 'param', 'params'); $method = 'override'; $attributes = null; $arr = array(); $i_results = array(); foreach ($xml as $id => $element) { $tag = $element->getName(); if ($tag !== 'param' && $tag !== 'params') { continue; } $_recursive = true; $val = null; $skin = $parse = null; $atts = $element->attributes(); $ori_key = $key = isset($atts['name']) ? (string) $atts['name'] : ''; $encoded = isset($atts['encoded']) ? (string) $atts['encoded'] : 0; $export = isset($atts['export']) ? (string) $atts['export'] : ''; if ($count++ == 0) { if (isset($atts['method'])) { $method = (string) $atts['method']; } //tell how to append data if (isset($atts['prefix'])) { $prefix = (string) $atts['prefix']; //prefix option name } $attributes = $atts; } if ($count != 1 && $prefix !== '') { #$key = $prefix.$key; } //for skin data, because not count for xpath namespace element #if(HW_XML::count_childs($element) /*|| count($element->children('params'))*/ ) { //$skin = $this->build_skin_data($element); #} $import_result = HWIE_Module_Import_Results::valid($element); if ($import_result) { $val = $parse = new HWIE_Module_Import_Results(dom_import_simplexml($import_result)); //$parse->init($this->parser->importer); //$val = $parse->parse_data()->value; //$this->import_results[] = &$val; //$i_results[] = &$val; } else { $val = $skin = $this->build_skin_data($element); } //get value /*if(!empty($skin)) { $val = $skin; }*/ if ($val === null) { $val = isset($atts['value']) ? (string) $atts['value'] : (!count($element->children()) ? HW_String::truncate_empty_lines((string) $element) : null); } if ($val === null && !count($element->children()) && $tag == 'params') { $val = array(); } if ($export && isset($val[$export])) { $val = $val[$export]; //used for build_skin_data & other special param } if ($encoded) { //for single param $val = base64_encode(serialize($val)); } $e = get_object_vars($element); if (!empty($e)) { if (HW_XML::count_childs($element) && $_recursive && (empty($skin) && empty($parse))) { $recursive = $this->recursive_option_data($element, $prefix, $count); //join data option if (is_array($recursive->option) && isset($atts->join)) { $recursive->option = join((string) $atts->join, $recursive->option); } if ($key !== '') { $arr[$key] =& $recursive->option; } else { $arr[] =& $recursive->option; } $i_results = array_merge($i_results, $recursive->import_results); #$i_results[0]='XY'; //list($_key, $_value) = end($arr); if ($key) { $_key = $key; } else { end($arr); $_key = key($arr); } $_value = $arr[$_key]; reset($arr); //make sure you reset array data if (is_array($_value) && isset($_value[$export])) { $arr[$_key] = $_value[$export]; $i_results[$_key] =& $arr[$_key]; } if ($encoded) { //encoded whole params has sub $arr[$_key] = base64_encode(serialize($_value)); $i_results[$_key] =& $arr[$_key]; #$arr = base64_encode(serialize($_value)); //don't because if exists more element around it } } else { if ($key !== '') { if ($val instanceof HWIE_Module_Import_Results) { $arr[$key] =& $val; $i_results[$key] =& $val; } else { $arr[$key] = $val; } } else { if ($val instanceof HWIE_Module_Import_Results) { $arr[] =& $val; $i_results[] =& $val; } else { $arr[] = $val; } } } if ($skin) { /*$_skin=$element->xpath('params:skin'); $node = dom_import_simplexml($element); $node->parentNode->removeChild($node);*/ $this->recursive_option_data($element, $prefix, $count); } } else { if ($key !== '') { $arr[$key] = trim($element); } else { $arr[] = trim($element); } } } $result = (object) array('import_results' => &$i_results, 'option' => &$arr, 'method' => $method, 'prefix' => $prefix, 'attributes' => $attributes['@attributes']); return $result; }