/** * 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); } } } }
/** * @param array|XmlElement $errors * @param array|XmlElement $options * @param array|XmlElement $defaultErrors */ public function __construct($errors = array(), $options = array(), $defaultErrors = array()) { /** * Use xml object to set the errors * Otherwise use an array (Manually) */ if ($errors instanceof XmlElement) { foreach ($errors->getChildren() as $child) { $this->addError($child->getName(), $child->getContent()); } } else { if (true == is_array($errors)) { foreach ($errors as $type => $message) { $this->addError($type, $message); } } } /** * Use xml object to set the options * of the validation * Otherwise use an array (Manually) */ if ($options instanceof XmlElement) { foreach ($options->getAttributes() as $type => $value) { $this->addOption($type, $value); } } else { if (true == is_array($options)) { foreach ($options as $type => $value) { $this->addOption($type, $value); } } } /** * Set default errors as fallback */ if ($defaultErrors instanceof XmlElement) { foreach ($defaultErrors->getChildren() as $child) { $this->addError($child->getName(), $child->getContent(), true); } } else { if (true == is_array($defaultErrors)) { foreach ($defaultErrors as $type => $message) { if ($message instanceof Error) { $this->defaultErrors[] = $message; continue; } $this->addError($type, $message, true); } } } /** * Prepeare the result and * parse the errors */ $this->result = new Validation\Result($this->errors, $this->defaultErrors); }
/** * Creates a navigation from the given * config * * @param XmlElement $config */ public function __construct(XmlElement $config) { $this->setDomTag(self::UL_TAG); $this->setDomStr(self::UL_STR); $this->config = $config; // Apply config data foreach ($this->config->getChildren() as $child) { $this->pages[] = $this->getPageByConfig($child); } $this->setChildren($this->pages); }
/** * 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); } } } }
/** * Adds a form item to the * formular * * @param string $type * @param string $name * @param array $attributes * @param array|\Fewlines\Core\Xml\Tree\Element $validation * @return Form * @throws Exception\SelectOptionInvalidException * @throws Exception\ValidationParametersEmptyException */ public function addElement($type, $name, $attributes = array(), $validation = array()) { $element = null; /** * Force name tag to be set as attribute * for the dom element */ if (false == array_key_exists('name', $attributes)) { $attributes['name'] = $name; } // Create element by type switch (strtolower($type)) { case Input::HTML_TAG: if (false == array_key_exists('type', $attributes)) { return; } $class = __NAMESPACE__ . "\\Element\\Input\\"; $class .= ucfirst($attributes['type']); $element = new $class(); break; case Select::HTML_TAG: $class = __NAMESPACE__ . "\\Element\\Select"; $element = new $class(); if (array_key_exists('options', $attributes) && is_array($attributes['options'])) { $options = $attributes['options']; for ($i = 0, $len = count($options); $i < $len; $i++) { if ($options[$i] instanceof \Fewlines\Core\Xml\Tree\Element) { $content = (string) $options[$i]; $value = $options[$i]->getAttribute("value"); $selected = $options[$i]->getAttribute("selected"); } else { if (true == is_array($options[$i])) { $content = $options[$i]['content']; $value = $options[$i]['value']; $selected = array_key_exists('selected', $options[$i]) ? $options[$i]['selected'] : ''; } else { throw new Exception\SelectOptionInvalidException("\n\t\t\t\t\t\t\t\tThe option given has no valid format to\n\t\t\t\t\t\t\t\tconvert it.\n\t\t\t\t\t\t\t"); } } if (empty($selected)) { $selected = "false"; } $element->addOption(Select::createOption($content, $value, $selected)); } unset($attributes['options']); } break; case Textarea::HTML_TAG: $class = __NAMESPACE__ . "\\Element\\Textarea"; $element = new $class(); break; } if ($element instanceof Element) { // Set element name $element->setName($name); // Set other attributes $this->addElementAttributes($element, $attributes); // Set validation if ($validation != false) { if (true == $validation instanceof \Fewlines\Core\Xml\Tree\Element) { $element->setValidation($validation->getChildByName('errors'), $validation->getChildByName('options'), $this->getValidationErrors()); } else { if (true == is_array($validation)) { if (true == array_key_exists('options', $validation) && true == array_key_exists('errors', $validation)) { $element->setValidation($validation['errors'], $validation['options'], $this->getValidationErrors()); } else { throw new Exception\ValidationParametersEmptyException('Please set the keys errors and options for a valid validation of the given element. Use it in the last argument section as array.'); } } } } } if (false == is_null($element)) { $this->elements[] = $element; } return $this; }
/** * @param Element &$element */ private function applyChildrenEnvironment(Element &$element, Element &$parent) { if (!$this->environmentElementActive($element)) { $parent->removeChild($element); } else { foreach ($element->getChildren() as $child) { $this->applyChildrenEnvironment($child, $element); } } }