/**
  * @param \DOMNode $node
  * @param $path
  * @return array|string
  */
 protected function toArrayTree(\DOMElement $node, $path)
 {
     $hasChildren = false;
     $record = [];
     $currentPath = $path . '/' . $node->nodeName;
     if ($node->hasChildNodes()) {
         foreach ($node->childNodes as $child) {
             if ($child instanceof \DOMElement) {
                 $hasChildren = true;
                 if (in_array($currentPath, $this->iterationPath) && in_array($child->nodeName, $this->iterationTag)) {
                     $record[$child->nodeName][] = $this->toArrayTree($child, $currentPath);
                 } else {
                     $record[$child->nodeName] = $this->toArrayTree($child, $currentPath);
                 }
             }
         }
     }
     if ($node->hasAttributes()) {
         $record["_attributes"] = [];
         foreach ($node->attributes as $attr) {
             $record["_attributes"][$attr->name] = $attr->value;
         }
         if (!$hasChildren) {
             $record["_value"] = $node->nodeValue;
         }
     } elseif (!$hasChildren) {
         $record = trim($node->nodeValue);
     }
     return $record;
 }
Ejemplo n.º 2
0
 /**
  * Convert a DOM node's properties into an array
  *
  * @param   DOMElement $node
  * @param   array      $data
  *
  * @return array
  */
 public static function domNodeAttributesToArray($node, $data = array())
 {
     if ($node->hasAttributes()) {
         foreach ($node->attributes as $attr) {
             $data[$attr->nodeName] = $attr->nodeValue;
         }
     }
     return $data;
 }
Ejemplo n.º 3
0
/**
 * converts a DOMElement to an array representation
 *
 * @param \DOMElement $root
 */
