Jarves\Configuration\Model::propertyFromArray PHP Method

propertyFromArray() public method

public propertyFromArray ( string $key, mixed $value )
$key string
$value mixed
    public function propertyFromArray($key, $value)
    {
        $reflection = new \ReflectionClass($this);
        $setter = 'set' . ucfirst($key);
        $getter = 'get' . ucfirst($key);
        $setterValue = $value;
        $namespace = $this->getNamespacePath();
        if (method_exists($this, $setter)) {
            $reflectionMethod = $reflection->getMethod($setter);
            $reflectionMethodGet = $reflection->getMethod($getter);
            $parameters = $reflectionMethod->getParameters();
            $phpDocs = $this->getMethodMetaData($reflectionMethodGet);
            if (1 <= count($parameters)) {
                $firstParameter = $parameters[0];
                if ($firstParameter->getClass() && ($className = $firstParameter->getClass()->name)) {
                    $setterValue = new $className(null, $this->getJarves());
                    $setterValue->fromArray($value, $key);
                }
                if ($firstParameter->isArray() && is_array($value)) {
                    $setterValue = array();
                    $result = str_replace(array('[', ']'), '', $phpDocs['return']['type']);
                    $returnType = explode('|', $result)[0];
                    if (!class_exists($clazz = $returnType)) {
                        if (!class_exists($clazz = $namespace . $returnType)) {
                            if (!class_exists($clazz = $namespace . $key)) {
                                $clazz = null;
                            }
                        }
                    }
                    if (is_array($value)) {
                        foreach ($value as $subKey => $subValue) {
                            if ($clazz) {
                                $object = new $clazz(null, $this->getJarves());
                                $object->fromArray($subValue, $subKey);
                                $setterValue[] = $object;
                            } else {
                                $setterValue[] = $subValue;
                            }
                        }
                    }
                }
            }
        }
        if (is_callable(array($this, $setter))) {
            $this->{$setter}($setterValue);
        }
    }