0 follower

Final Class Yiisoft\Hydrator\TypeCaster\EnumTypeCaster

InheritanceYiisoft\Hydrator\TypeCaster\EnumTypeCaster
ImplementsYiisoft\Hydrator\TypeCaster\TypeCasterInterface

Casts values to enumerations.

Public Methods

Hide inherited methods

Method Description Defined By
cast() Yiisoft\Hydrator\TypeCaster\EnumTypeCaster

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();
    if ($type instanceof ReflectionNamedType) {
        return $this->castInternal($value, $type);
    }
    if (!$type instanceof ReflectionUnionType) {
        return Result::fail();
    }
    foreach ($type->getTypes() as $t) {
        if (!$t instanceof ReflectionNamedType) {
            continue;
        }
        $result = $this->castInternal($value, $t);
        if ($result->isResolved()) {
            return $result;
        }
    }
    return Result::fail();
}