Skip to content

Commit 27b29f8

Browse files
authored
Merge pull request #148 from patchlevel/rename-union-object-normalizer-into-object-map-normalizer
rename union object normalizer into object map normalizer
2 parents 9a356a3 + c8a896d commit 27b29f8

5 files changed

Lines changed: 38 additions & 27 deletions

File tree

README.md

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -308,20 +308,30 @@ final class AnotherDto
308308
> [!WARNING]
309309
> Circular references are not supported and will result in an exception.
310310
311-
#### Union Object
311+
#### ObjectMap
312312

313-
If you have a union type from multiple classes, then you can use the `UnionObjectNormalizer` normalizer
313+
You can also use the `ObjectMapNormalizer` if you have either inheritance or a union type.
314314

315315
```php
316-
use Patchlevel\Hydrator\Normalizer\UnionObjectNormalizer;
316+
use Patchlevel\Hydrator\Normalizer\ObjectMapNormalizer;
317317

318-
#[UnionObjectNormalizer([
319-
ContentBlock::class => 'content',
320-
CodeBlock::class => 'code'
318+
// inheritance
319+
#[ObjectMapNormalizer([
320+
ContentBlock::class => 'content',
321+
CodeBlock::class => 'code'
321322
])]
322323
interface Block
323324
{
324325
}
326+
327+
// union type
328+
class ContentBlock implements Block {
329+
#[ObjectMapNormalizer([
330+
ContentA::class => 'content',
331+
ContentB::class => 'code'
332+
])]
333+
public ContentA|ContentB $content;
334+
}
325335
```
326336

327337
> [!NOTE]

phpstan-baseline.neon

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@ parameters:
8484
count: 1
8585
path: src/Normalizer/EnumNormalizer.php
8686

87+
-
88+
message: '#^Parameter \#2 \$data of method Patchlevel\\Hydrator\\Hydrator\:\:hydrate\(\) expects array\<string, mixed\>, array given\.$#'
89+
identifier: argument.type
90+
count: 1
91+
path: src/Normalizer/ObjectMapNormalizer.php
92+
8793
-
8894
message: '#^Parameter \#2 \$data of method Patchlevel\\Hydrator\\Hydrator\:\:hydrate\(\) expects array\<string, mixed\>, array\<mixed, mixed\> given\.$#'
8995
identifier: argument.type
@@ -102,12 +108,6 @@ parameters:
102108
count: 1
103109
path: src/Normalizer/ReflectionTypeUtil.php
104110

105-
-
106-
message: '#^Parameter \#2 \$data of method Patchlevel\\Hydrator\\Hydrator\:\:hydrate\(\) expects array\<string, mixed\>, array given\.$#'
107-
identifier: argument.type
108-
count: 1
109-
path: src/Normalizer/UnionObjectNormalizer.php
110-
111111
-
112112
message: '#^Method Patchlevel\\Hydrator\\Tests\\Unit\\Fixture\\DtoWithHooks\:\:postHydrate\(\) is unused\.$#'
113113
identifier: method.unused

phpstan.neon.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ parameters:
88
paths:
99
- src
1010
- tests
11+
reportUnmatchedIgnoredErrors: false
1112

1213
services:
1314
-
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
use function sprintf;
1818

