Horde_Xml_Element::fromArray PHP Method

fromArray() public method

ElementName -> <$ElementName> will be appended with a value of $value #AttributeName -> An attribute $AttributeName will be added to this element with a value of $value ElementName#AttributeName -> <$ElementName> will be appended to this element if it doesn't already exist, and have its attribute $AttributeName set to $value
public fromArray ( $array )
$array Hash to import into this element.
    public function fromArray($array)
    {
        foreach ($array as $key => $value) {
            $element = null;
            $attribute = null;
            $hash_position = strpos($key, '#');
            if ($hash_position === false) {
                $element = $key;
            } elseif ($hash_position === 0) {
                $attribute = substr($key, 1);
            } else {
                list($element, $attribute) = explode('#', $key, 2);
            }
            if (!is_null($element)) {
                if (!is_null($attribute)) {
                    $this->{$element}[$attribute] = $value;
                } else {
                    if (is_array($value)) {
                        // Detect numeric arrays and treat them as multiple
                        // instances of the same key.
                        $firstKey = key($value);
                        if ($firstKey === 0) {
                            if (strpos($element, ':') !== false) {
                                list($ns) = explode(':', $element, 2);
                                $baseNode = $this->_element->ownerDocument->createElementNS(Horde_Xml_Element::lookupNamespace($ns), $element);
                            } else {
                                $baseNode = $this->_element->ownerDocument->createElement($element);
                            }
                            foreach ($value as $v) {
                                $node = $baseNode->cloneNode();
                                if (is_array($v)) {
                                    $e = new Horde_Xml_Element($node);
                                    $e->fromArray($v);
                                } else {
                                    $node->nodeValue = $v;
                                    $e = new Horde_Xml_Element($node);
                                }
                                $this->appendChild($e);
                            }
                        } else {
                            $this->{$element}->fromArray($value);
                        }
                    } else {
                        $this->{$element} = $value;
                    }
                }
            } elseif (!is_null($attribute)) {
                $this[$attribute] = $value;
            }
        }
    }

Usage Example

Exemplo n.º 1
0
 public function testIllegalFromArray()
 {
     $failed = false;
     $e = new Horde_Xml_Element('<element />');
     try {
         $e->fromArray(array('#name' => array('foo' => 'bar')));
     } catch (InvalidArgumentException $e) {
         $failed = true;
     }
     $this->assertTrue($failed);
 }
All Usage Examples Of Horde_Xml_Element::fromArray