Create field instance by its declaration.

@param array $declaration Field declaration.
@return AbstractField Field instance.
@throws ConfigurationException If invalid declaration is presented.
9847
private static function instantiate(array $declaration)
    {
        $fieldType = array_shift($declaration);

        $class = "\\Zerg\\Field\\" . ucfirst(strtolower($fieldType));
        if (class_exists($class)) {
            $reflection = new \ReflectionClass($class);
            return $reflection->newInstanceArgs($declaration);
        }

        throw new ConfigurationException("Field {$fieldType} doesn't exist");
    }
1204342
public function create()
    {
        /**
         * @var ConfigurationInterface $configuration
         */
        $classNamespace = $this->getEntityNamespace();
        $configuration = new $classNamespace();

        $configuration
            ->setNamespace('')
            ->setType(ElcodiConfigurationTypes::TYPE_STRING)
            ->setCreatedAt($this->now());

        return $configuration;
    }
306496
public function setDeclarations($declarations)
    {
        $this->declarations = [];
        if (!is_array($declarations)) {
            $declarations = [$declarations];
        }
        foreach ($declarations as $declaration) {
            $this->addDeclaration($declaration);
        }

        return $this;
    }
153411
protected function createTaxTypeFromDefinition(array $definition)
    {
        // Load the referenced zone.
        $definition['zone'] = $this->zoneRepository->get($definition['zone']);
        // Provide defaults.
        if (!isset($definition['compound'])) {
            $definition['compound'] = false;
        }
        if (!isset($definition['display_inclusive'])) {
            $definition['display_inclusive'] = false;
        }
        if (!isset($definition['rounding_mode'])) {
            $definition['rounding_mode'] = PHP_ROUND_HALF_UP;
        }

        $type = new TaxType();
        // Bind the closure to the TaxType object, giving it access to
        // its protected properties. Faster than both setters and reflection.
        $setValues = \Closure::bind(function ($definition) {
            $this->id = $definition['id'];
            $this->name = $definition['name'];
            $this->compound = $definition['compound'];
            $this->displayInclusive = $definition['display_inclusive'];
            $this->roundingMode = $definition['rounding_mode'];
            $this->zone = $definition['zone'];
            if (isset($definition['generic_label'])) {
                $this->genericLabel = $definition['generic_label'];
            }
            if (isset($definition['tag'])) {
                $this->tag = $definition['tag'];
            }
        }, $type, '\CommerceGuys\Tax\Model\TaxType');
        $setValues($definition);

        foreach ($definition['rates'] as $rateDefinition) {
            $rate = $this->createTaxRateFromDefinition($rateDefinition);
            $type->addRate($rate);
        }

        return $type;
    }
530469
public static function create($configuration)
    {
        $currentConfiguration = is_array($configuration) ?
                array_merge(self::$defaultConfiguration, $configuration)
                : self::$defaultConfiguration;
        $class = self::className($currentConfiguration);
        return new $class($currentConfiguration["params"]);
    }
100809
protected function initializeField($field)
    {
        $field->setParentContext($this);
        $this->getFieldManager()->pushField($field);
        return $field;
    }
193606
public function createAttribute($code, array $attributeConfiguration = [])
    {
        $attributeClass = $this->attributeClass;
        /** @var AttributeInterface $attribute */
        $attribute = new $attributeClass(
            $code,
            $this->attributeTypeRegistry,
            $attributeConfiguration,
            $this->globalContextMask
        );
        if (method_exists($attribute, 'setTranslator')) {
            $attribute->setTranslator($this->translator);
        }

        return $attribute;
    }
1370254
public function resolve(array $settings = [])
    {
        return (new Configuration())->newDefaultAnnotationDriver(
            Arr::get($settings, 'paths', []),
            Arr::get($settings, 'simple', false)
        );
    }
684198
public function createFields(ActionConfiguration $configuration): array
    {
        $fields = [];

        foreach ($configuration->getParameter('fields') as $field => $fieldConfiguration) {
            $fields[] = $this->create($field, $fieldConfiguration, $configuration);
        }

        return $fields;
    }
1235572
final public static function getInstance(?array $configurations = null): self
    {
        if (is_null(self::$instance)) {
            self::$instance = new self($configurations);
        }
        return self::$instance;
    }
