0 follower

Final Class Yiisoft\Hydrator\Attribute\Parameter\ToDateTimeResolver

InheritanceYiisoft\Hydrator\Attribute\Parameter\ToDateTimeResolver
ImplementsYiisoft\Hydrator\Attribute\Parameter\ParameterAttributeResolverInterface

Method Details

Hide inherited methods

__construct() public method

public mixed __construct ( string|null $format null, integer $dateType IntlDateFormatter::SHORT, integer $timeType IntlDateFormatter::SHORT, string|null $timeZone null, string|null $locale null )
$format string|null
$dateType integer
$timeType integer
$timeZone string|null
$locale string|null

                public function __construct(
    private readonly ?string $format = null,
    private readonly int $dateType = IntlDateFormatter::SHORT,
    private readonly int $timeType = IntlDateFormatter::SHORT,
    private readonly ?string $timeZone = null,
    private readonly ?string $locale = null,
) {
}

            
getParameterValue() public method

public Yiisoft\Hydrator\Result getParameterValue ( Yiisoft\Hydrator\Attribute\Parameter\ParameterAttributeInterface $attribute, Yiisoft\Hydrator\AttributeHandling\ParameterAttributeResolveContext $context )
$attribute Yiisoft\Hydrator\Attribute\Parameter\ParameterAttributeInterface
$context Yiisoft\Hydrator\AttributeHandling\ParameterAttributeResolveContext

                public function getParameterValue(
    ParameterAttributeInterface $attribute,
    ParameterAttributeResolveContext $context
): Result {
    if (!$attribute instanceof ToDateTime) {
        throw new UnexpectedAttributeException(ToDateTime::class, $attribute);
    }
    if (!$context->isResolved()) {
        return Result::fail();
    }
    $resolvedValue = $context->getResolvedValue();
    $shouldBeMutable = $this->shouldResultBeMutable($context);
    if ($resolvedValue instanceof DateTimeInterface) {
        return $this->createSuccessResult($resolvedValue, $shouldBeMutable);
    }
    $timeZone = $attribute->timeZone ?? $this->timeZone;
    if ($timeZone !== null) {
        $timeZone = new DateTimeZone($timeZone);
    }
    if (is_int($resolvedValue)) {
        return Result::success(
            $this->makeDateTimeFromTimestamp($resolvedValue, $timeZone, $shouldBeMutable)
        );
    }
    if (is_string($resolvedValue) && !empty($resolvedValue)) {
        $format = $attribute->format ?? $this->format;
        if (is_string($format) && str_starts_with($format, 'php:')) {
            return $this->parseWithPhpFormat($resolvedValue, substr($format, 4), $timeZone, $shouldBeMutable);
        }
        return $this->parseWithIntlFormat(
            $resolvedValue,
            $format,
            $attribute->dateType ?? $this->dateType,
            $attribute->timeType ?? $this->timeType,
            $timeZone,
            $attribute->locale ?? $this->locale,
            $shouldBeMutable,
        );
    }
    return Result::fail();
}