1919
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_CLASS)]
20-
final class UnionObjectNormalizer implements Normalizer, HydratorAwareNormalizer
20+
final class ObjectMapNormalizer implements Normalizer, HydratorAwareNormalizer
2121
{
2222
private Hydrator|null $hydrator = null;
2323

tests/Unit/Normalizer/UnionObjectNormalizerTest.php renamed to tests/Unit/Normalizer/ObjectMapNormalizerTest.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use Patchlevel\Hydrator\Hydrator;
88
use Patchlevel\Hydrator\Normalizer\InvalidArgument;
99
use Patchlevel\Hydrator\Normalizer\MissingHydrator;
10-
use Patchlevel\Hydrator\Normalizer\UnionObjectNormalizer;
10+
use Patchlevel\Hydrator\Normalizer\ObjectMapNormalizer;
1111
use Patchlevel\Hydrator\Tests\Unit\Fixture\Email;
1212
use Patchlevel\Hydrator\Tests\Unit\Fixture\ProfileCreated;
1313
use Patchlevel\Hydrator\Tests\Unit\Fixture\ProfileId;
@@ -17,30 +17,30 @@
1717
use function serialize;
1818
use function unserialize;
1919

20-
#[CoversClass(UnionObjectNormalizer::class)]
21-
final class UnionObjectNormalizerTest extends TestCase
20+
#[CoversClass(ObjectMapNormalizer::class)]
21+
final class ObjectMapNormalizerTest extends TestCase
2222
{
2323
public function testNormalizeMissingHydrator(): void
2424
{
2525
$this->expectException(MissingHydrator::class);
2626

27-
$normalizer = new UnionObjectNormalizer([ProfileCreated::class => 'created']);
27+
$normalizer = new ObjectMapNormalizer([ProfileCreated::class => 'created']);
2828
$this->assertEquals(null, $normalizer->normalize(null));
2929
}
3030

3131
public function testDenormalizeMissingHydrator(): void
3232
{
3333
$this->expectException(MissingHydrator::class);
3434

35-
$normalizer = new UnionObjectNormalizer([ProfileCreated::class => 'created']);
35+
$normalizer = new ObjectMapNormalizer([ProfileCreated::class => 'created']);
3636
$this->assertEquals(null, $normalizer->denormalize(null));
3737
}
3838

3939
public function testNormalizeWithNull(): void
4040
{
4141
$hydrator = $this->createStub(Hydrator::class);
4242

43-
$normalizer = new UnionObjectNormalizer([ProfileCreated::class => 'created']);
43+
$normalizer = new ObjectMapNormalizer([ProfileCreated::class => 'created']);
4444
$normalizer->setHydrator($hydrator);
4545

4646
$this->assertEquals(null, $normalizer->normalize(null));
@@ -50,7 +50,7 @@ public function testDenormalizeWithNull(): void
5050
{
5151
$hydrator = $this->createStub(Hydrator::class);
5252

53-
$normalizer = new UnionObjectNormalizer([ProfileCreated::class => 'created']);
53+
$normalizer = new ObjectMapNormalizer([ProfileCreated::class => 'created']);
5454
$normalizer->setHydrator($hydrator);
5555

5656
$this->assertEquals(null, $normalizer->denormalize(null));
@@ -63,7 +63,7 @@ public function testNormalizeWithInvalidArgument(): void
6363

6464
$hydrator = $this->createStub(Hydrator::class);
6565

66-
$normalizer = new UnionObjectNormalizer([ProfileCreated::class => 'created']);
66+
$normalizer = new ObjectMapNormalizer([ProfileCreated::class => 'created']);
6767
$normalizer->setHydrator($hydrator);
6868
$normalizer->normalize('foo');
6969
}
@@ -75,7 +75,7 @@ public function testDenormalizeWithInvalidArgument(): void
7575

7676
$hydrator = $this->createStub(Hydrator::class);
7777

78-
$normalizer = new UnionObjectNormalizer([ProfileCreated::class => 'created']);
78+
$normalizer = new ObjectMapNormalizer([ProfileCreated::class => 'created']);
7979
$normalizer->setHydrator($hydrator);
8080
$normalizer->denormalize('foo');
8181
}
@@ -95,7 +95,7 @@ public function testNormalizeWithValue(): void
9595
->with($event)
9696
->willReturn(['profileId' => '1', 'email' => 'info@patchlevel.de']);
9797

98-
$normalizer = new UnionObjectNormalizer([ProfileCreated::class => 'created']);
98+
$normalizer = new ObjectMapNormalizer([ProfileCreated::class => 'created']);
9999
$normalizer->setHydrator($hydrator);
100100

101101
self::assertEquals(
@@ -119,7 +119,7 @@ public function testDenormalizeWithValue(): void
119119
->with(ProfileCreated::class, ['profileId' => '1', 'email' => 'info@patchlevel.de'])
120120
->willReturn($expected);
121121

122-
$normalizer = new UnionObjectNormalizer([ProfileCreated::class => 'created']);
122+
$normalizer = new ObjectMapNormalizer([ProfileCreated::class => 'created']);
123123
$normalizer->setHydrator($hydrator);
124124

125125
$this->assertEquals(
@@ -132,13 +132,13 @@ public function testSerialize(): void
132132
{
133133
$hydrator = $this->createStub(Hydrator::class);
134134

135-
$normalizer = new UnionObjectNormalizer([ProfileCreated::class => 'created']);
135+
$normalizer = new ObjectMapNormalizer([ProfileCreated::class => 'created']);
136136
$normalizer->setHydrator($hydrator);
137137

138138
$serialized = serialize($normalizer);
139139
$normalizer2 = unserialize($serialized);
140140

141-
self::assertInstanceOf(UnionObjectNormalizer::class, $normalizer2);
142-
self::assertEquals(new UnionObjectNormalizer([ProfileCreated::class => 'created']), $normalizer2);
141+
self::assertInstanceOf(ObjectMapNormalizer::class, $normalizer2);
142+
self::assertEquals(new ObjectMapNormalizer([ProfileCreated::class => 'created']), $normalizer2);
143143
}
144144
}

0 commit comments

Comments
 (0)