340276
private function createApplicationConfiguration(array $configuration): ApplicationConfiguration
    {
        $resolver = new OptionsResolver();
        $applicationConfiguration = new ApplicationConfiguration();
        $applicationConfiguration->configureOptions($resolver);

        try {
            $applicationConfiguration->setParameters($resolver->resolve($configuration));
        } catch (Exception $exception) {
            throw new Exception(
                'An error has been found when processing the configuration of the admin application',
                0,
                $exception
            );
        }

        return $applicationConfiguration;
    }
747597
public function compile(array $expression, CompilationContext $compilationContext)
    {
        $compiler = &$compilationContext->compiler;
        $className = $expression['left']['value'];
        $constant = $expression['right']['value'];

        /*
         * Fetch the class definition according to the class where the constant
         * is supposed to be declared
         */
        if (!\in_array($className, ['this', 'self', 'static', 'parent'])) {
            $className = $compilationContext->getFullName($className);
            if ($compiler->isClass($className) || $compiler->isInterface($className)) {
                $classDefinition = $compiler->getClassDefinition($className);
            } else {
                if ($compiler->isBundledClass($className) || $compiler->isBundledInterface($className)) {
                    $classDefinition = $compiler->getInternalClassDefinition($className);
                } else {
                    throw new CompilerException("Cannot locate class '".$className."'", $expression['left']);
                }
            }
        } else {
            if (\in_array($className, ['self', 'static', 'this'])) {
                $classDefinition = $compilationContext->classDefinition;
            } else {
                if ('parent' == $className) {
                    $classDefinition = $compilationContext->classDefinition;
                    $extendsClass = $classDefinition->getExtendsClass();
                    if (!$extendsClass) {
                        throw new CompilerException(
                            sprintf(
                                'Cannot find constant called "%s" on parent because class %s does not extend any class',
                                $constant,
                                $classDefinition->getCompleteName()
                            ),
                            $expression
                        );
                    } else {
                        $classDefinition = $classDefinition->getExtendsClassDefinition();
                    }
                }
            }
        }

        /*
         * Constants are resolved to their values at compile time
         * so we need to check that they effectively do exist
         */
        if (!$classDefinition->hasConstant($constant)) {
            throw new CompilerException(
                sprintf(
                    "Class '%s' does not have a constant called: '%s'",
                    $classDefinition->getCompleteName(),
                    $constant
                ),
                $expression
            );
        }

        /*
         * We can optimize the reading of constants by avoiding query their value every time
         */
        if (!$compilationContext->config->get('static-constant-class-folding', 'optimizations')) {
            /*
             * Resolves the symbol that expects the value
             */
            if ($this->expecting) {
                if ($this->expectingVariable) {
                    $symbolVariable = $this->expectingVariable;
                    $symbolVariable->initVariant($compilationContext);
                } else {
                    $symbolVariable = $compilationContext->symbolTable->getTempVariableForWrite(
                        'variable',
                        $compilationContext,
                        $expression
                    );
                }
            } else {
                $symbolVariable = $compilationContext->symbolTable->getTempVariableForWrite(
                    'variable',
                    $compilationContext,
                    $expression
                );
            }

            /*
             * Variable that receives property accesses must be polymorphic
             */
            if (!$symbolVariable->isVariable()) {
                throw new CompilerException(
                    'Cannot use variable: '.$symbolVariable->getType().' to assign class constants',
                    $expression
                );
            }

            $symbolVariable->setDynamicTypes('undefined');

            $compilationContext->headersManager->add('kernel/object');

            $compilationContext->codePrinter->output(
                sprintf(
                    'zephir_get_class_constant(%s, %s, SS("%s") TSRMLS_CC);',
                    $symbolVariable->getName(),
                    $classDefinition->getClassEntry($compilationContext),
                    $constant
                )
            );

            return new CompiledExpression('variable', $symbolVariable->getRealName(), $expression);
        }

        $constantDefinition = $classDefinition->getConstant($constant);

        if ($constantDefinition instanceof ClassConstant) {
            $constantDefinition->processValue($compilationContext);
            $value = $constantDefinition->getValueValue();
            $type = $constantDefinition->getValueType();
        } else {
            $value = $constantDefinition;
            $type = \gettype($value);
            if ('integer' == $type) {
                $type = 'int';
            }
        }

        switch ($type) {
            case 'string':
            case 'int':
            case 'double':
            case 'float':
            case 'bool':
            case 'null':
                break;
            default:
                $compilationContext->logger->warning(
                    "Constant '".$constantDefinition->getName()."' does not exist at compile time",
                    ['nonexistent-constant', $expression]
                );

                return new CompiledExpression('null', null, $expression);
        }

        /*
         * Return the value as a literal expression
         */
        return new CompiledExpression($type, $value, $expression);
    }
