Skip to content

Commit

Permalink
v3.10
Browse files Browse the repository at this point in the history
  • Loading branch information
fenric committed May 23, 2024
1 parent 1baefad commit 7eb5501
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/TypeConverter/TimestampTypeConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Sunrise\Hydrator\AnnotationReaderAwareInterface;
use Sunrise\Hydrator\AnnotationReaderInterface;
use Sunrise\Hydrator\Dictionary\ContextKey;
use Sunrise\Hydrator\Exception\InvalidObjectException;
use Sunrise\Hydrator\Exception\InvalidValueException;
use Sunrise\Hydrator\Type;
use Sunrise\Hydrator\TypeConverterInterface;
Expand All @@ -34,6 +35,7 @@

use const FILTER_NULL_ON_FAILURE;
use const FILTER_VALIDATE_INT;
use const PHP_MAJOR_VERSION;

/**
* @since 3.1.0
Expand Down Expand Up @@ -76,6 +78,11 @@ public function castValue($value, Type $type, array $path, array $context): Gene
return;
}

// The DateTimeImmutable::createFromFormat method returns self instead of static...
if (PHP_MAJOR_VERSION === 7 && $className !== DateTimeImmutable::class) {
throw InvalidObjectException::unsupportedType($type);
}

// phpcs:ignore Generic.Files.LineLength
$format = $this->annotationReader->getAnnotations(Format::class, $type->getHolder())->current()->value ?? $context[ContextKey::TIMESTAMP_FORMAT] ?? self::DEFAULT_FORMAT;

Expand Down Expand Up @@ -110,14 +117,14 @@ public function castValue($value, Type $type, array $path, array $context): Gene
throw InvalidValueException::mustBeString($path);
}

/** @var int|string $value */
$value = (string) $value;

$timezone = null;
if (isset($context[ContextKey::TIMEZONE])) {
$timezone = new DateTimeZone($context[ContextKey::TIMEZONE]);
}

$timestamp = $className::createFromFormat($format, (string) $value, $timezone);
$timestamp = $className::createFromFormat($format, $value, $timezone);
if ($timestamp === false) {
throw InvalidValueException::invalidTimestamp($path, $format);
}
Expand Down
2 changes: 2 additions & 0 deletions tests/HydratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1864,6 +1864,8 @@ public function testHydrateTimestampPropertyWithoutValue(): void
// phpcs:ignore Generic.Files.LineLength
public function testHydrateOverriddenDateTimeImmutable(array $data, string $expected, ?string $format = null, ?string $timezone = null): void
{
$this->phpRequired('8.0');

$object = new class {
public Fixture\OverriddenDateTimeImmutable $value;
};
Expand Down

0 comments on commit 7eb5501

Please sign in to comment.