0 follower

Final Class Yiisoft\Hydrator\AttributeHandling\DataAttributesHandler

InheritanceYiisoft\Hydrator\AttributeHandling\DataAttributesHandler

Handles data attributes that implement {@see DataAttributeInterface}.

Method Details

Hide inherited methods

__construct() public method

public mixed __construct ( Yiisoft\Hydrator\AttributeHandling\ResolverFactory\AttributeResolverFactoryInterface $attributeResolverFactory )
$attributeResolverFactory Yiisoft\Hydrator\AttributeHandling\ResolverFactory\AttributeResolverFactoryInterface

                public function __construct(
    private AttributeResolverFactoryInterface $attributeResolverFactory,
) {
}

            
handle() public method

Handle data attributes.

public Yiisoft\Hydrator\DataInterface handle ( ReflectionClass $reflectionClass, Yiisoft\Hydrator\DataInterface $data )
$reflectionClass ReflectionClass

Reflection of class to attributes handle.

$data Yiisoft\Hydrator\DataInterface

Current data object ({@see \Yiisoft\Hydrator\DataInterface}).

                public function handle(ReflectionClass $reflectionClass, DataInterface $data): DataInterface
{
    $reflectionAttributes = $reflectionClass->getAttributes(
        DataAttributeInterface::class,
        ReflectionAttribute::IS_INSTANCEOF
    );
    foreach ($reflectionAttributes as $reflectionAttribute) {
        $attribute = $reflectionAttribute->newInstance();
        $resolver = $this->attributeResolverFactory->create($attribute);
        if (!$resolver instanceof DataAttributeResolverInterface) {
            throw new RuntimeException(
                sprintf(
                    'Data attribute resolver "%s" must implement "%s".',
                    get_debug_type($resolver),
                    DataAttributeResolverInterface::class,
                ),
            );
        }
        $data = $resolver->prepareData($attribute, $data);
    }
    return $data;
}