Skip to content

Commit a5cda9f

Browse files
committed
Refactor Jsonapi parsing
1 parent a8f944c commit a5cda9f

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

src/V1/Jsonapi.php

+13-12
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,19 @@ protected function parse(mixed $object): void
3131
throw new ValidationException('Jsonapi has to be an object, "' . gettype($object) . '" given.');
3232
}
3333

34-
foreach ($object as $key => $value) {
35-
if ($key === 'version') {
36-
if (is_object($value) or is_array($value)) {
37-
throw new ValidationException('property "version" cannot be an object or array, "' . gettype($value) . '" given.');
38-
}
39-
40-
$this->set('version', strval($value));
41-
} else if ($key === 'meta') {
42-
$this->set('meta', $this->create('Meta', $value));
43-
} else {
44-
$this->set($key, $value);
45-
}
34+
if (property_exists($object, 'version') and (is_object($object->version) or is_array($object->version))) {
35+
throw new ValidationException('property "version" cannot be an object or array, "' . gettype($object->version) . '" given.');
36+
}
37+
38+
foreach (get_object_vars($object) as $key => $value) {
39+
$value = match ($key) {
40+
/** @phpstan-ignore-next-line */
41+
'version' => strval($value),
42+
'meta' => $this->create('Meta', $value),
43+
default => $value,
44+
};
45+
46+
$this->set($key, $value);
4647
}
4748
}
4849

0 commit comments

Comments
 (0)