1240926
public function create(string $name, array $configuration, ActionConfiguration $actionConfiguration): FieldInterface
    {
        $resolver = new OptionsResolver();
        $configuration = $this->resolveConfiguration($configuration, $actionConfiguration);

        // Dispatch an event to allow dynamic changes on the form type
        $event = new FieldEvent(
            $actionConfiguration->getAdminName(),
            $actionConfiguration->getActionName(),
            $name,
            $actionConfiguration->getAdminConfiguration()->get('entity'),
            $configuration['type']
        );
        $this->eventDispatcher->dispatch(Events::FIELD_PRE_CREATE, $event);

        if (null === $event->getType()) {
            throw new FieldTypeNotFoundException($event->getAdminName(), $event->getActionName(), $name);
        }
        $type = $event->getType();
        $options = array_merge($configuration['options'], $event->getOptions());

        if (!key_exists($type, $this->fieldsMapping)) {
            $type = 'auto';
        }
        $field = $this->instanciateField($name, $type);
        $field->configureOptions($resolver, $actionConfiguration);

        try {
            $field->setOptions($resolver->resolve($options));
        } catch (\Exception $exception) {
            throw new Exception(
                'An error has occurred when resolving the options for the field "'.$name.'": '.$exception->getMessage(),
                $exception->getCode(),
                $exception
            );
        }
        $event = new FieldEvent(
            $actionConfiguration->getAdminName(),
            $actionConfiguration->getActionName(),
            $name,
            $actionConfiguration->getAdminConfiguration()->get('entity'),
            $type,
            $field
        );
        $this->eventDispatcher->dispatch(Events::FIELD_POST_CREATE, $event);

        return $field;
    }
224471
public function getClassname($declaration) {
        if(!$this->injectionConfiguration->has($declaration)) {
            throw new DeclarationNotFound("Declaration of Class '" . $declaration . "' not found.");
        }

        $json = $this->injectionConfiguration->get($declaration);
        if(!isset($json["class"])) {
            throw new \Exception("Index 'class' not found in declaration '" . $declaration . "'.");
        }

        return $json["class"];
    }
