0 follower

Final Class Yiisoft\Hydrator\TypeCaster\HydratorTypeCaster

InheritanceYiisoft\Hydrator\TypeCaster\HydratorTypeCaster
ImplementsYiisoft\Hydrator\TypeCaster\TypeCasterInterface

Casts arrays to objects.

Method Details

Hide inherited methods

cast() public method

public Yiisoft\Hydrator\Result cast ( mixed $value, Yiisoft\Hydrator\TypeCaster\TypeCastContext $context )
$value mixed
$context Yiisoft\Hydrator\TypeCaster\TypeCastContext

                public function cast(mixed $value, TypeCastContext $context): Result
{
    $type = $context->getReflectionType();
    $hydrator = $context->getHydrator();
    if (!is_array($value)) {
        return Result::fail();
    }
    if ($type instanceof ReflectionNamedType) {
        return $this->castInternal($value, $type, $hydrator);
    }
    if (!$type instanceof ReflectionUnionType) {
        return Result::fail();
    }
    foreach ($type->getTypes() as $t) {
        if (!$t instanceof ReflectionNamedType) {
            continue;
        }
        $result = $this->castInternal($value, $t, $hydrator);
        if ($result->isResolved()) {
            return $result;
        }
    }
    return Result::fail();
}