Jarves\Configuration\Model::importNode PHP Method

importNode() public method

public importNode ( DOMNode $element ) : array
$element DOMNode
return array imported properties
    public function importNode(\DOMNode $element)
    {
        $reflection = new \ReflectionClass($this);
        $imported = [];
        /** @var \DOMNode $child */
        foreach ($element->childNodes as $child) {
            $nodeName = $child->nodeName;
            $value = $child->nodeValue;
            if ('#comment' === $nodeName) {
                continue;
            }
            if ('#text' === $nodeName) {
                if (null == $this->nodeValueVar) {
                    continue;
                } else {
                    $nodeName = $this->nodeValueVar;
                }
            }
            $elementToArrayProperty = isset($this->elementToArray[$nodeName]) ? $this->elementToArray[$nodeName] : null;
            $propertyName = $elementToArrayProperty ?: $nodeName;
            $propertyName = Tools::char2Camelcase($propertyName, '-');
            $setter = 'set' . ucfirst($propertyName);
            $getter = 'get' . ucfirst($propertyName);
            $setterValue = $value;
            $namespace = $this->getNamespacePath();
            if (method_exists($this, $setter)) {
                $reflectionMethod = $reflection->getMethod($setter);
                $parameters = $reflectionMethod->getParameters();
                $phpDocs = $this->getMethodMetaData($reflectionMethod);
                if (1 <= count($parameters)) {
                    $firstParameter = $parameters[0];
                    if ($firstParameter->getClass() && ($className = $firstParameter->getClass()->name)) {
                        $setterValue = new $className($child, $this->getJarves());
                    }
                    $result = str_replace(array('[', ']'), '', $phpDocs['param']['type']);
                    $types = explode('|', $result);
                    $clazz = '';
                    if (1 === count($types)) {
                        $returnType = $types[0];
                        if (!class_exists($clazz = $namespace . $returnType)) {
                            if (!class_exists($clazz = $returnType)) {
                                if (!'string' == ($clazz = $returnType)) {
                                    $clazz = null;
                                }
                            }
                        }
                    }
                    $noClazz = !$clazz;
                    if ($firstParameter->isArray()) {
                        if ($elementToArrayProperty) {
                            //for elements without plural parent: <route><req><req></route>
                            $setterValue = $this->{$getter}() ?: [];
                            if ($noClazz) {
                                $clazz = $namespace . ucfirst($nodeName);
                            }
                            if (class_exists($clazz)) {
                                $obj = new $clazz($child, $this->getJarves());
                                $setterValue[] = $obj;
                            } else {
                                if ($key = $child->attributes->getNamedItem('key')) {
                                    $setterValue[$key->nodeValue] = $value;
                                } else {
                                    $setterValue[] = $value;
                                }
                            }
                        } else {
                            $setterValue = array();
                            //for elements with plural parent: <route><reqs><req><req></reqs></route>
                            foreach ($child->childNodes as $subChild) {
                                if ('#' !== substr($subChild->nodeName, 0, 1)) {
                                    if ($noClazz) {
                                        $clazz = $namespace . ucfirst($subChild->nodeName);
                                    }
                                    if (class_exists($clazz)) {
                                        $object = new $clazz($subChild, $this->getJarves());
                                        $setterValue[] = $object;
                                    } else {
                                        if ($key = $subChild->attributes->getNamedItem('key')) {
                                            $setterValue[$key->nodeValue] = $subChild->nodeValue;
                                        } else {
                                            $setterValue[] = $subChild->nodeValue;
                                        }
                                    }
                                }
                            }
                        }
                    } else {
                        if (class_exists($clazz)) {
                            $setterValue = new $clazz($child, $this->getJarves());
                        }
                    }
                }
            }
            if (is_callable(array($this, $setter))) {
                $this->{$setter}($setterValue);
            } else {
                if (!isset($this->{$nodeName})) {
                    $this->extractExtraNodes($child, $this->additionalNodes);
                }
            }
            $imported[] = $propertyName;
        }
        foreach ($element->attributes as $attribute) {
            $nodeName = $attribute->nodeName;
            $value = $attribute->nodeValue;
            $nodeName = lcfirst(Tools::char2Camelcase($nodeName, '-'));
            $setter = 'set' . ucfirst($nodeName);
            if (is_callable(array($this, $setter))) {
                $this->{$setter}($value);
            } else {
                $this->additionalAttributes[$nodeName] = $value;
            }
            $imported[] = $nodeName;
        }
        return $imported;
    }