1042582
public function compile(CompilationContext $compilationContext)
    {
        $statement = $this->statement;

        $codePrinter = $compilationContext->codePrinter;

        if (isset($statement['expr'])) {
            $currentMethod = $compilationContext->currentMethod;

            if ($currentMethod->isConstructor()) {
                throw new CompilerException('Constructors cannot return values', $statement['expr']);
            }

            if ($currentMethod->isVoid()) {
                throw new CompilerException(
                    "Method is marked as 'void' and it must not return any value",
                    $statement['expr']
                );
            }

            /*
             * Use return member for properties on this
             */
            if ('property-access' == $statement['expr']['type']) {
                if ('variable' == $statement['expr']['left']['type']) {
                    if ('this' == $statement['expr']['left']['value']) {
                        if ('variable' == $statement['expr']['right']['type']) {
                            /*
                             * If the property is accessed on 'this', we check if the property does exist.
                             */
                            $property = $statement['expr']['right']['value'];
                            $classDefinition = $compilationContext->classDefinition;
                            if (!$classDefinition->hasProperty($property)) {
                                throw new CompilerException(
                                    sprintf(
                                        "Class '%s' does not have a property called: '%s'",
                                        $classDefinition->getCompleteName(),
                                        $property
                                    ),
                                    $statement['expr']['right']
                                );
                            }

                            $compilationContext->headersManager->add('kernel/object');
                            $codePrinter->output('RETURN_MM_MEMBER(getThis(), "'.$property.'");');

                            return;
                        }
                    }
                }
            }

            /*
             * Fetches return_value and tries to return the value directly there.
             */
            $variable = $compilationContext->symbolTable->getVariable('return_value');

            $expr = new Expression($statement['expr']);
            $expr->setExpectReturn(true, $variable);
            $expr->setReadOnly(true);
            $resolvedExpr = $expr->compile($compilationContext);

            /*
             * Here we check if the variable returns a compatible type according to its type hints
             */
            if ($currentMethod->hasReturnTypes()) {
                switch ($resolvedExpr->getType()) {
                    case Types::T_NULL:
                        if (false == $currentMethod->areReturnTypesNullCompatible()) {
                            throw new InvalidTypeException($resolvedExpr->getType(), $statement['expr']);
                        }
                        break;

                    case Types::T_INT:
                    case Types::T_UINT:
                    case Types::T_LONG:
                    case Types::T_ULONG:
                    case Types::T_CHAR:
                    case Types::T_UCHAR:
                        if (false == $currentMethod->areReturnTypesIntCompatible()) {
                            throw new InvalidTypeException($resolvedExpr->getType(), $statement['expr']);
                        }
                        break;

                    case Types::T_BOOL:
                        if (false == $currentMethod->areReturnTypesBoolCompatible()) {
                            throw new InvalidTypeException($resolvedExpr->getType(), $statement['expr']);
                        }
                        break;

                    case Types::T_DOUBLE:
                        if (false == $currentMethod->areReturnTypesDoubleCompatible()) {
                            throw new InvalidTypeException($resolvedExpr->getType(), $statement['expr']);
                        }
                        break;

                    case Types::T_STRING:
                    case Types::T_ISTRING:
                        if (false == $currentMethod->areReturnTypesStringCompatible()) {
                            throw new InvalidTypeException($resolvedExpr->getType(), $statement['expr']);
                        }
                        break;

                    case Types::T_VARIABLE:
                        $symbolVariable = $compilationContext->symbolTable->getVariableForRead(
                            $resolvedExpr->getCode(),
                            $compilationContext,
                            $statement['expr']
                        );

                        switch ($symbolVariable->getType()) {
                            case Types::T_INT:
                            case Types::T_UINT:
                            case Types::T_LONG:
                            case Types::T_ULONG:
                            case Types::T_CHAR:
                            case Types::T_UCHAR:
                                if (false == $currentMethod->areReturnTypesIntCompatible()) {
                                    throw new InvalidTypeException($resolvedExpr->getType(), $statement['expr']);
                                }
                                break;

                            case Types::T_DOUBLE:
                                if (false == $currentMethod->areReturnTypesDoubleCompatible()) {
                                    throw new InvalidTypeException($resolvedExpr->getType(), $statement['expr']);
                                }
                                break;

                            case Types::T_STRING:
                            case Types::T_ISTRING:
                                if (false == $currentMethod->areReturnTypesStringCompatible()) {
                                    throw new InvalidTypeException($resolvedExpr->getType(), $statement['expr']);
                                }
                                break;

                            case Types::T_BOOL:
                                if (false == $currentMethod->areReturnTypesBoolCompatible()) {
                                    throw new InvalidTypeException($resolvedExpr->getType(), $statement['expr']);
                                }
                                break;

                            case Types::T_VARIABLE:
                                break;
                        }
                        break;
                }
            }

            switch ($resolvedExpr->getType()) {
                case Types::T_NULL:
                    $codePrinter->output('RETURN_MM_NULL();');
                    break;

                case Types::T_INT:
                case Types::T_UINT:
                case Types::T_LONG:
                case Types::T_ULONG:
                case Types::T_CHAR:
                case Types::T_UCHAR:
                    $codePrinter->output('RETURN_MM_LONG('.$resolvedExpr->getCode().');');
                    break;

                case Types::T_BOOL:
                    $codePrinter->output('RETURN_MM_BOOL('.$resolvedExpr->getBooleanCode().');');
                    break;

                case Types::T_DOUBLE:
                    $codePrinter->output('RETURN_MM_DOUBLE('.$resolvedExpr->getCode().');');
                    break;

                case Types::T_STRING:
                case Types::T_ISTRING:
                    $compilationContext->backend->returnString(
                        add_slashes($resolvedExpr->getCode()),
                        $compilationContext
                    );
                    break;

                case Types::T_ARRAY:
                    if ('return_value' != $resolvedExpr->getCode()) {
                        $codePrinter->output('RETURN_CTOR('.$resolvedExpr->getCode().');');
                    } else {
                        $codePrinter->output('RETURN_MM();');
                    }
                    break;

                case Types::T_VARIABLE:
                    if (!isset($symbolVariable)) {
                        $symbolVariable = $compilationContext->symbolTable->getVariableForRead(
                            $resolvedExpr->getCode(),
                            $compilationContext,
                            $statement['expr']
                        );
                    }

                    switch ($symbolVariable->getType()) {
                        case Types::T_INT:
                        case Types::T_UINT:
                        case Types::T_LONG:
                        case Types::T_ULONG:
                        case Types::T_CHAR:
                        case Types::T_UCHAR:
                            $codePrinter->output('RETURN_MM_LONG('.$symbolVariable->getName().');');
                            break;

                        case Types::T_DOUBLE:
                            $codePrinter->output('RETURN_MM_DOUBLE('.$symbolVariable->getName().');');
                            break;

                        case Types::T_STRING:
                        case Types::T_ISTRING:
                        case Types::T_ARRAY:
                            $codePrinter->output(
                                'RETURN_CTOR('.$compilationContext->backend->getVariableCode($symbolVariable).');'
                            );
                            break;

                        case Types::T_BOOL:
                            $codePrinter->output('RETURN_MM_BOOL('.$symbolVariable->getName().');');
                            break;

                        case Types::T_VARIABLE:
                            if ('this_ptr' == $symbolVariable->getName()) {
                                $codePrinter->output('RETURN_THIS();');
                            } else {
                                if ('return_value' != $symbolVariable->getName()) {
                                    if (!$symbolVariable->isExternal()) {
                                        if ($symbolVariable->isLocalOnly()) {
                                            $codePrinter->output(
                                                sprintf(
                                                    'RETURN_LCTOR(%s);',
                                                    $compilationContext->backend->getVariableCode($symbolVariable)
                                                )
                                            );
                                        } else {
                                            if (!$symbolVariable->isMemoryTracked()) {
                                                $codePrinter->output(
                                                    sprintf(
                                                        'RETURN_CTOR(%s);',
                                                        $compilationContext->backend->getVariableCode($symbolVariable)
                                                    )
                                                );
                                            } else {
                                                $codePrinter->output(
                                                    sprintf(
                                                        'RETURN_CCTOR(%s);',
                                                        $compilationContext->backend->getVariableCode($symbolVariable)
                                                    )
                                                );
                                            }
                                        }
                                    } else {
                                        $codePrinter->output(
                                            sprintf(
                                                'RETVAL_ZVAL(%s, 1, 0);',
                                                $compilationContext->backend->getVariableCode($symbolVariable)
                                            )
                                        );
                                        $codePrinter->output('RETURN_MM();');
                                    }
                                } else {
                                    $codePrinter->output('RETURN_MM();');
                                }
                            }
                            if ($symbolVariable->isTemporal()) {
                                $symbolVariable->setIdle(true);
                            }
                            break;

                        default:
                            throw new CompilerException(
                                sprintf("Cannot return variable '%s'", $symbolVariable->getType()),
                                $statement['expr']
                            );
                    }
                    break;

                default:
                    throw new CompilerException("Cannot return '".$resolvedExpr->getType()."'", $statement['expr']);
            }

            return;
        }

        /*
         * Return without an expression
         */
        $codePrinter->output('RETURN_MM_NULL();');
    }
928991
public function add(array $fields)
    {
        foreach ($fields as $field => $normalizer) {
            if (is_array($normalizer)) {
                // 'field' => ['field.path', 'defaultValue']
                if (2 == count($normalizer)) {
                    $this->path($field, $normalizer[ 0 ], $normalizer[ 1 ]);
                } else {
                    throw new GenericException('Array can contain only 2 elements!');
                }
            } // 'field' => '::method'
            elseif (0 === strpos($normalizer, '::')) {
                $method = str_replace('::', '', $normalizer);
                if (is_int($field)) {
                    $field = $method;
                }
                $this->method($field, $method);
            } // 'field' => 'field.path'
            else {
                $this->path($field, $normalizer);
            }
        }

        return $this;
    }
1349334
public function addField(
        $fieldOrName,
        $classes = [],
        string $type = null,
        $value = null,
        string $title = null
    ) : self {
        if (!$fieldOrName instanceof Field) {
            $fieldOrName = new Field(
                $fieldOrName,
                is_array($classes) ? $classes : [$classes],
                $type,
                $value,
                $title
            );
        }

        $this->fields[] = $fieldOrName;

        return $this;
    }