function dom_to_array($root)
{
    // if the node has only a single text node
    if (!$root->hasAttributes() && $root->childNodes->length == 1 && $root->childNodes->item(0)->nodeType == XML_TEXT_NODE) {
        return $root->childNodes->item(0)->nodeValue;
    }
    $result = array();
    if ($root->hasAttributes()) {
        $attrs = $root->attributes;
        foreach ($attrs as $i => $attr) {
            $result["_" . $attr->name] = $attr->value;
        }
    }
    $children = $root->childNodes;
    $group = array();
    $text = "";
    for ($i = 0; $i < $children->length; $i++) {
        $child = $children->item($i);
        if ($child->nodeType == XML_TEXT_NODE) {
            $text = $text . $child->nodeValue;
        } else {
            if (!isset($result[$child->nodeName])) {
                $result[$child->nodeName] = dom_to_array($child);
            } else {
                if (!isset($group[$child->nodeName])) {
                    $tmp = $result[$child->nodeName];
                    $result[$child->nodeName] = array($tmp);
                    $group[$child->nodeName] = 1;
                }
                $result[$child->nodeName][] = dom_to_array($child);
            }
        }
    }
    $trimmed = trim($text);
    if ($trimmed != "") {
        $result['#text'] = $text;
    }
    return $result;
}
Ejemplo n.º 4
0
 /**
  * Creates and return eZPageBlockItem object from given XML
  *
  * @static
  * @param DOMElement $node
  * @return eZPageBlockItem
  */
 public static function createFromXML(DOMElement $node)
 {
     $newObj = new eZPageBlockItem();
     if ($node->hasAttributes()) {
         foreach ($node->attributes as $attr) {
             $newObj->setAttribute($attr->name, $attr->value);
         }
     }
     foreach ($node->childNodes as $node) {
         if ($node->nodeType == XML_ELEMENT_NODE) {
             $newObj->setAttribute($node->nodeName, $node->nodeValue);
         }
     }
     return $newObj;
 }
 /**
  * Class EcasPhpCASParser
  * @param \DOMElement $root XML element coming from phpCAS
  * @return array Attributes
  * @see \phpCAS
  */
 public function parse(\DOMElement $root)
 {
     phpCAS::trace('Found attribute ' . $root->nodeName);
     $result = array();
     if ($root->hasAttributes()) {
         $attrs = $root->attributes;
         foreach ($attrs as $attr) {
             if ($attr->name == 'number') {
                 continue;
             }
             phpCAS::trace('Additional attribute ' . $attr->name . ' : ' . $attr->value);
             $result['@attributes'][$attr->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 = array();
         foreach ($children as $child) {
             $nodeName = str_replace('cas:', '', $child->nodeName);
             phpCAS::traceBegin();
             if ($nodeName == 'groups') {
                 $result['groups'] = array();
                 phpCas::traceBegin();
                 foreach ($child->childNodes as $groupChild) {
                     $result['groups'][] = $this->parse($groupChild);
                 }
                 phpCAS::traceEnd('Parsed groups');
             } elseif (!isset($result[$nodeName])) {
                 $result[$nodeName] = $this->parse($child);
             } else {
                 if (!isset($groups[$nodeName])) {
                     $result[$nodeName] = array($result[$nodeName]);
                     $groups[$nodeName] = 1;
                 }
                 $result[$nodeName][] = $this->parse($child);
             }
             phpCAS::traceEnd();
         }
     }
     return $result;
 }
Ejemplo n.º 6
0
 /**
  * Enlève les élements vide d'un noeud
  *
  * @param DOMElement $node DOMElement
  *
  * @return void
  */
 function purgeEmptyElementsNode($node)
 {
     // childNodes undefined for non-element nodes (eg text nodes)
     if ($node->childNodes) {
         // Copy childNodes array
         $childNodes = array();
         foreach ($node->childNodes as $childNode) {
             $childNodes[] = $childNode;
         }
         // Browse with the copy (recursive call)
         foreach ($childNodes as $childNode) {
             $this->purgeEmptyElementsNode($childNode);
         }
     }
     // Remove if empty
     if (!$node->hasChildNodes() && !$node->hasAttributes() && $node->nodeValue === "") {
         $node->parentNode->removeChild($node);
     }
 }
Ejemplo n.º 7
0
 /**
  * Creates and return eZPageZone object from given XML
  *
  * @static
  * @param DOMElement $node
  * @return eZPageZone
  */
 public static function createFromXML(DOMElement $node)
 {
     $newObj = new eZPageZone();
     if ($node->hasAttributes()) {
         foreach ($node->attributes as $attr) {
             if ($attr->name == 'id') {
                 $value = explode('_', $attr->value);
                 $newObj->setAttribute($attr->name, $value[1]);
             } else {
                 $newObj->setAttribute($attr->name, $attr->value);
             }
         }
     }
     foreach ($node->childNodes as $node) {
         if ($node->nodeType == XML_ELEMENT_NODE && $node->nodeName == 'block') {
             $blockNode = eZPageBlock::createFromXML($node);
             $newObj->addBlock($blockNode);
         } elseif ($node->nodeType == XML_ELEMENT_NODE) {
             $newObj->setAttribute($node->nodeName, $node->nodeValue);
         }
     }
     return $newObj;
 }
Ejemplo n.º 8
0
    /**
     * Creates and return eZPageBlock object from given XML
     *
     * @static
     * @param DOMElement $node
     * @return eZPageBlock
     */
    public static function createFromXML( DOMElement $node )
    {
        $newObj = new eZPageBlock();

        if ( $node->hasAttributes() )
        {
            foreach ( $node->attributes as $attr )
            {
                if ( $attr->name == 'id' )
                {
                    $value = explode( '_', $attr->value );
                    $newObj->setAttribute( $attr->name, $value[1] );
                }
                else
                {
                    $newObj->setAttribute( $attr->name, $attr->value );
                }
            }
        }

        foreach ( $node->childNodes as $node )
        {
            if ( $node->nodeType == XML_ELEMENT_NODE && $node->nodeName == 'item' )
            {
                $blockItemNode = eZPageBlockItem::createFromXML( $node );
                $newObj->addItem( $blockItemNode );
            }
            elseif ( $node->nodeType == XML_ELEMENT_NODE && $node->nodeName == 'rotation' )
            {
                $attrValue = array();

                foreach ( $node->childNodes as $subNode )
                {
                    if ( $subNode->nodeType == XML_ELEMENT_NODE )
                        $attrValue[$subNode->nodeName] = $subNode->nodeValue;
                }

                $newObj->setAttribute( $node->nodeName, $attrValue );
            }
            elseif ( $node->nodeType == XML_ELEMENT_NODE && $node->nodeName == 'custom_attributes' )
            {
                $attrValue = array();

                foreach ( $node->childNodes as $subNode )
                {
                    if ( $subNode->nodeType == XML_ELEMENT_NODE )
                        $attrValue[$subNode->nodeName] = $subNode->nodeValue;
                }

                $newObj->setAttribute( $node->nodeName, $attrValue );
            }
            else
            {
                if ( $node->nodeType == XML_ELEMENT_NODE )
                    $newObj->setAttribute( $node->nodeName, $node->nodeValue );
            }
        }

        return $newObj;
    }
 private function nodeNotEmpty(\DOMElement $element)
 {
     return $element->hasChildNodes() || $element->hasAttributes();
 }
Ejemplo n.º 10
0
 /**
  * 获得节点的属性
  * 
  * 该属性将不包含属性为name的值--规则(name的值将作为解析后数组的key值索引存在)
  * 
  * @param DOMElement $node 节点
  * @return array 返回属性数组
  */
 public function getAttributes($node)
 {
     if (!$node instanceof DOMElement || !$node->hasAttributes()) {
         return array();
     }
     $attributes = array();
     foreach ($node->attributes as $attribute) {
         if (self::NAME != $attribute->nodeName) {
             $value = (string) $attribute->nodeValue;
             $__tmp = strtolower($value);
             $attributes[$attribute->nodeName] = 'false' === $__tmp ? false : ('true' === $__tmp ? true : $value);
         }
     }
     return $attributes;
 }
Ejemplo n.º 11
0
 private function nodeAttr(\DOMElement $node)
 {
     $attributes = array();
     if ($node->hasAttributes()) {
         foreach ($node->attributes as $attr) {
             $attributes[strtolower($attr->nodeName)] = $attr->nodeValue;
         }
     }
     return $attributes;
 }
Ejemplo n.º 12
0
 /**
  * Parses the XML element $node and creates a feed element in the current
  * module with name $name.
  *
  * @param string $name The name of the element belonging to the module
  * @param DOMElement $node The XML child from which to take the values for $name
  */
 public function parse($name, DOMElement $node)
 {
     if ($this->isElementAllowed($name)) {
         $element = $this->add($name);
         $value = $node->textContent;
         switch ($name) {
             case 'date':
                 $element->date = $value;
                 break;
             case 'contributor':
             case 'creator':
             case 'publisher':
                 $element->name = $value;
                 break;
             case 'identifier':
                 $element->id = $value;
                 break;
             case 'source':
                 $element->source = $value;
                 break;
             default:
                 $element->text = $value;
         }
         if ($node->hasAttributes()) {
             foreach ($node->attributes as $attribute) {
                 switch ($attribute->name) {
                     case 'lang':
                         $element->language = $attribute->value;
                         break;
                 }
             }
         }
     }
 }
Ejemplo n.º 13
0
 /**
  * Misc function that maps the node's attributes to a key => value array
  * and results any expressions to actual values
  *
  * @param DOMElement $node
  * @return array
  */
 private function nodeAttributesToScope(DOMElement &$node)
 {
     $myContext = array();
     if ($node->hasAttributes()) {
         foreach ($node->attributes as $attr) {
             if (strpos($attr->value, '${') !== false) {
                 // attribute value contains an expression
                 $expressions = array();
                 preg_match_all('/(\\$\\{)(.*)(\\})/imsxU', $attr->value, $expressions);
                 for ($i = 0; $i < count($expressions[0]); $i++) {
                     $expression = $expressions[2][$i];
                     $myContext[$attr->name] = ExpressionParser::evaluate($expression, $this->dataContext);
                 }
             } else {
                 // plain old string
                 $myContext[$attr->name] = trim($attr->value);
             }
         }
     }
     return $myContext;
 }
Ejemplo n.º 14
0
 /**
  * removes all children of element except of key elements, which has to remain
  *
  * @param      \DOMElement  $domNode
  * @param      \DOMNodeList $domNodeChildren
  * @param bool              $leaveKey
  * @param bool              $recursive
  *
  * @return int  number of key elements, that remains
  */
 public function removeChildrenExceptOfKeyElements($domNode, $domNodeChildren, $leaveKey = false, $recursive = false)
 {
     $keyElemIndex = $keyElemsCnt = 0;
     while ($domNodeChildren->length > $keyElemIndex) {
         $isKey = $isCreated = false;
         $child = $domNodeChildren->item($keyElemIndex);
         if ($child->hasAttributes()) {
             foreach ($child->attributes as $attr) {
                 if ($attr->nodeName == "iskey" && $attr->nodeValue == "true") {
                     if ($child->hasAttributes()) {
                         foreach ($child->attributes as $attr) {
                             if ($attr->nodeName !== 'xc:operation') {
                                 $attributesArr[] = $attr->nodeName;
                             }
                         }
                         // remove must be in new foreach, previous deletes only first one
                         foreach ($attributesArr as $attrName) {
                             $child->removeAttribute($attrName);
                         }
                     }
                     if ($leaveKey == true) {
                         $keyElemIndex++;
                         $isKey = true;
                     } else {
                         if (isset($child)) {
                             $nodeName = $child->nodeName;
                             $nodeValue = $child->nodeValue;
                             $domNode->setAttribute("GUIcustom_" . $nodeName, $nodeValue);
                         }
                     }
                     $keyElemsCnt++;
                 } elseif ($attr->nodeName == "xc:operation" && $attr->nodeValue == "create") {
                     $keyElemIndex++;
                     $isCreated = true;
                 }
             }
         }
         if (!$isKey && !$isCreated || $leaveKey == false) {
             try {
                 $childrenRemains = 0;
                 // recursively check all children for their key elements
                 if (sizeof($child->childNodes) && $recursive) {
                     foreach ($child->childNodes as $chnode) {
                         if (sizeof($chnode->childNodes)) {
                             $childrenRemains += $this->removeChildrenExceptOfKeyElements($chnode, $chnode->childNodes, $leaveKey, $recursive);
                         }
                     }
                 }
                 if ($childrenRemains == 0) {
                     $domNode->removeChild($child);
                 } else {
                     $keyElemIndex++;
                 }
             } catch (\DOMException $e) {
             }
         }
     }
     if ($domNode->hasAttributes()) {
         foreach ($domNode->attributes as $attr) {
             if (strpos($attr->nodeName, "GUIcustom_") !== 0) {
                 $attributesArr[] = $attr->nodeName;
             }
         }
         // remove must be in new foreach, previous deletes only first one
         foreach ($attributesArr as $attrName) {
             $domNode->removeAttribute($attrName);
         }
     }
     return $keyElemsCnt;
 }
Ejemplo n.º 15
0
 /**
  * Writes beginning pair tag.
  *
  * @param XMLWriter $writer
  *   Writer that writes the output.
  * @param DOMElement $node
  *   Current node.
  * @param $id
  *   Current node id.
  */
 protected function writeBPT(\XMLWriter $writer, \DOMElement $node, $id)
 {
     $beginning_tag = '<' . $node->nodeName;
     if ($node->hasAttributes()) {
         $attributes = array();
         /** @var DOMAttr $attribute */
         foreach ($node->attributes as $attribute) {
             $attributes[] = $attribute->name . '="' . $attribute->value . '"';
         }
         $beginning_tag .= ' ' . implode(' ', $attributes);
     }
     $beginning_tag .= '>';
     $writer->startElement('bpt');
     $writer->writeAttribute('id', $id);
     $writer->text($beginning_tag);
     $writer->endElement();
 }
Ejemplo n.º 16
0
 /**
  * Creates a new ezcsseRule object from given XML node
  * 
  * @param DOMElement $node
  * @return ezcsseRule
  */
 static function createFromXML(DOMElement $node)
 {
     $newObj = new ezcsseRule();
     if ($node->hasAttributes()) {
         foreach ($node->attributes as $attr) {
             $newObj->setAttribute($attr->name, $attr->value);
         }
     }
     foreach ($node->childNodes as $node) {
         if ($node->nodeType == XML_ELEMENT_NODE && $node->nodeName == 'property') {
             $newObj->addProperty(ezcsseProperty::createFromXML($node));
         } elseif ($node->nodeType == XML_ELEMENT_NODE) {
             $newObj->setAttribute($node->nodeName, $node->nodeValue);
         }
     }
     return $newObj;
 }
Ejemplo n.º 17
0
 /**
  *
  * @param DOMElement $element
  * @param string $name
  * @param bool $throwIfNotExists
  * @param mixed $default
  * @return string
  */
 private function getAttribute(DOMElement $element, $name, $throwIfNotExists = true, $default = null)
 {
     $node = null;
     if (!$element->hasAttributes() || null === ($node = $element->attributes->getNamedItem($name))) {
         if ($throwIfNotExists) {
             throw new DOMException("Attribute with '{$name}' not found in " . $this->nodeName . " node");
         } else {
             return $default;
         }
     }
     return $node->nodeValue;
 }
Ejemplo n.º 18
0
 /**
  * @param DOMElement $root
  * @param bool $eagerAttributes
  * @return array|string
  */
 function ifw_xml_to_array(DOMElement $root, $eagerAttributes = false)
 {
     $result = array();
     if ($root->hasAttributes()) {
         $attrs = $root->attributes;
         foreach ($attrs as $attr) {
             $result['@attributes'][$attr->name] = $attr->value;
         }
     }
     if ($root->hasChildNodes()) {
         $children = $root->childNodes;
         if ($children->length == 1) {
             $child = $children->item(0);
             if ($child->nodeType == XML_TEXT_NODE) {
                 if (!$eagerAttributes) {
                     $result = $child->nodeValue;
                     return $result;
                 } else {
                     $result['_value'] = $child->nodeValue;
                     return count($result) == 1 ? $result['_value'] : $result;
                 }
             }
         }
         $groups = array();
         foreach ($children as $child) {
             if (!isset($result[$child->nodeName])) {
                 $result[$child->nodeName] = ifw_xml_to_array($child);
             } else {
                 if (!isset($groups[$child->nodeName])) {
                     $result[$child->nodeName] = array($result[$child->nodeName]);
                     $groups[$child->nodeName] = 1;
                 }
                 $result[$child->nodeName][] = ifw_xml_to_array($child);
             }
         }
     }
     return $result;
 }
 /**
  * Converts DOMElement object into text representation
  *
  * @param DOMElement $ele
  * @param boolean $allowEmpty
  * @return string 
  */
 public static function getNodeAsString(DOMElement $ele, $allowEmpty = TRUE)
 {
     $nStr = '<' . $ele->nodeName;
     if ($ele->hasAttributes()) {
         $attrs = $ele->attributes;
         foreach ($attrs as $attr) {
             $nStr = $nStr . ' ' . $attr->nodeName . '="' . $attr->nodeValue . '"';
         }
         $nStr = $nStr . ($allowEmpty ? '/>' : '></' . $ele->tagName . '>');
     }
     return $nStr;
 }