Final Class Yiisoft\Hydrator\TypeCaster\EnumTypeCaster
| Inheritance | Yiisoft\Hydrator\TypeCaster\EnumTypeCaster |
|---|---|
| Implements | Yiisoft\Hydrator\TypeCaster\TypeCasterInterface |
Casts values to enumerations.
Public Methods
| Method | Description | Defined By |
|---|---|---|
| cast() | Yiisoft\Hydrator\TypeCaster\EnumTypeCaster |
Method Details
| 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();
}
Signup or Login in order to comment.