/** * Sets the parent node. * * @param Node|null $parent * @return self * * @throws \InvalidArgumentException */ public function setParent(Node $parent = null) { if ($parent === $this) { throw new \InvalidArgumentException('A node cannot have itself as a parent'); } if ($parent === $this->parent) { return $this; } if ($this->parent !== null) { $this->parent->remove($this); } $this->parent = $parent; if ($this->parent !== null && !$this->parent->contains($this, false)) { $this->parent->add($this); } return $this; }