public function insertBefore($node, $referenceNode = null)
    {
        if ($this->node->ownerDocument === null) {
            throw new LogicException('Can not insert child to element without owner document');
        }

        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))));
        }

        if ($referenceNode !== null) {
            if ($referenceNode instanceof Element) {
                $referenceNode = $referenceNode->getNode();
            }

            if (!$referenceNode instanceof DOMNode) {
                throw new InvalidArgumentException(sprintf('Argument 2 passed to %s must be an instance of %s\Element or DOMNode, %s given', __METHOD__, __NAMESPACE__, (is_object($referenceNode) ? get_class($referenceNode) : gettype($referenceNode))));
            }
        }

        Errors::disable();

        $clonedNode = $node->cloneNode(true);
        $newNode = $this->node->ownerDocument->importNode($clonedNode, true);

        $insertedNode = $this->node->insertBefore($newNode, $referenceNode);

        Errors::restore();

        return new Element($insertedNode);
    }
public function insertBefore($node, $referenceNode = null)
    {
        if ($this->node->ownerDocument === null) {
            throw new LogicException('Can not insert child to element without owner document');
        }

        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))));
        }

        if ($referenceNode !== null) {
            if ($referenceNode instanceof Element) {
                $referenceNode = $referenceNode->getNode();
            }

            if (!$referenceNode instanceof DOMNode) {
                throw new InvalidArgumentException(sprintf('Argument 2 passed to %s must be an instance of %s\Element or DOMNode, %s given', __METHOD__, __NAMESPACE__, (is_object($referenceNode) ? get_class($referenceNode) : gettype($referenceNode))));
            }
        }

        Errors::disable();

        $clonedNode = $node->cloneNode(true);
        $newNode = $this->node->ownerDocument->importNode($clonedNode, true);

        $insertedNode = $this->node->insertBefore($newNode, $referenceNode);

        Errors::restore();

        return new Element($insertedNode);
    }
public function insertBefore ( $ node , $ referenceNode = null ) { if ( $ this -> node -> ownerDocument === null ) { throw new LogicException ( 'Can not insert child to element without owner document' ) ; } 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 ) ) ) ) ; } if ( $ referenceNode !== null ) { if ( $ referenceNode instanceof Element ) { $ referenceNode = $ referenceNode -> getNode ( ) ; } if ( ! $ referenceNode instanceof DOMNode ) { throw new InvalidArgumentException ( sprintf ( 'Argument 2 passed to %s must be an instance of %s\Element or DOMNode, %s given' , __METHOD__ , __NAMESPACE__ , ( is_object ( $ referenceNode ) ? get_class ( $ referenceNode ) : gettype ( $ referenceNode ) ) ) ) ; } } Errors :: disable ( ) ; $ clonedNode = $ node -> cloneNode ( true ) ; $ newNode = $ this -> node -> ownerDocument -> importNode ( $ clonedNode , true ) ; $ insertedNode = $ this -> node -> insertBefore ( $ newNode , $ referenceNode ) ; Errors :: restore ( ) ; return new Element ( $ insertedNode ) ; }
Adds a new child before a reference node.

@param \DiDom\Element|\DOMNode $node The new node
@param \DiDom\Element|\DOMNode|null $referenceNode The reference node

@return \DiDom\Element

@throws \LogicException if current node has no owner document
@throws \InvalidArgumentException if $node is not an instance of \DOMNode or \DiDom\Element
@throws \InvalidArgumentException if $referenceNode is not an instance of \DOMNode or \DiDom\Element
Adds a new child before a reference node .