-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
92 additions
and
1 deletion.
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,51 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Premier\Enum\Symfony; | ||
|
||
use Premier\Enum\Enum; | ||
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; | ||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; | ||
use function is_int; | ||
use function is_subclass_of; | ||
|
||
final class EnumNormalizer implements NormalizerInterface, DenormalizerInterface | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function denormalize($data, string $type, string $format = null, array $context = []): Enum | ||
{ | ||
/** @var callable $callable */ | ||
$callable = [$type, 'create']; | ||
|
||
return $callable($data); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function supportsDenormalization($data, string $type, string $format = null): bool | ||
{ | ||
return is_int($data) && is_subclass_of($type, Enum::class); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function normalize($object, string $format = null, array $context = []): int | ||
{ | ||
/** @var Enum $object */ | ||
|
||
return $object->toId(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function supportsNormalization($data, string $format = null): bool | ||
{ | ||
return $data instanceof Enum; | ||
} | ||
} |
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,39 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Premier\Enum\Tests\Symfony; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Premier\Enum\Enum; | ||
use Premier\Enum\Gender; | ||
use Premier\Enum\Symfony\EnumNormalizer; | ||
use Generator; | ||
|
||
final class EnumSerializerTest extends TestCase | ||
{ | ||
/** | ||
* @dataProvider enums | ||
*/ | ||
public function testNormalize(Enum $enum, int $expected): void | ||
{ | ||
self::assertSame($expected, (new EnumNormalizer())->normalize($enum)); | ||
} | ||
|
||
/** | ||
* @dataProvider enums | ||
*/ | ||
public function testDenormalize(Enum $expected, int $data): void | ||
{ | ||
self::assertSame($expected, (new EnumNormalizer())->denormalize($data, $expected::class)); | ||
} | ||
|
||
public function enums(): Generator | ||
{ | ||
$male = Gender::male(); | ||
$female = Gender::female(); | ||
|
||
yield [$male, $male->toId()]; | ||
yield [$female, $female->toId()]; | ||
} | ||
} |