-
Notifications
You must be signed in to change notification settings - Fork 2
add union object normalizer #40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
DavidBadura
wants to merge
1
commit into
1.3.x
Choose a base branch
from
union-object-normalizer
base: 1.3.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,121 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Patchlevel\Hydrator\Normalizer; | ||
|
|
||
| use Attribute; | ||
| use Patchlevel\Hydrator\Hydrator; | ||
|
|
||
| use function array_flip; | ||
| use function array_key_exists; | ||
| use function array_keys; | ||
| use function implode; | ||
| use function is_array; | ||
| use function is_object; | ||
| use function is_string; | ||
| use function sprintf; | ||
|
|
||
| #[Attribute(Attribute::TARGET_PROPERTY)] | ||
| final class UnionObjectNormalizer implements Normalizer, HydratorAwareNormalizer | ||
| { | ||
| private Hydrator|null $hydrator = null; | ||
|
|
||
| /** @var array<string, class-string> */ | ||
| private array $typeToClassMap; | ||
|
|
||
| /** @param array<class-string, string> $classToTypeMap */ | ||
| public function __construct( | ||
| private readonly array|null $classToTypeMap = null, | ||
| private readonly string $typeFieldName = '_type', | ||
| ) { | ||
| $this->typeToClassMap = array_flip($classToTypeMap); | ||
| } | ||
|
|
||
| public function setHydrator(Hydrator $hydrator): void | ||
| { | ||
| $this->hydrator = $hydrator; | ||
| } | ||
|
|
||
| public function normalize(mixed $value): mixed | ||
| { | ||
| if (!$this->hydrator) { | ||
| throw new MissingHydrator(); | ||
| } | ||
|
|
||
| if ($value === null) { | ||
| return null; | ||
| } | ||
|
|
||
| if (!is_object($value)) { | ||
| throw InvalidArgument::withWrongType( | ||
| sprintf('%s|null', implode('|', array_keys($this->classToTypeMap))), | ||
| $value, | ||
| ); | ||
| } | ||
|
|
||
| if (!array_key_exists($value::class, $this->classToTypeMap)) { | ||
| throw InvalidArgument::withWrongType( | ||
| sprintf('%s|null', implode('|', array_keys($this->classToTypeMap))), | ||
| $value, | ||
| ); | ||
| } | ||
|
|
||
| $data = $this->hydrator->extract($value); | ||
| $data[$this->typeFieldName] = $this->classToTypeMap[$value::class]; | ||
|
|
||
| return $data; | ||
| } | ||
|
|
||
| public function denormalize(mixed $value): mixed | ||
| { | ||
| if (!$this->hydrator) { | ||
| throw new MissingHydrator(); | ||
| } | ||
|
|
||
| if ($value === null) { | ||
| return null; | ||
| } | ||
|
|
||
| if (!is_array($value)) { | ||
| throw InvalidArgument::withWrongType('array<string, mixed>|null', $value); | ||
| } | ||
|
|
||
| if (!array_key_exists($this->typeFieldName, $value)) { | ||
| throw new InvalidArgument(sprintf('missing type field "%s"', $this->typeFieldName)); | ||
| } | ||
|
|
||
| $type = $value[$this->typeFieldName]; | ||
|
|
||
| if (!is_string($type)) { | ||
| throw InvalidArgument::withWrongType('string', $type); | ||
| } | ||
|
|
||
| if (!array_key_exists($type, $this->typeToClassMap)) { | ||
| throw new InvalidArgument(sprintf('unknown type "%s"', $type)); | ||
| } | ||
|
|
||
| $className = $this->typeToClassMap[$type]; | ||
| unset($value[$this->typeFieldName]); | ||
|
|
||
| return $this->hydrator->hydrate($className, $value); | ||
| } | ||
|
|
||
| /** | ||
| * @return array{ | ||
| * typeToClassMap: array<string, class-string>, | ||
| * classToTypeMap: array<class-string, string>, | ||
| * typeFieldName: string, | ||
| * hydrator: null | ||
| * } | ||
| */ | ||
| public function __serialize(): array | ||
| { | ||
| return [ | ||
| 'typeToClassMap' => $this->typeToClassMap, | ||
| 'classToTypeMap' => $this->classToTypeMap, | ||
| 'typeFieldName' => $this->typeFieldName, | ||
| 'hydrator' => null, | ||
| ]; | ||
| } | ||
| } | ||
This file contains hidden or 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,143 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Patchlevel\Hydrator\Tests\Unit\Normalizer; | ||
|
|
||
| use Attribute; | ||
| use Patchlevel\Hydrator\Hydrator; | ||
| use Patchlevel\Hydrator\Normalizer\InvalidArgument; | ||
| use Patchlevel\Hydrator\Normalizer\MissingHydrator; | ||
| use Patchlevel\Hydrator\Normalizer\UnionObjectNormalizer; | ||
| use Patchlevel\Hydrator\Tests\Unit\Fixture\Email; | ||
| use Patchlevel\Hydrator\Tests\Unit\Fixture\ProfileCreated; | ||
| use Patchlevel\Hydrator\Tests\Unit\Fixture\ProfileId; | ||
| use PHPUnit\Framework\TestCase; | ||
| use Prophecy\PhpUnit\ProphecyTrait; | ||
|
|
||
| use function serialize; | ||
| use function unserialize; | ||
|
|
||
| #[Attribute(Attribute::TARGET_PROPERTY)] | ||
| final class UnionObjectNormalizerTest extends TestCase | ||
| { | ||
| use ProphecyTrait; | ||
|
|
||
| public function testNormalizeMissingHydrator(): void | ||
| { | ||
| $this->expectException(MissingHydrator::class); | ||
|
|
||
| $normalizer = new UnionObjectNormalizer([ProfileCreated::class => 'created']); | ||
| $this->assertEquals(null, $normalizer->normalize(null)); | ||
| } | ||
|
|
||
| public function testDenormalizeMissingHydrator(): void | ||
| { | ||
| $this->expectException(MissingHydrator::class); | ||
|
|
||
| $normalizer = new UnionObjectNormalizer([ProfileCreated::class => 'created']); | ||
| $this->assertEquals(null, $normalizer->denormalize(null)); | ||
| } | ||
|
|
||
| public function testNormalizeWithNull(): void | ||
| { | ||
| $hydrator = $this->prophesize(Hydrator::class); | ||
|
|
||
| $normalizer = new UnionObjectNormalizer([ProfileCreated::class => 'created']); | ||
| $normalizer->setHydrator($hydrator->reveal()); | ||
|
|
||
| $this->assertEquals(null, $normalizer->normalize(null)); | ||
| } | ||
|
|
||
| public function testDenormalizeWithNull(): void | ||
| { | ||
| $hydrator = $this->prophesize(Hydrator::class); | ||
|
|
||
| $normalizer = new UnionObjectNormalizer([ProfileCreated::class => 'created']); | ||
| $normalizer->setHydrator($hydrator->reveal()); | ||
|
|
||
| $this->assertEquals(null, $normalizer->denormalize(null)); | ||
| } | ||
|
|
||
| public function testNormalizeWithInvalidArgument(): void | ||
| { | ||
| $this->expectException(InvalidArgument::class); | ||
| $this->expectExceptionMessage('type "Patchlevel\Hydrator\Tests\Unit\Fixture\ProfileCreated|null" was expected but "string" was passed.'); | ||
|
|
||
| $hydrator = $this->prophesize(Hydrator::class); | ||
|
|
||
| $normalizer = new UnionObjectNormalizer([ProfileCreated::class => 'created']); | ||
| $normalizer->setHydrator($hydrator->reveal()); | ||
| $normalizer->normalize('foo'); | ||
| } | ||
|
|
||
| public function testDenormalizeWithInvalidArgument(): void | ||
| { | ||
| $this->expectException(InvalidArgument::class); | ||
| $this->expectExceptionMessage('array<string, mixed>|null" was expected but "string" was passed.'); | ||
|
|
||
| $hydrator = $this->prophesize(Hydrator::class); | ||
|
|
||
| $normalizer = new UnionObjectNormalizer([ProfileCreated::class => 'created']); | ||
| $normalizer->setHydrator($hydrator->reveal()); | ||
| $normalizer->denormalize('foo'); | ||
| } | ||
|
|
||
| public function testNormalizeWithValue(): void | ||
| { | ||
| $hydrator = $this->prophesize(Hydrator::class); | ||
|
|
||
| $event = new ProfileCreated( | ||
| ProfileId::fromString('1'), | ||
| Email::fromString('info@patchlevel.de'), | ||
| ); | ||
|
|
||
| $hydrator->extract($event) | ||
| ->willReturn(['profileId' => '1', 'email' => 'info@patchlevel.de']) | ||
| ->shouldBeCalledOnce(); | ||
|
|
||
| $normalizer = new UnionObjectNormalizer([ProfileCreated::class => 'created']); | ||
| $normalizer->setHydrator($hydrator->reveal()); | ||
|
|
||
| self::assertEquals( | ||
| $normalizer->normalize($event), | ||
| ['profileId' => '1', 'email' => 'info@patchlevel.de', '_type' => 'created'], | ||
| ); | ||
| } | ||
|
|
||
| public function testDenormalizeWithValue(): void | ||
| { | ||
| $hydrator = $this->prophesize(Hydrator::class); | ||
|
|
||
| $expected = new ProfileCreated( | ||
| ProfileId::fromString('1'), | ||
| Email::fromString('info@patchlevel.de'), | ||
| ); | ||
|
|
||
| $hydrator->hydrate(ProfileCreated::class, ['profileId' => '1', 'email' => 'info@patchlevel.de']) | ||
| ->willReturn($expected) | ||
| ->shouldBeCalledOnce(); | ||
|
|
||
| $normalizer = new UnionObjectNormalizer([ProfileCreated::class => 'created']); | ||
| $normalizer->setHydrator($hydrator->reveal()); | ||
|
|
||
| $this->assertEquals( | ||
| $expected, | ||
| $normalizer->denormalize(['profileId' => '1', 'email' => 'info@patchlevel.de', '_type' => 'created']), | ||
| ); | ||
| } | ||
|
|
||
| public function testSerialize(): void | ||
| { | ||
| $hydrator = $this->prophesize(Hydrator::class); | ||
|
|
||
| $normalizer = new UnionObjectNormalizer([ProfileCreated::class => 'created']); | ||
| $normalizer->setHydrator($hydrator->reveal()); | ||
|
|
||
| $serialized = serialize($normalizer); | ||
| $normalizer2 = unserialize($serialized); | ||
|
|
||
| self::assertInstanceOf(UnionObjectNormalizer::class, $normalizer2); | ||
| self::assertEquals(new UnionObjectNormalizer([ProfileCreated::class => 'created']), $normalizer2); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's not the type but instead the type key right?