/** * Helper method to retrieve simplexml attributes * @param string $ns * @param boolean $is_prefix * @return array */ function attributes($ns = null, $is_prefix = null) { $attr = (array) parent::attributes($ns, $is_prefix); if (isset($attr['@attributes'])) { return $attr['@attributes']; } return []; }
/** * Recursively convert \SimpleXMLIterator to array * * @param \SimpleXMLIterator $XMLNode * * @return array */ private static function _XMLNodeToArray($XMLNode) { $result = array(); $attributes = $XMLNode->attributes(); foreach ($attributes as $k => $v) { $val = (string) $v; if ($val == "True" || $val == "False") { $val = (bool) $val; } $result[$k] = $val; } $children = $XMLNode->children(); foreach ($children as $chK => $chNode) { $result['Items'][] = self::_XMLNodeToArray($chNode); } return $result; }
/** * @param \SimpleXMLIterator $element * @param \Twig_Template $template * * @param string $formId * @param string $xPath * @param string $requiredChildren * @param array $identityRefs * * @return array|bool */ public function getChildrenValues($element, $template, $formId, $xPath = "", $requiredChildren = "", $identityRefs = array()) { $retArr = array(); $targetAttributes = array('key', 'iskey', 'mandatory'); foreach ($element as $label => $el) { $attributesArr = array_fill_keys($targetAttributes, false); foreach ($element->attributes() as $name => $attr) { if ($name == "key") { $attributesArr[$name] = (string) $attr[0]; } } foreach ($el->attributes() as $name => $attr) { if (in_array($name, array('iskey', 'mandatory'))) { $attributesArr[$name] = (string) $attr[0]; } } if (($attributesArr['iskey'] !== "true" && $attributesArr['key'] == false || $requiredChildren !== "" && $label != $requiredChildren) && $attributesArr['mandatory'] == false) { continue; } if ($attributesArr['key'] !== false) { $requiredChildren = $attributesArr['key']; } else { $requiredChildren = ""; } $twigArr = array(); $twigArr['key'] = ""; $twigArr['xpath'] = ""; $twigArr['element'] = $el; $twigArr['useHiddenInput'] = true; $twigArr['moduleIdentityRefs'] = $identityRefs; $newXPath = $xPath . "/*"; $res = $this->getAvailableLabelValuesForXPath($formId, $newXPath); $retArr[$label] = array(); if (isset($res['labelsAttributes'][$label])) { $retArr[$label]['labelAttributes'] = $res['labelsAttributes'][$label]; } $retArr[$label]['valueElem'] = $this->removeMultipleWhitespaces($template->renderBlock('configInputElem', $twigArr)); $retArr[$label]['children'] = $this->getChildrenValues($el, $template, $formId, $newXPath, $requiredChildren); } return sizeof($retArr) ? $retArr : false; }
/** * This class method is used to construct a ResourceDescriptor object by providing the XML for that object. * * @param string $xml - XML String defining ResourceDescriptor * @return ResourceDescriptor */ public static function createFromXML($xml) { $sxi = new \SimpleXMLIterator($xml); $temp = new self((string) $sxi->attributes()->name, (string) $sxi->attributes()->wsType, (string) $sxi->attributes()->uriString, (string) $sxi->attributes()->isNew); $desc = !empty($sxi->description) ? (string) $sxi->description : null; $label = !empty($sxi->label) ? (string) $sxi->label : null; $date = !empty($sxi->creationDate) ? (string) $sxi->creationDate : null; $temp->setLabel($label); $temp->setDescription($desc); $temp->setCreationDate($date); foreach ($sxi->resourceDescriptor as $nestRd) { $temp->children[] = ResourceDescriptor::createFromXML($nestRd->asXML()); } foreach ($sxi->resourceProperty as $prop) { $temp->properties[] = ResourceProperty::createFromXML($prop->asXML()); } return $temp; }
/** * Create an Array from XML * * This method sets up the SimpleXMLIterator and starts the parsing * of an xml body to iterate through it and transform it into * an array that can be used by the developers. * * @param string $xml * @return array An array mapped to the passed xml */ public static function arrayFromXml($xml) { // replace namespace defs $xml = str_replace('xmlns=', 'ns=', $xml); // catch libxml errors libxml_use_internal_errors(true); try { $iterator = new SimpleXMLIterator($xml); } catch(Exception $e) { $xmlErrors = libxml_get_errors(); return new Frapi_Exception( 'Xml Parsing Failed', 'INVALID_XML', 400, 'xml_parsing' ); libxml_clear_errors(); } $xmlRoot = $iterator->getName(); $type = $iterator->attributes()->type; // SimpleXML provides the root information on construct self::$_xmlRoot = $iterator->getName(); self::$_responseType = $type; // return the mapped array with the root element as the header return array($xmlRoot => self::_iteratorToArray($iterator)); }
/** * sets up the SimpleXMLIterator and starts the parsing * @access public * @param string $xml * @return array array mapped to the passed xml */ public static function arrayFromXml($xml) { // SimpleXML provides the root information on construct $iterator = new SimpleXMLIterator($xml); $xmlRoot = Braintree_Util::delimiterToCamelCase($iterator->getName()); $type = $iterator->attributes()->type; self::$_xmlRoot = $iterator->getName(); self::$_responseType = $type; // return the mapped array with the root element as the header return array($xmlRoot => self::_iteratorToArray($iterator)); }