/** * Add a child node * * @param TreeNode $child * @param TreeNode $parent * * @return $this */ public function addChild(TreeNode $child, TreeNode $parent = null) { if ($parent === null) { $parent = $this->sentinel; } elseif (!isset($this->nodes[$parent->getId()])) { throw new LogicException(sprintf('Can\'t append child node %s to parent node %s: Parent node does not exist', $child->getId(), $parent->getId())); } if (isset($this->nodes[$child->getId()])) { throw new LogicException(sprintf('Can\'t append child node %s to parent node %s: Child node does already exist', $child->getId(), $parent->getId())); } $this->nodes[$child->getId()] = $child; $parent->appendChild($child); return $this; }
/** * Create a new iterator over a tree node's children * * @param TreeNode $node */ public function __construct(TreeNode $node) { $this->children = new ArrayIterator($node->getChildren()); }
/** * {@inheritdoc} */ public function setId($id) { return parent::setId(str_replace(' ', '-', (string) $id)); }