Final Class Yiisoft\Hydrator\Hydrator
| Inheritance | Yiisoft\Hydrator\Hydrator |
|---|---|
| Implements | Yiisoft\Hydrator\HydratorInterface |
Creates or hydrate objects from a set of raw data.
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __construct() | Yiisoft\Hydrator\Hydrator | |
| create() | Yiisoft\Hydrator\Hydrator | |
| hydrate() | Yiisoft\Hydrator\Hydrator |
Method Details
| 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,
);
}
| 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;
}
| 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,
);
}
Signup or Login in order to comment.