File tree Expand file tree Collapse file tree 3 files changed +28
-4
lines changed
src/FreeElephants/JsonApi/DTO
tests/FreeElephants/JsonApi/DTO Expand file tree Collapse file tree 3 files changed +28
-4
lines changed Original file line number Diff line number Diff line change @@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6
6
7
7
## [ Unreleased]
8
8
9
+ ## [ 0.0.3] - 2025-03-18
10
+
11
+ ### Added
12
+ - Handle nullable objects
13
+
9
14
## [ 0.0.2] - 2025-03-14
10
15
11
16
### Added
@@ -17,6 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
17
22
### Added
18
23
- Extract all DTO types from FreeElephants/json-api-php-toolkit to this project
19
24
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
21
27
[ 0.0.2 ] : https://github.com/FreeElephants/json-api-dto/releases/tag/0.0.2
22
28
[ 0.0.1 ] : https://github.com/FreeElephants/json-api-dto/releases/tag/0.0.1
Original file line number Diff line number Diff line change @@ -12,8 +12,12 @@ public function __construct(array $attributes)
12
12
if ($ property ->hasType ()) {
13
13
$ propertyType = $ property ->getType ();
14
14
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
+ }
17
21
}
18
22
}
19
23
$ this ->{$ name } = $ value ;
Original file line number Diff line number Diff line change @@ -23,7 +23,10 @@ public function testFromRequest()
23
23
"someNestedStructure": {
24
24
"someKey": "someValue"
25
25
}
26
- }
26
+ },
27
+ "nullableObjectField": null,
28
+ "nullableScalarField": null,
29
+ "nullableScalarFilledField": "baz"
27
30
},
28
31
"relationships": {
29
32
"baz": {
@@ -47,6 +50,9 @@ public function testFromRequest()
47
50
$ this ->assertEquals (new \DateTime ('2012-04-23T18:25:43.511Z ' ), $ fooDTO ->data ->attributes ->date );
48
51
$ this ->assertSame ('someValue ' , $ fooDTO ->data ->attributes ->nested ->someNestedStructure ->someKey );
49
52
$ 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 );
50
56
}
51
57
}
52
58
@@ -66,6 +72,14 @@ class FooAttributes extends AbstractAttributes
66
72
public string $ foo ;
67
73
public \DateTime $ date ;
68
74
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 ;
69
83
}
70
84
71
85
class FooRelationships extends AbstractRelationships
You can’t perform that action at this time.
0 commit comments