Skip to content

Commit a8f944c

Browse files
committed
Refactor Document parsing
1 parent 07c1f61 commit a8f944c

File tree

2 files changed

+18
-24
lines changed

2 files changed

+18
-24
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"reuse-annotate": "pipx run reuse annotate src tests --license=\"GPL-3.0-or-later\" --copyright=\"2015-2023 Artur Weigandt https://wlabs.de/kontakt\" --recursive --exclude-year --copyright-style=\"spdx\"",
4040
"reuse-lint": "pipx run reuse --suppress-deprecation lint",
4141
"test": [
42-
"@codestyle --verbose --diff",
42+
"@codestyle --diff --dry-run",
4343
"@phpstan",
4444
"@phpunit"
4545
]

src/V1/Document.php

+17-23
Original file line numberDiff line numberDiff line change
@@ -40,26 +40,22 @@ protected function parse(mixed $object): void
4040
throw new ValidationException('The properties `data` and `errors` MUST NOT coexist in Document.');
4141
}
4242

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);
6359
}
6460
}
6561

@@ -80,11 +76,9 @@ public function get($key): mixed
8076
/**
8177
* Parse the data value
8278
*
83-
* @param null|object|array<string, mixed> $data Data value
84-
*
8579
* @throws ValidationException If $data isn't null or an object
8680
*/
87-
private function parseData($data): Accessable
81+
private function parseData(mixed $data): Accessable
8882
{
8983
if ($data === null) {
9084
return $this->create('ResourceNull', $data);

0 commit comments

Comments
 (0)