File tree Expand file tree Collapse file tree 8 files changed +81
-9
lines changed
src/FreeElephants/JsonApi/DTO
tests/FreeElephants/JsonApi/DTO Expand file tree Collapse file tree 8 files changed +81
-9
lines changed Original file line number Diff line number Diff line change @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6
6
7
7
## [ Unreleased]
8
8
9
+ ### Fixed
10
+ - Handle Union Types for ` data ` and ` attributes ` fields.
11
+
9
12
## [ 0.0.5] - 2025-03-24
10
13
11
14
### Fixed
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,14 @@ 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
+ }
19
22
if ($ dataClassName !== 'array ' ) {
20
23
$ data = new $ dataClassName ($ payload ['data ' ]);
21
24
} 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