Final Class Yiisoft\Hydrator\Attribute\Parameter\ToDateTimeResolver
| Inheritance | Yiisoft\Hydrator\Attribute\Parameter\ToDateTimeResolver |
|---|---|
| Implements | Yiisoft\Hydrator\Attribute\Parameter\ParameterAttributeResolverInterface |
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __construct() | Yiisoft\Hydrator\Attribute\Parameter\ToDateTimeResolver | |
| getParameterValue() | Yiisoft\Hydrator\Attribute\Parameter\ToDateTimeResolver |
Method Details
| 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,
) {
}
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();
}
Signup or Login in order to comment.