Final Class Yiisoft\Hydrator\AttributeHandling\ParameterAttributesHandler
| Inheritance | Yiisoft\ |
|---|
Handles parameters' attributes that implement {@see ParameterAttributeInterface}.
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __construct() | Yiisoft\ |
|
| handle() | Handle parameters' attributes of passed parameter. | Yiisoft\ |
| withHydrator() | Yiisoft\ |
Method Details
| public mixed __construct ( Yiisoft\ | ||
| $attributeResolverFactory | Yiisoft\ |
|
| $hydrator | ?\ |
|
public function __construct(
private AttributeResolverFactoryInterface $attributeResolverFactory,
private ?HydratorInterface $hydrator = null,
) {}
Handle parameters' attributes of passed parameter.
| public Yiisoft\ | ||
| $parameter | ReflectionParameter|ReflectionProperty |
Parameter or property reflection to handle attributes from. |
| $resolveResult | Yiisoft\ |
The resolved value object to pass to attribute resolver via {@see \ |
| $data | Yiisoft\ |
Raw data and map to pass to attribute resolver via {@see \ |
| return | Yiisoft\ |
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;
}
| public self withHydrator ( Yiisoft\ | ||
| $hydrator | Yiisoft\ |
|
public function withHydrator(HydratorInterface $hydrator): self
{
$new = clone $this;
$new->hydrator = $hydrator;
return $new;
}
User Contributed Notes
Leave a comment
Join the conversation to share a note.