0 follower

Final Class Yiisoft\Definitions\ArrayDefinition

InheritanceYiisoft\Definitions\ArrayDefinition
ImplementsYiisoft\Definitions\Contract\DefinitionInterface

Builds an object by array config.

Psalm Types

Name Value
MethodOrPropertyItem array{0: string, 1: string, 2: mixed}
ArrayDefinitionConfig array{class: class-string, '__construct()'?: array}&array<string, mixed>

Constants

Hide inherited constants

Constant Value Description Defined By
CLASS_NAME 'class' Yiisoft\Definitions\ArrayDefinition
CONSTRUCTOR '__construct()' Yiisoft\Definitions\ArrayDefinition
TYPE_METHOD 'method' Yiisoft\Definitions\ArrayDefinition
TYPE_PROPERTY 'property' Yiisoft\Definitions\ArrayDefinition

Method Details

Hide inherited methods

fromConfig() public static method

Create ArrayDefinition from array config.

public static self fromConfig ( array $config )
$config array

                public static function fromConfig(array $config): self
{
    return new self(
        $config[self::CLASS_NAME],
        $config[self::CONSTRUCTOR] ?? [],
        self::getMethodsAndPropertiesFromConfig($config),
    );
}

            
fromPreparedData() public static method

public static self fromPreparedData ( string $class, array $constructorArguments = [], array $methodsAndProperties = [] )
$class string
$constructorArguments array
$methodsAndProperties array

                public static function fromPreparedData(string $class, array $constructorArguments = [], array $methodsAndProperties = []): self
{
    return new self($class, $constructorArguments, $methodsAndProperties);
}

            
getClass() public method

public string getClass ( )

                public function getClass(): string
{
    return $this->class;
}

            
getConstructorArguments() public method

public array getConstructorArguments ( )

                public function getConstructorArguments(): array
{
    return $this->constructorArguments;
}

            
getMethodsAndProperties() public method

public array getMethodsAndProperties ( )

                public function getMethodsAndProperties(): array
{
    return $this->methodsAndProperties;
}

            
merge() public method

Create a new definition that is merged from this definition and another definition.

public self merge ( Yiisoft\Definitions\ArrayDefinition $other )
$other Yiisoft\Definitions\ArrayDefinition

Definition to merge with.

return self

New definition that is merged from this definition and another definition.

                public function merge(self $other): self
{
    $new = clone $this;
    $new->class = $other->class;
    $new->constructorArguments = ArrayDefinitionHelper::mergeArguments($this->constructorArguments, $other->constructorArguments);
    $methodsAndProperties = $this->methodsAndProperties;
    foreach ($other->methodsAndProperties as $key => $item) {
        if ($item[0] === self::TYPE_PROPERTY) {
            $methodsAndProperties[$key] = $item;
        } elseif ($item[0] === self::TYPE_METHOD) {
            /** @psalm-suppress MixedArgument */
            $arguments = isset($methodsAndProperties[$key])
                ? ArrayDefinitionHelper::mergeArguments($methodsAndProperties[$key][2], $item[2])
                : $item[2];
            $methodsAndProperties[$key] = [$item[0], $item[1], $arguments];
        }
    }
    $new->methodsAndProperties = $methodsAndProperties;
    return $new;
}

            
resolve() public method

public object resolve ( \Psr\Container\ContainerInterface $container )
$container \Psr\Container\ContainerInterface

                public function resolve(ContainerInterface $container): object
{
    $class = $this->class;
    $resolvedConstructorArguments = $this->resolveFunctionArguments(
        $container,
        DefinitionExtractor::fromClassName($class),
        $this->getConstructorArguments(),
    );
    /** @psalm-suppress MixedMethodCall */
    $object = new $class(...$resolvedConstructorArguments);
    foreach ($this->getMethodsAndProperties() as $item) {
        [$type, $name, $value] = $item;
        if ($type === self::TYPE_METHOD) {
            /** @var array $value */
            if (method_exists($object, $name)) {
                $resolvedMethodArguments = $this->resolveFunctionArguments(
                    $container,
                    DefinitionExtractor::fromFunction(new ReflectionMethod($object, $name)),
                    $value,
                );
            } else {
                $resolvedMethodArguments = $value;
            }
            $setter = call_user_func_array([$object, $name], $resolvedMethodArguments);
            if ($setter instanceof $object) {
                /** @var object $object */
                $object = $setter;
            }
        } elseif ($type === self::TYPE_PROPERTY) {
            $object->$name = DefinitionResolver::resolve($container, $this->referenceContainer, $value);
        }
    }
    return $object;
}

            
withReferenceContainer() public method

public self withReferenceContainer ( \Psr\Container\ContainerInterface|null $referenceContainer )
$referenceContainer \Psr\Container\ContainerInterface|null

Container to resolve references with.

                public function withReferenceContainer(?ContainerInterface $referenceContainer): self
{
    $new = clone $this;
    $new->referenceContainer = $referenceContainer;
    return $new;
}