Skip to content

Commit cfe6122

Browse files
authored
Merge pull request #3 from FreeElephants/handle-nullable-values
Support nullable attributes
2 parents 0ca6307 + c2f5eef commit cfe6122

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
## [0.0.3] - 2025-03-18
10+
11+
### Added
12+
- Handle nullable objects
13+
914
## [0.0.2] - 2025-03-14
1015

1116
### Added
@@ -17,6 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1722
### Added
1823
- Extract all DTO types from FreeElephants/json-api-php-toolkit to this project
1924

20-
[Unreleased]: https://github.com/FreeElephants/json-api-dto/compare/0.0.2...HEAD
25+
[Unreleased]: https://github.com/FreeElephants/json-api-dto/compare/0.0.3...HEAD
26+
[0.0.3]: https://github.com/FreeElephants/json-api-dto/releases/tag/0.0.3
2127
[0.0.2]: https://github.com/FreeElephants/json-api-dto/releases/tag/0.0.2
2228
[0.0.1]: https://github.com/FreeElephants/json-api-dto/releases/tag/0.0.1

src/FreeElephants/JsonApi/DTO/BaseKeyValueStructure.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,12 @@ public function __construct(array $attributes)
1212
if ($property->hasType()) {
1313
$propertyType = $property->getType();
1414
if ($propertyType instanceof \ReflectionNamedType && !$propertyType->isBuiltin()) {
15-
$propertyClassName = $propertyType->getName();
16-
$value = new $propertyClassName($value);
15+
if($propertyType->allowsNull() && is_null($value)) {
16+
$value = null;
17+
} else {
18+
$propertyClassName = $propertyType->getName();
19+
$value = new $propertyClassName($value);
20+
}
1721
}
1822
}
1923
$this->{$name} = $value;

tests/FreeElephants/JsonApi/DTO/DocumentTest.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ public function testFromRequest()
2323
"someNestedStructure": {
2424
"someKey": "someValue"
2525
}
26-
}
26+
},
27+
"nullableObjectField": null,
28+
"nullableScalarField": null,
29+
"nullableScalarFilledField": "baz"
2730
},
2831
"relationships": {
2932
"baz": {
@@ -47,6 +50,9 @@ public function testFromRequest()
4750
$this->assertEquals(new \DateTime('2012-04-23T18:25:43.511Z'), $fooDTO->data->attributes->date);
4851
$this->assertSame('someValue', $fooDTO->data->attributes->nested->someNestedStructure->someKey);
4952
$this->assertSame('baz-id', $fooDTO->data->relationships->baz->data->id);
53+
$this->assertNull($fooDTO->data->attributes->nullableObjectField);
54+
$this->assertNull($fooDTO->data->attributes->nullableScalarField);
55+
$this->assertSame('baz', $fooDTO->data->attributes->nullableScalarFilledField);
5056
}
5157
}
5258

@@ -66,6 +72,14 @@ class FooAttributes extends AbstractAttributes
6672
public string $foo;
6773
public \DateTime $date;
6874
public Nested $nested;
75+
public ?NullableObjectAttribute $nullableObjectField;
76+
public ?string $nullableScalarField;
77+
public ?string $nullableScalarFilledField;
78+
}
79+
80+
class NullableObjectAttribute
81+
{
82+
public string $someField;
6983
}
7084

7185
class FooRelationships extends AbstractRelationships

0 commit comments

Comments
 (0)