From c2f5eef3370ea6922792e49248c4f57aa0eb55e1 Mon Sep 17 00:00:00 2001 From: "n.gnato" Date: Tue, 18 Mar 2025 15:43:23 +0300 Subject: [PATCH] Support nullable attributes --- CHANGELOG.md | 8 +++++++- .../JsonApi/DTO/BaseKeyValueStructure.php | 8 ++++++-- tests/FreeElephants/JsonApi/DTO/DocumentTest.php | 16 +++++++++++++++- 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5de9783..fbb7821 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [0.0.3] - 2025-03-18 + +### Added +- Handle nullable objects + ## [0.0.2] - 2025-03-14 ### Added @@ -17,6 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - Extract all DTO types from FreeElephants/json-api-php-toolkit to this project -[Unreleased]: https://github.com/FreeElephants/json-api-dto/compare/0.0.2...HEAD +[Unreleased]: https://github.com/FreeElephants/json-api-dto/compare/0.0.3...HEAD +[0.0.3]: https://github.com/FreeElephants/json-api-dto/releases/tag/0.0.3 [0.0.2]: https://github.com/FreeElephants/json-api-dto/releases/tag/0.0.2 [0.0.1]: https://github.com/FreeElephants/json-api-dto/releases/tag/0.0.1 diff --git a/src/FreeElephants/JsonApi/DTO/BaseKeyValueStructure.php b/src/FreeElephants/JsonApi/DTO/BaseKeyValueStructure.php index 3e71a2e..42e87ce 100644 --- a/src/FreeElephants/JsonApi/DTO/BaseKeyValueStructure.php +++ b/src/FreeElephants/JsonApi/DTO/BaseKeyValueStructure.php @@ -12,8 +12,12 @@ public function __construct(array $attributes) if ($property->hasType()) { $propertyType = $property->getType(); if ($propertyType instanceof \ReflectionNamedType && !$propertyType->isBuiltin()) { - $propertyClassName = $propertyType->getName(); - $value = new $propertyClassName($value); + if($propertyType->allowsNull() && is_null($value)) { + $value = null; + } else { + $propertyClassName = $propertyType->getName(); + $value = new $propertyClassName($value); + } } } $this->{$name} = $value; diff --git a/tests/FreeElephants/JsonApi/DTO/DocumentTest.php b/tests/FreeElephants/JsonApi/DTO/DocumentTest.php index 5e2e60d..9415fd3 100644 --- a/tests/FreeElephants/JsonApi/DTO/DocumentTest.php +++ b/tests/FreeElephants/JsonApi/DTO/DocumentTest.php @@ -23,7 +23,10 @@ public function testFromRequest() "someNestedStructure": { "someKey": "someValue" } - } + }, + "nullableObjectField": null, + "nullableScalarField": null, + "nullableScalarFilledField": "baz" }, "relationships": { "baz": { @@ -47,6 +50,9 @@ public function testFromRequest() $this->assertEquals(new \DateTime('2012-04-23T18:25:43.511Z'), $fooDTO->data->attributes->date); $this->assertSame('someValue', $fooDTO->data->attributes->nested->someNestedStructure->someKey); $this->assertSame('baz-id', $fooDTO->data->relationships->baz->data->id); + $this->assertNull($fooDTO->data->attributes->nullableObjectField); + $this->assertNull($fooDTO->data->attributes->nullableScalarField); + $this->assertSame('baz', $fooDTO->data->attributes->nullableScalarFilledField); } } @@ -66,6 +72,14 @@ class FooAttributes extends AbstractAttributes public string $foo; public \DateTime $date; public Nested $nested; + public ?NullableObjectAttribute $nullableObjectField; + public ?string $nullableScalarField; + public ?string $nullableScalarFilledField; +} + +class NullableObjectAttribute +{ + public string $someField; } class FooRelationships extends AbstractRelationships