0 follower

Final Class Yiisoft\Hydrator\Hydrator

InheritanceYiisoft\Hydrator\Hydrator
ImplementsYiisoft\Hydrator\HydratorInterface

Creates or hydrate objects from a set of raw data.

Method Details

Hide inherited methods

__construct() public method

public __construct( Yiisoft\Hydrator\TypeCaster\TypeCasterInterface|null $typeCaster null, Yiisoft\Hydrator\AttributeHandling\ResolverFactory\AttributeResolverFactoryInterface|null $attributeResolverFactory null, Yiisoft\Hydrator\ObjectFactory\ObjectFactoryInterface|null $objectFactory null ): mixed
$typeCaster Yiisoft\Hydrator\TypeCaster\TypeCasterInterface|null

Type caster used to cast raw values.

$attributeResolverFactory Yiisoft\Hydrator\AttributeHandling\ResolverFactory\AttributeResolverFactoryInterface|null
$objectFactory Yiisoft\Hydrator\ObjectFactory\ObjectFactoryInterface|null

                public function __construct(
    ?TypeCasterInterface $typeCaster = null,
    ?AttributeResolverFactoryInterface $attributeResolverFactory = null,
    ?ObjectFactoryInterface $objectFactory = null,
) {
    $this->typeCaster = $typeCaster ?? new CompositeTypeCaster(
        new PhpNativeTypeCaster(),
        new HydratorTypeCaster(),
    );
    $attributeResolverFactory ??= new ReflectionAttributeResolverFactory();
    $this->dataAttributesHandler = new DataAttributesHandler($attributeResolverFactory);
    $this->parameterAttributesHandler = new ParameterAttributesHandler($attributeResolverFactory, $this);
    $this->objectFactory = $objectFactory ?? new ReflectionObjectFactory();
    $this->constructorArgumentsExtractor = new ConstructorArgumentsExtractor(
        $this,
        $this->parameterAttributesHandler,
        $this->typeCaster,
    );
}

            
create() public method

public create( string $class, array|Yiisoft\Hydrator\DataInterface $data = [] ): object
$class string
$data array|Yiisoft\Hydrator\DataInterface

                public function create(string $class, array|DataInterface $data = []): object
{
    if (!class_exists($class)) {
        throw new NonExistClassException($class);
    }
    if (is_array($data)) {
        $data = new ArrayData($data);
    }
    $reflectionClass = new ReflectionClass($class);
    $constructor = $reflectionClass->getConstructor();
    $data = $this->dataAttributesHandler->handle($reflectionClass, $data);
    [$excludeProperties, $constructorArguments] = $this->constructorArgumentsExtractor->extract(
        $constructor,
        $data,
    );
    $object = $this->objectFactory->create($reflectionClass, $constructorArguments);
    $this->hydrateInternal(
        $object,
        $reflectionClass,
        ReflectionFilter::filterProperties($object, $reflectionClass, $excludeProperties),
        $data,
    );
    return $object;
}

            
hydrate() public method

public hydrate( object $object, array|Yiisoft\Hydrator\DataInterface $data = [] ): void
$object object
$data array|Yiisoft\Hydrator\DataInterface

                public function hydrate(object $object, array|DataInterface $data = []): void
{
    if (is_array($data)) {
        $data = new ArrayData($data);
    }
    $reflectionClass = new ReflectionClass($object);
    $data = $this->dataAttributesHandler->handle($reflectionClass, $data);
    $this->hydrateInternal(
        $object,
        $reflectionClass,
        ReflectionFilter::filterProperties($object, $reflectionClass),
        $data,
    );
}