DiDom\Element::appendChild PHP Метод

appendChild() публичный Метод

Adds new child at the end of the children.
public appendChild ( Element | DOMNode | array $nodes ) : Element
$nodes Element | DOMNode | array The appended child
Результат Element
    public function appendChild($nodes)
    {
        if ($this->node->ownerDocument === null) {
            throw new LogicException('Can not append child to element without owner document');
        }
        $nodes = is_array($nodes) ? $nodes : [$nodes];
        foreach ($nodes as $node) {
            if ($node instanceof Element) {
                $node = $node->getNode();
            }
            if (!$node instanceof \DOMNode) {
                throw new InvalidArgumentException(sprintf('Argument 1 passed to %s must be an instance of %s\\Element or DOMNode, %s given', __METHOD__, __NAMESPACE__, is_object($node) ? get_class($node) : gettype($node)));
            }
            Errors::disable();
            $cloned = $node->cloneNode(true);
            $newNode = $this->node->ownerDocument->importNode($cloned, true);
            $this->node->appendChild($newNode);
            Errors::restore();
        }
        return $this;
    }

Usage Example

Пример #1
0
 public function testAppendChild()
 {
     $list = new Element('ul');
     $this->assertCount(0, $list->find('li'));
     $node = new Element('li', 'foo');
     $list->appendChild($node);
     $this->assertCount(1, $list->find('li'));
     $items = [];
     $items[] = new Element('li', 'bar');
     $items[] = new Element('li', 'baz');
     $list->appendChild($items);
     $this->assertCount(3, $list->find('li'));
 }