PHPHtmlParser\Dom\AbstractNode::setParent PHP Method

setParent() public method

Sets the parent node.
public setParent ( InnerNode $parent )
$parent InnerNode
    public function setParent(InnerNode $parent)
    {
        // remove from old parent
        if (!is_null($this->parent)) {
            if ($this->parent->id() == $parent->id()) {
                // already the parent
                return $this;
            }
            $this->parent->removeChild($this->id);
        }
        $this->parent = $parent;
        // assign child to parent
        $this->parent->addChild($this);
        //clear any cache
        $this->clear();
        return $this;
    }

Usage Example

Exemplo n.º 1
0
 /**
  * Adds a child node to this node and returns the id of the child for this
  * parent.
  * 
  * @param AbstractNode $child
  * @return bool
  * @throws CircularExceptionException
  */
 public function addChild(AbstractNode $child)
 {
     $key = null;
     $newKey = 0;
     // check integrity
     if ($this->isAncestor($child->id())) {
         throw new CircularException('Can not add child. It is my ancestor.');
     }
     // check if child is itself
     if ($child->id() == $this->id) {
         throw new CircularException('Can not set itself as a child.');
     }
     if ($this->hasChildren()) {
         if (isset($this->children[$child->id()])) {
             // we already have this child
             return false;
         }
         $sibling = $this->lastChild();
         $key = $sibling->id();
         $this->children[$key]['next'] = $child->id();
     }
     // add the child
     $this->children[$child->id()] = array('node' => $child, 'next' => null, 'prev' => $key);
     // tell child I am the new parent
     $child->setParent($this);
     //clear any cache
     $this->clear();
     return true;
 }