0 follower

Final Class Yiisoft\Hydrator\AttributeHandling\ParameterAttributesHandler

InheritanceYiisoft\Hydrator\AttributeHandling\ParameterAttributesHandler

Handles parameters' attributes that implement {@see ParameterAttributeInterface}.

Method Details

Hide inherited methods

__construct() public method

public mixed __construct ( Yiisoft\Hydrator\AttributeHandling\ResolverFactory\AttributeResolverFactoryInterface $attributeResolverFactory, Yiisoft\Hydrator\HydratorInterface|null $hydrator null )
$attributeResolverFactory Yiisoft\Hydrator\AttributeHandling\ResolverFactory\AttributeResolverFactoryInterface
$hydrator Yiisoft\Hydrator\HydratorInterface|null

                public function __construct(
    private AttributeResolverFactoryInterface $attributeResolverFactory,
    private ?HydratorInterface $hydrator = null,
) {
}

            
handle() public method

Handle parameters' attributes of passed parameter.

public Yiisoft\Hydrator\Result handle ( ReflectionParameter|ReflectionProperty $parameter, Yiisoft\Hydrator\Result|null $resolveResult null, Yiisoft\Hydrator\DataInterface|null $data null )
$parameter ReflectionParameter|ReflectionProperty

Parameter or property reflection to handle attributes from.

$resolveResult Yiisoft\Hydrator\Result|null

The resolved value object to pass to attribute resolver via {@see \Yiisoft\Hydrator\AttributeHandling\ParameterAttributeResolveContext}.

$data Yiisoft\Hydrator\DataInterface|null

Raw data and map to pass to attribute resolver via {@see \Yiisoft\Hydrator\AttributeHandling\ParameterAttributeResolveContext}.

return Yiisoft\Hydrator\Result

The resolved from attributes' value object.

                public function handle(
    ReflectionParameter|ReflectionProperty $parameter,
    ?Result $resolveResult = null,
    ?DataInterface $data = null
): Result {
    if ($this->hydrator === null) {
        throw new LogicException('Hydrator is not set in parameter attributes handler.');
    }
    $resolveResult ??= Result::fail();
    $data ??= new ArrayData();
    $reflectionAttributes = $parameter
        ->getAttributes(ParameterAttributeInterface::class, ReflectionAttribute::IS_INSTANCEOF);
    foreach ($reflectionAttributes as $reflectionAttribute) {
        $attribute = $reflectionAttribute->newInstance();
        $resolver = $this->attributeResolverFactory->create($attribute);
        if (!$resolver instanceof ParameterAttributeResolverInterface) {
            throw new RuntimeException(
                sprintf(
                    'Parameter attribute resolver "%s" must implement "%s".',
                    get_debug_type($resolver),
                    ParameterAttributeResolverInterface::class,
                ),
            );
        }
        $context = new ParameterAttributeResolveContext($parameter, $resolveResult, $data, $this->hydrator);
        $tryResolveResult = $resolver->getParameterValue($attribute, $context);
        if ($tryResolveResult->isResolved()) {
            $resolveResult = $tryResolveResult;
        }
    }
    return $resolveResult;
}

            
withHydrator() public method

public self withHydrator ( Yiisoft\Hydrator\HydratorInterface $hydrator )
$hydrator Yiisoft\Hydrator\HydratorInterface

                public function withHydrator(HydratorInterface $hydrator): self
{
    $new = clone $this;
    $new->hydrator = $hydrator;
    return $new;
}