Skip to content

Commit d9d2b32

Browse files
committed
feat: enable skipping of null-valued properties
1 parent 5238cd4 commit d9d2b32

File tree

5 files changed

+75
-1
lines changed

5 files changed

+75
-1
lines changed

phpunit.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
44
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/12.1/phpunit.xsd"
55
colors="true"
6+
displayDetailsOnAllIssues="true"
67
>
78
<php>
89
<ini name="display_errors" value="1"/>

src/Attribute/Attribute.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class Attribute
1111
{
1212
public function __construct(
1313
public ?string $name = null,
14+
public bool $ignoreIfNull = true,
1415
) {
1516
}
1617
}

src/Serializer/EntityNormalizer.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,10 @@ protected function normalizeAttributes(object $entity): array
338338
foreach ($propertyAttributes as $prop => $attr) {
339339
$reflectionProperty = $classMetadata->get($prop);
340340
$propertyValue = $reflectionProperty?->getValue($entity);
341-
$attributes[$attr->name ?: $prop] = $this->normalizer->normalize($propertyValue);
341+
342+
if (false === $attr->ignoreIfNull || null !== $propertyValue) {
343+
$attributes[$attr->name ?: $prop] = $this->normalizer->normalize($propertyValue);
344+
}
342345
}
343346

344347
return $attributes;
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace EduardoMarques\DynamoPHP\Tests\Integration\Serializer;
6+
7+
use DateTime;
8+
use EduardoMarques\DynamoPHP\Metadata\MetadataLoader;
9+
use EduardoMarques\DynamoPHP\Serializer\EntityNormalizer;
10+
use EduardoMarques\DynamoPHP\Tests\Integration\Stubs\EntityA;
11+
use PHPUnit\Framework\Attributes\Test;
12+
use PHPUnit\Framework\TestCase;
13+
use Symfony\Component\Serializer\Normalizer\BackedEnumNormalizer;
14+
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
15+
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
16+
use Symfony\Component\Serializer\Serializer;
17+
18+
final class EntityNormalizerTest extends TestCase
19+
{
20+
private MetadataLoader $metadataLoader;
21+
22+
private Serializer $serializer;
23+
24+
private EntityNormalizer $entityNormalizer;
25+
26+
#[Test]
27+
public function itReturnsNormalizedEntity(): void
28+
{
29+
$id = '8798b91f-fe8e-498c-8145-c757029346ef';
30+
$name = 'John Doe';
31+
$creationDate = new DateTime();
32+
$entity = new EntityA($id);
33+
$entity->name = $name;
34+
$entity->creationDate = $creationDate;
35+
36+
$expected = [
37+
'PK' => $id,
38+
'SK' => $creationDate->format('c'),
39+
'name' => $name,
40+
'type' => 'A',
41+
'createdAt' => $creationDate->format('c'),
42+
'birthDate' => null,
43+
'id' => $id,
44+
];
45+
46+
$this->assertSame($expected, $this->entityNormalizer->normalize($entity));
47+
}
48+
49+
protected function setUp(): void
50+
{
51+
parent::setUp();
52+
53+
$this->metadataLoader = new MetadataLoader();
54+
55+
$this->serializer = new Serializer([
56+
new BackedEnumNormalizer(),
57+
new DateTimeNormalizer(),
58+
new ObjectNormalizer(),
59+
]);
60+
61+
$this->entityNormalizer = new EntityNormalizer($this->metadataLoader, $this->serializer);
62+
}
63+
}

tests/Integration/Stubs/EntityA.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ final class EntityA
3030
#[Attribute(name: 'createdAt')]
3131
public DateTimeInterface $creationDate;
3232

33+
#[Attribute]
34+
public ?string $nationality = null;
35+
36+
#[Attribute(ignoreIfNull: false)]
37+
public ?DateTimeInterface $birthDate = null;
38+
3339
public function __construct(
3440
#[Attribute]
3541
protected string $id,

0 commit comments

Comments
 (0)