Skip to content

Commit

Permalink
Merge pull request #37 from sunrise-php/release/v3.10.0
Browse files Browse the repository at this point in the history
v3.10
  • Loading branch information
fenric authored May 23, 2024
2 parents 1316732 + 7eb5501 commit f122aab
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 6 deletions.
2 changes: 1 addition & 1 deletion psalm.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
phpVersion="8.2"
phpVersion="8.3"
>
<projectFiles>
<directory name="src" />
Expand Down
4 changes: 2 additions & 2 deletions src/TypeConverter/BooleanTypeConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
use function trim;

use const FILTER_NULL_ON_FAILURE;
use const FILTER_VALIDATE_BOOL;
use const FILTER_VALIDATE_BOOLEAN;

/**
* @since 3.1.0
Expand Down Expand Up @@ -55,7 +55,7 @@ public function castValue($value, Type $type, array $path, array $context): Gene
}

// https://github.com/php/php-src/blob/b7d90f09d4a1688f2692f2fa9067d0a07f78cc7d/ext/filter/logical_filters.c#L273
$value = filter_var($value, FILTER_VALIDATE_BOOL, FILTER_NULL_ON_FAILURE);
$value = filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
}

if (!is_bool($value)) {
Expand Down
16 changes: 13 additions & 3 deletions src/TypeConverter/TimestampTypeConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,21 @@
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;

use function filter_var;
use function is_a;
use function is_int;
use function is_string;
use function preg_replace;
use function trim;

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

/**
* @since 3.1.0
Expand Down Expand Up @@ -69,10 +72,17 @@ public function castValue($value, Type $type, array $path, array $context): Gene
{
/** @var array{timestamp_format?: string, timezone?: string} $context */

if ($type->getName() <> DateTimeImmutable::class) {
$className = $type->getName();

if (!is_a($className, DateTimeImmutable::class, true)) {
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 @@ -107,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 = DateTimeImmutable::createFromFormat($format, (string) $value, $timezone);
$timestamp = $className::createFromFormat($format, $value, $timezone);
if ($timestamp === false) {
throw InvalidValueException::invalidTimestamp($path, $format);
}
Expand Down
11 changes: 11 additions & 0 deletions tests/Fixture/OverriddenDateTimeImmutable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Sunrise\Hydrator\Tests\Fixture;

use DateTimeImmutable;

final class OverriddenDateTimeImmutable extends DateTimeImmutable
{
}
19 changes: 19 additions & 0 deletions tests/HydratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1857,6 +1857,25 @@ public function testHydrateTimestampPropertyWithoutValue(): void
$this->createHydrator()->hydrate($object, []);
}

/**
* @group timestamp
* @dataProvider timestampDataProvider
*/
// 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;
};

$this->assertInvalidValueExceptionCount(0);
// phpcs:ignore Generic.Files.LineLength
$this->createHydrator([ContextKey::TIMESTAMP_FORMAT => $format, ContextKey::TIMEZONE => $timezone])->hydrate($object, $data);
$this->assertSame($expected, $object->value->format($format ?? TimestampTypeConverter::DEFAULT_FORMAT));
}

/**
* @group timezone
* @dataProvider timezoneDataProvider
Expand Down

0 comments on commit f122aab

Please sign in to comment.