/** * Add the node to a position under the tree * * @param \SimpleXmlElement|Element $node * @param Element $parent */ public function addChild($node, $parent = null) { if ($node instanceof \SimpleXmlElement) { $name = $node->getName(); $attributes = (array) $node->attributes(); $content = trim((string) $node); $element = new Element($name, $attributes, $content); if (!$this->tree) { $this->tree = $element; } else { if (!$parent) { $parent = $this->tree; } $parent->addChild($element); } // Add child elements recursive if ($node->count() > 0) { foreach ($node as $childNode) { $this->addChild($childNode, $element); } } } else { if ($node instanceof Element) { if (!$this->tree) { $this->tree = $node; } else { if (!$parent) { $parent = $this->tree; } $parent->addChild($node); } } } }
/** * Adding the route to the collection (recursive). * All subroutes will be flattenend and also added * to this collection, to save the parent element in * which they are nested in it will given to the * route * * @param XmlElement $element * @param Routes\Route $parent */ public function addRouteByElement(XmlElement $element, Routes\Route $parent = null) { $name = strtolower($element->getName()); // Add route to parent if (true == preg_match(HTTP_METHODS_PATTERN, $name)) { $route = static::createRoute($name, $element->getAttribute('from'), $element->getAttribute('to')); // Add optional attributes if ($element->hasAttribute('id')) { $route->setId($element->getAttribute('id')); } $this->add($route); if (!is_null($parent)) { $route->setParent($parent); } // Add children if ($element->hasChildren()) { foreach ($element->getChildren() as $child) { $this->addRouteByElement($child, $route); } } } }