0 follower

Final Class Yiisoft\Hydrator\TypeCaster\NullTypeCaster

InheritanceYiisoft\Hydrator\TypeCaster\NullTypeCaster
ImplementsYiisoft\Hydrator\TypeCaster\TypeCasterInterface

Configurable type caster for casting value to null.

Method Details

Hide inherited methods

__construct() public method

public mixed __construct ( boolean $null true, boolean $emptyString false, boolean $emptyArray false )
$null boolean
$emptyString boolean
$emptyArray boolean

                public function __construct(
    private bool $null = true,
    private bool $emptyString = false,
    private bool $emptyArray = false,
) {
}

            
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
{
    if (!$this->isAllowNull($context->getReflectionType())) {
        return Result::fail();
    }
    if (
        ($this->null && $value === null)
        || ($this->emptyString && $value === '')
        || ($this->emptyArray && $value === [])
    ) {
        return Result::success(null);
    }
    return Result::fail();
}