-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
allow to extend infer normalizerer with custom guesser
- Loading branch information
1 parent
ab7500a
commit c1aeeea
Showing
6 changed files
with
187 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Patchlevel\Hydrator\Guesser; | ||
|
||
use BackedEnum; | ||
use DateTime; | ||
use DateTimeImmutable; | ||
use DateTimeZone; | ||
use Patchlevel\Hydrator\Normalizer\DateTimeImmutableNormalizer; | ||
use Patchlevel\Hydrator\Normalizer\DateTimeNormalizer; | ||
use Patchlevel\Hydrator\Normalizer\DateTimeZoneNormalizer; | ||
use Patchlevel\Hydrator\Normalizer\EnumNormalizer; | ||
use Patchlevel\Hydrator\Normalizer\Normalizer; | ||
|
||
use function is_a; | ||
|
||
final class BuiltInGuesser implements Guesser | ||
{ | ||
/** @param class-string $className */ | ||
public function guess(string $className): Normalizer|null | ||
{ | ||
$normalizer = match ($className) { | ||
DateTimeImmutable::class => new DateTimeImmutableNormalizer(), | ||
DateTime::class => new DateTimeNormalizer(), | ||
DateTimeZone::class => new DateTimeZoneNormalizer(), | ||
default => null, | ||
}; | ||
|
||
if ($normalizer) { | ||
return $normalizer; | ||
} | ||
|
||
if (is_a($className, BackedEnum::class, true)) { | ||
return new EnumNormalizer($className); | ||
} | ||
|
||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Patchlevel\Hydrator\Guesser; | ||
|
||
use Patchlevel\Hydrator\Normalizer\Normalizer; | ||
|
||
interface Guesser | ||
{ | ||
/** @param class-string $className */ | ||
public function guess(string $className): Normalizer|null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Patchlevel\Hydrator\Tests\Unit\Guesser; | ||
|
||
use DateTime; | ||
use DateTimeImmutable; | ||
use DateTimeZone; | ||
use Patchlevel\Hydrator\Guesser\BuiltInGuesser; | ||
use Patchlevel\Hydrator\Normalizer\DateTimeImmutableNormalizer; | ||
use Patchlevel\Hydrator\Normalizer\DateTimeNormalizer; | ||
use Patchlevel\Hydrator\Normalizer\DateTimeZoneNormalizer; | ||
use Patchlevel\Hydrator\Normalizer\EnumNormalizer; | ||
use Patchlevel\Hydrator\Tests\Unit\Fixture\Email; | ||
use Patchlevel\Hydrator\Tests\Unit\Fixture\Status; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class BuiltInGuesserTest extends TestCase | ||
{ | ||
public function testNoMatch(): void | ||
{ | ||
$guesser = new BuiltInGuesser(); | ||
self::assertNull($guesser->guess(Email::class)); | ||
} | ||
|
||
public function testEnum(): void | ||
{ | ||
$guesser = new BuiltInGuesser(); | ||
self::assertInstanceOf( | ||
EnumNormalizer::class, | ||
$guesser->guess(Status::class), | ||
); | ||
} | ||
|
||
public function testDateTimeImmutable(): void | ||
{ | ||
$guesser = new BuiltInGuesser(); | ||
self::assertInstanceOf( | ||
DateTimeImmutableNormalizer::class, | ||
$guesser->guess(DateTimeImmutable::class), | ||
); | ||
} | ||
|
||
public function testDateTime(): void | ||
{ | ||
$guesser = new BuiltInGuesser(); | ||
self::assertInstanceOf( | ||
DateTimeNormalizer::class, | ||
$guesser->guess(DateTime::class), | ||
); | ||
} | ||
|
||
public function testDateTimeZone(): void | ||
{ | ||
$guesser = new BuiltInGuesser(); | ||
self::assertInstanceOf( | ||
DateTimeZoneNormalizer::class, | ||
$guesser->guess(DateTimeZone::class), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters