@@ -40,26 +40,22 @@ protected function parse(mixed $object): void
40
40
throw new ValidationException ('The properties `data` and `errors` MUST NOT coexist in Document. ' );
41
41
}
42
42
43
- foreach ($ object as $ key => $ value ) {
44
- if ($ key === 'data ' ) {
45
- $ this ->set ('data ' , $ this ->parseData ($ value ));
46
- } else if ($ key === 'meta ' ) {
47
- $ this ->set ('meta ' , $ this ->create ('Meta ' , $ value ));
48
- } else if ($ key === 'errors ' ) {
49
- $ this ->set ('errors ' , $ this ->create ('ErrorCollection ' , $ value ));
50
- } else if ($ key === 'included ' ) {
51
- if (!property_exists ($ object , 'data ' )) {
52
- throw new ValidationException ('If Document does not contain a `data` property, the `included` property MUST NOT be present either. ' );
53
- }
54
-
55
- $ this ->set ('included ' , $ this ->create ('ResourceCollection ' , $ object ->included ));
56
- } else if ($ key === 'jsonapi ' ) {
57
- $ this ->set ('jsonapi ' , $ this ->create ('Jsonapi ' , $ value ));
58
- } else if ($ key === 'links ' ) {
59
- $ this ->set ('links ' , $ this ->create ('DocumentLink ' , $ value ));
60
- } else {
61
- $ this ->set ($ key , $ value );
62
- }
43
+ if (property_exists ($ object , 'included ' ) and !property_exists ($ object , 'data ' )) {
44
+ throw new ValidationException ('If Document does not contain a `data` property, the `included` property MUST NOT be present either. ' );
45
+ }
46
+
47
+ foreach (get_object_vars ($ object ) as $ key => $ value ) {
48
+ $ value = match ($ key ) {
49
+ 'data ' => $ this ->parseData ($ value ),
50
+ 'meta ' => $ this ->create ('Meta ' , $ value ),
51
+ 'errors ' => $ this ->create ('ErrorCollection ' , $ value ),
52
+ 'included ' => $ this ->create ('ResourceCollection ' , $ value ),
53
+ 'jsonapi ' => $ this ->create ('Jsonapi ' , $ value ),
54
+ 'links ' => $ this ->create ('DocumentLink ' , $ value ),
55
+ default => $ value ,
56
+ };
57
+
58
+ $ this ->set ($ key , $ value );
63
59
}
64
60
}
65
61
@@ -80,11 +76,9 @@ public function get($key): mixed
80
76
/**
81
77
* Parse the data value
82
78
*
83
- * @param null|object|array<string, mixed> $data Data value
84
- *
85
79
* @throws ValidationException If $data isn't null or an object
86
80
*/
87
- private function parseData ($ data ): Accessable
81
+ private function parseData (mixed $ data ): Accessable
88
82
{
89
83
if ($ data === null ) {
90
84
return $ this ->create ('ResourceNull ' , $ data );
0 commit comments