Ejemplo n.º 1
0
 /**
  * From http://stackoverflow.com/questions/14553547/what-is-the-best-php-dom-2-array-function
  *
  * @param \DomDocument $root
  * @return array
  */
 protected function domToArray($root)
 {
     $result = [];
     if ($root->hasAttributes()) {
         $attrs = $root->attributes;
         foreach ($attrs as $attr) {
             $name = str_replace('cas:', '', $attr->name);
             $result['attributes'][$name] = $attr->value;
         }
     }
     if ($root->hasChildNodes()) {
         $children = $root->childNodes;
         if ($children->length == 1) {
             $child = $children->item(0);
             if ($child->nodeType == XML_TEXT_NODE) {
                 $result['value'] = $child->nodeValue;
                 return count($result) == 1 ? $result['value'] : $result;
             }
         }
         $groups = [];
         foreach ($children as $child) {
             $childName = str_replace('cas:', '', $child->nodeName);
             if (!isset($result[$childName])) {
                 $result[$childName] = $this->domToArray($child);
             } else {
                 if (!isset($groups[$childName])) {
                     $result[$childName] = [$result[$childName]];
                     $groups[$childName] = 1;
                 }
                 $result[$childName][] = $this->domToArray($child);
             }
         }
     }
     return $result;
 }