Skip to content

Commit

Permalink
v3.10
Browse files Browse the repository at this point in the history
  • Loading branch information
fenric committed May 22, 2024
1 parent 1316732 commit 1baefad
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 5 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
7 changes: 5 additions & 2 deletions src/TypeConverter/TimestampTypeConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use Sunrise\Hydrator\TypeConverterInterface;

use function filter_var;
use function is_a;
use function is_int;
use function is_string;
use function preg_replace;
Expand Down Expand Up @@ -69,7 +70,9 @@ 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;
}

Expand Down Expand Up @@ -114,7 +117,7 @@ public function castValue($value, Type $type, array $path, array $context): Gene
$timezone = new DateTimeZone($context[ContextKey::TIMEZONE]);
}

$timestamp = DateTimeImmutable::createFromFormat($format, (string) $value, $timezone);
$timestamp = $className::createFromFormat($format, (string) $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
{
}
17 changes: 17 additions & 0 deletions tests/HydratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1857,6 +1857,23 @@ 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
{
$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 1baefad

Please sign in to comment.