File tree Expand file tree Collapse file tree 8 files changed +96
-10
lines changed
src/FreeElephants/JsonApi/DTO
tests/FreeElephants/JsonApi/DTO Expand file tree Collapse file tree 8 files changed +96
-10
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.6] - 2025-04-05
10
+
11
+ ### Fixed
12
+ - Handle Union Types for ` data ` and ` attributes ` fields.
13
+
9
14
## [ 0.0.5] - 2025-03-24
10
15
11
16
### Fixed
@@ -32,7 +37,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
32
37
### Added
33
38
- Extract all DTO types from FreeElephants/json-api-php-toolkit to this project
34
39
35
- [ Unreleased ] : https://github.com/FreeElephants/json-api-dto/compare/0.0.5...HEAD
40
+ [ Unreleased ] : https://github.com/FreeElephants/json-api-dto/compare/0.0.6...HEAD
41
+ [ 0.0.6 ] : https://github.com/FreeElephants/json-api-dto/releases/tag/0.0.6
36
42
[ 0.0.5 ] : https://github.com/FreeElephants/json-api-dto/releases/tag/0.0.5
37
43
[ 0.0.4 ] : https://github.com/FreeElephants/json-api-dto/releases/tag/0.0.4
38
44
[ 0.0.3 ] : https://github.com/FreeElephants/json-api-dto/releases/tag/0.0.3
Original file line number Diff line number Diff line change 2
2
3
3
namespace FreeElephants \JsonApi \DTO ;
4
4
5
- use Psr \Http \Message \MessageInterface ;
6
-
7
5
/**
8
6
* @property AbstractResourceObject|mixed $data
9
7
*/
@@ -13,9 +11,25 @@ final public function __construct(array $payload)
13
11
{
14
12
$ concreteClass = new \ReflectionClass ($ this );
15
13
$ dataProperty = $ concreteClass ->getProperty ('data ' );
16
- /** @var \ReflectionNamedType $reflectionType */
14
+
17
15
$ reflectionType = $ dataProperty ->getType ();
18
- $ dataClassName = $ reflectionType ->getName ();
16
+ if ($ reflectionType instanceof \ReflectionNamedType) {
17
+ $ dataClassName = $ reflectionType ->getName ();
18
+ } else {
19
+ /** @var \ReflectionUnionType $reflectionType */
20
+ $ dataClassName = $ reflectionType ->getTypes ()[0 ]->getName ();
21
+ }
22
+
23
+ /**
24
+ * In cases like:
25
+ * `public array|Example\ResourceObjectExt|Example\ResourceObject $data;`
26
+ * ReflectionUnionType::getTypes() return types in next orders:
27
+ * - Example\ResourceObjectExt
28
+ * - Example\ResourceObject
29
+ * - array
30
+ *
31
+ * This exception is [can] not covered with test. But this behavior not documented at https://www.php.net/manual/en/reflectionuniontype.gettypes.php
32
+ */
19
33
if ($ dataClassName !== 'array ' ) {
20
34
$ data = new $ dataClassName ($ payload ['data ' ]);
21
35
} else {
Original file line number Diff line number Diff line change @@ -15,14 +15,19 @@ class AbstractResourceObject
15
15
16
16
public function __construct (array $ data )
17
17
{
18
- $ this ->id = $ data ['id ' ] ?? null ;
18
+ $ this ->id = $ data ['id ' ];
19
19
$ this ->type = $ data ['type ' ];
20
20
21
21
$ concreteClass = new \ReflectionClass ($ this );
22
22
23
23
if (property_exists ($ this , 'attributes ' )) {
24
- $ attributesProperty = $ concreteClass ->getProperty ('attributes ' );
25
- $ attributesClass = $ attributesProperty ->getType ()->getName ();
24
+ $ attributesPropertyType = $ concreteClass ->getProperty ('attributes ' )->getType ();
25
+
26
+ if ($ attributesPropertyType instanceof \ReflectionUnionType) {
27
+ $ attributesClass = $ attributesPropertyType ->getTypes ()[0 ]->getName ();
28
+ } else {
29
+ $ attributesClass = $ attributesPropertyType ->getName ();
30
+ }
26
31
$ this ->attributes = new $ attributesClass ($ data ['attributes ' ]);
27
32
}
28
33
Original file line number Diff line number Diff line change
1
+ <?php
2
+ declare (strict_types=1 );
3
+
4
+ namespace FreeElephants \JsonApi \DTO \Example ;
5
+
6
+ class AttributesExt extends Attributes
7
+ {
8
+ public int $ baz ;
9
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+ declare (strict_types=1 );
3
+
4
+ namespace FreeElephants \JsonApi \DTO \Example ;
5
+
6
+ use FreeElephants \JsonApi \DTO \AbstractResourceObject ;
7
+
8
+ class ResourceObject extends AbstractResourceObject
9
+ {
10
+
11
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+ declare (strict_types=1 );
3
+
4
+ namespace FreeElephants \JsonApi \DTO \Example ;
5
+
6
+ class ResourceObjectExt extends ResourceObject
7
+ {
8
+
9
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+ declare (strict_types=1 );
3
+
4
+ namespace FreeElephants \JsonApi \DTO \Reflection ;
5
+
6
+ use FreeElephants \JsonApi \AbstractTestCase ;
7
+ use FreeElephants \JsonApi \DTO \AbstractDocument ;
8
+ use FreeElephants \JsonApi \DTO \Example ;
9
+
10
+ class DocumentTestPHP8 extends AbstractTestCase
11
+ {
12
+ public function testUnionTypes (): void
13
+ {
14
+ $ document = new class ([
15
+ 'data ' => [
16
+ 'id ' => '1 ' ,
17
+ 'type ' => 'foos ' ,
18
+ ],
19
+ ]) extends AbstractDocument {
20
+ public Example \ResourceObjectExt |Example \ResourceObject $ data ;
21
+ };
22
+
23
+ $ this ->assertInstanceOf (Example \ResourceObjectExt::class, $ document ->data );
24
+ }
25
+ }
Original file line number Diff line number Diff line change 1
1
<?php
2
2
3
- namespace FreeElephants \JsonApi \DTO ;
3
+ namespace FreeElephants \JsonApi \DTO \ Reflection ;
4
4
5
5
use FreeElephants \JsonApi \AbstractTestCase ;
6
+ use FreeElephants \JsonApi \DTO \AbstractResourceObject ;
7
+ use FreeElephants \JsonApi \DTO \Example ;
8
+ use FreeElephants \JsonApi \DTO \Example \AttributesExt ;
6
9
7
10
class ResourceObjectTestPHP8 extends AbstractTestCase
8
11
{
@@ -13,6 +16,7 @@ public function testUnionTypes()
13
16
'type ' => 'type ' ,
14
17
'attributes ' => [
15
18
'foo ' => 'bar ' ,
19
+ 'baz ' => 1 ,
16
20
],
17
21
'relationships ' => [
18
22
'one ' => [
@@ -23,11 +27,14 @@ public function testUnionTypes()
23
27
],
24
28
],
25
29
]) extends AbstractResourceObject{
26
- public Example \Attributes $ attributes ;
30
+ public Example \AttributesExt | Example \ Attributes $ attributes ;
27
31
public Example \OneRelationships |Example \TwoRelationships $ relationships ;
28
32
};
29
33
30
34
$ this ->assertSame ('one ' , $ resourceObject ->relationships ->one ->data ->type );
35
+ $ this ->assertSame ('bar ' , $ resourceObject ->attributes ->foo );
36
+ $ this ->assertSame (1 , $ resourceObject ->attributes ->baz );
37
+ $ this ->assertInstanceOf (AttributesExt::class, $ resourceObject ->attributes );
31
38
}
32
39
}
33
40
You can’t perform that action at this time.
0 commit comments