/** * Convert an XML document to a multi dimensional array * Pass in an XML document (or SimpleXMLElement object) and this recrusively loops through and builds a representative array * * @param string $xml - XML document - can optionally be a SimpleXMLElement object * @return array ARRAY */ public static function toArray($xml) { if (is_string($xml)) { $xml = new SimpleXMLElement($xml); } $children = $xml->children(); if (!$children) { return (string) $xml; } $arr = array(); foreach ($children as $key => $node) { $node = ArrayToXML::toArray($node); // support for 'anon' non-associative arrays if ($key == 'anon') { $key = count($arr); } // if the node is already set, put it into an array if (isset($arr[$key])) { if (!is_array($arr[$key]) || !isset($arr[$key][0])) { $arr[$key] = array($arr[$key]); } $arr[$key][] = $node; } else { $arr[$key] = $node; } } return $arr; }
/** * @author giorgio 25/set/2013 * * renders the widgets of the page as described in the passed xml config file * * @param string $widgetsConfFilename xml configuration filename for the widgets * @param arrayn $optionsArray array of option to be passed to the widget loader * * @return array|AMA_Error */ public function fillin_widgetsFN($widgetsConfFilename = '', $optionsArray = array()) { require_once ROOT_DIR . '/widgets/include/widget_includes.inc.php'; if (is_file($widgetsConfFilename)) { try { $widgetAr = ArrayToXML::toArray(file_get_contents($widgetsConfFilename)); } catch (Exception $e) { /* * see config_errors.inc.php line 167 and following. * depending on the erorr phase / severity something will happen... */ return new ADA_Error(NULL, 'Widget configuration XML is not valid', __METHOD__, ADA_ERROR_ID_XML_PARSING); } } /** * @author giorgio 25/feb/2014 * ArrayToXML::toArray does not return an array of array if there's * only one widget in the xml. Let's build an array of array even in this case. */ if (!is_array(reset($widgetAr['widget']))) { $widgets = array($widgetAr['widget']); } else { $widgets = $widgetAr['widget']; } $retArray = array(); foreach ($widgets as $widget) { // if widget is not active skip the current iteration if (isset($widget['active']) && intval($widget['active']) === 0 || isset($widget[$widget['id']]) && intval($widget[$widget['id']['isActive']]) === 0) { continue; } $wobj = new Widget($widget); /** * if there are some params passed in, tell it to the widget */ if (isset($optionsArray[$wobj->templateField]) && !empty($optionsArray[$wobj->templateField])) { foreach ($optionsArray[$wobj->templateField] as $name => $value) { $wobj->setParam($name, $value); } } $retArray[$wobj->templateField] = $wobj->getWidget(); } return $retArray; }
/** * The main function for converting to an array. * Pass in a XML document and this recrusively loops through and builds up an array. * * @static * @param string $obj - XML document string (at start point) * @param array $arr - Array to generate * @return array - Array generated */ public static function toArray($obj, &$arr = NULL) { if (is_null($arr)) { $arr = array(); } if (is_string($obj)) { $obj = new SimpleXMLElement($obj); } // Get attributes for current node and add to current array element $attributes = $obj->attributes(); foreach ($attributes as $attrib => $value) { $arr[ArrayToXML::attr_arr_string][$attrib] = (string) $value; } $children = $obj->children(); $executed = FALSE; // Check all children of node foreach ($children as $elementName => $node) { // Check if there are multiple node with the same key and generate a multiarray if (array_key_exists($elementName, $arr) && $arr[$elementName] != NULL) { if (isset($arr[$elementName][0]) && $arr[$elementName][0] !== NULL) { $i = count($arr[$elementName]); ArrayToXML::toArray($node, $arr[$elementName][$i]); } else { $tmp = $arr[$elementName]; $arr[$elementName] = array(); $arr[$elementName][0] = $tmp; $i = count($arr[$elementName]); ArrayToXML::toArray($node, $arr[$elementName][$i]); } } else { $arr[$elementName] = array(); ArrayToXML::toArray($node, $arr[$elementName]); } $executed = TRUE; } // Check if is already processed and if already contains attributes if (!$executed && $children->getName() == "" && !isset($arr[ArrayToXML::attr_arr_string])) { $arr = (string) $obj; } return $arr; }