/** * @param string $nodeName * @return INode * @throws \LogicException * @throws \InvalidArgumentException */ public function root($nodeName) { if (!is_string($nodeName)) { throw new \InvalidArgumentException('Argument 1 expected to be string, ' . gettype($nodeName) . ' seen.'); } if (isset($this->rootNode)) { throw new \LogicException('"Where" nodes can only have a single immediate child element. Current child name: ' . $this->rootNode->nodeName() . '". Cannot add "' . $nodeName . '" as a 2nd immediate child.'); } $nodeName = strtolower(trim($nodeName)); if (isset($this->allowableChildren[$nodeName])) { $class = $this->nodeClassMap[$this->allowableChildren[$nodeName]]; $this->rootNode = new $class(); $this->rootNode->setParent($this); return $this->rootNode; } throw new \InvalidArgumentException('The node "' . $nodeName . '" does not map to a known node type.'); }
/** * @param INode $node * @return $this * @throws \InvalidArgumentException */ public function append(INode $node) { if (in_array($node->nodeName(), $this->allowableChildren)) { $node->setParent($this); $this->children[] = $node; return $this; } throw new \InvalidArgumentException(sprintf('Node "%s" cannot be added to "%s". Allowable child nodes: ["%s"].', $node->nodeName(), $this->nodeName(), implode('", "', $this->getAllowableChildren()))); }