Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,17 @@
}
},
"scripts": {
"codestyle": "php-cs-fixer fix --verbose --diff",
"codestyle": "php-cs-fixer fix",
"coverage": "phpunit --coverage-html=\".phpunit.cache/code-coverage\"",
"phpstan": "phpstan analyze --memory-limit 512M --configuration .phpstan.neon",
"phpunit": "phpunit",
"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\"",
"reuse-lint": "pipx run reuse --suppress-deprecation lint"
"reuse-lint": "pipx run reuse --suppress-deprecation lint",
"test": [
"@codestyle --diff --dry-run",
"@phpstan",
"@phpunit"
]
},
"config": {
"sort-packages": true
Expand Down
8 changes: 1 addition & 7 deletions src/V1/Attributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,7 @@ protected function parse(mixed $object): void
throw new ValidationException('These properties are not allowed in attributes: `type`, `id`, `relationships`, `links`');
}

$object_vars = get_object_vars($object);

if (count($object_vars) === 0) {
return;
}

foreach ($object_vars as $name => $value) {
foreach (get_object_vars($object) as $name => $value) {
$this->set($name, $value);
}
}
Expand Down
42 changes: 15 additions & 27 deletions src/V1/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,32 +40,22 @@ protected function parse(mixed $object): void
throw new ValidationException('The properties `data` and `errors` MUST NOT coexist in Document.');
}

if (property_exists($object, 'data')) {
$this->set('data', $this->parseData($object->data));
if (property_exists($object, 'included') and !property_exists($object, 'data')) {
throw new ValidationException('If Document does not contain a `data` property, the `included` property MUST NOT be present either.');
}

if (property_exists($object, 'meta')) {
$this->set('meta', $this->create('Meta', $object->meta));
}

if (property_exists($object, 'errors')) {
$this->set('errors', $this->create('ErrorCollection', $object->errors));
}

if (property_exists($object, 'included')) {
if (!property_exists($object, 'data')) {
throw new ValidationException('If Document does not contain a `data` property, the `included` property MUST NOT be present either.');
}

$this->set('included', $this->create('ResourceCollection', $object->included));
}

if (property_exists($object, 'jsonapi')) {
$this->set('jsonapi', $this->create('Jsonapi', $object->jsonapi));
}

if (property_exists($object, 'links')) {
$this->set('links', $this->create('DocumentLink', $object->links));
foreach (get_object_vars($object) as $key => $value) {
$value = match ($key) {
'data' => $this->parseData($value),
'meta' => $this->create('Meta', $value),
'errors' => $this->create('ErrorCollection', $value),
'included' => $this->create('ResourceCollection', $value),
'jsonapi' => $this->create('Jsonapi', $value),
'links' => $this->create('DocumentLink', $value),
default => $value,
};

$this->set($key, $value);
}
}

Expand All @@ -86,11 +76,9 @@ public function get($key): mixed
/**
* Parse the data value
*
* @param null|object|array<string, mixed> $data Data value
*
* @throws ValidationException If $data isn't null or an object
*/
private function parseData($data): Accessable
private function parseData(mixed $data): Accessable
{
if ($data === null) {
return $this->create('ResourceNull', $data);
Expand Down
118 changes: 54 additions & 64 deletions src/V1/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,71 +33,61 @@ protected function parse(mixed $object): void
);
}

if (property_exists($object, 'id')) {
if (!is_string($object->id)) {
throw new ValidationException(
'property "id" has to be a string, "' .
gettype($object->id) . '" given.'
);
foreach (get_object_vars($object) as $key => $value) {
if ($key === 'id') {
if (!is_string($value)) {
throw new ValidationException(
'property "id" has to be a string, "' .
gettype($value) . '" given.'
);
}

$this->set('id', strval($value));
} elseif ($key === 'links') {
$this->set('links', $this->create('ErrorLink', $value));
} elseif ($key === 'status') {
if (!is_string($value)) {
throw new ValidationException(
'property "status" has to be a string, "' .
gettype($value) . '" given.'
);
}

$this->set('status', strval($value));
} elseif ($key === 'code') {
if (!is_string($value)) {
throw new ValidationException(
'property "code" has to be a string, "' .
gettype($value) . '" given.'
);
}

$this->set('code', strval($value));
} elseif ($key === 'title') {
if (!is_string($value)) {
throw new ValidationException(
'property "title" has to be a string, "' .
gettype($value) . '" given.'
);
}

$this->set('title', strval($value));
} elseif ($key === 'detail') {
if (!is_string($value)) {
throw new ValidationException(
'property "detail" has to be a string, "' .
gettype($value) . '" given.'
);
}

$this->set('detail', strval($value));
} elseif ($key === 'source') {
$this->set('source', $this->create('ErrorSource', $value));
} elseif ($key === 'meta') {
$this->set('meta', $this->create('Meta', $value));
} else {
$this->set($key, $value);
}

$this->set('id', strval($object->id));
}

if (property_exists($object, 'links')) {
$this->set('links', $this->create('ErrorLink', $object->links));
}

if (property_exists($object, 'status')) {
if (!is_string($object->status)) {
throw new ValidationException(
'property "status" has to be a string, "' .
gettype($object->status) . '" given.'
);
}

$this->set('status', strval($object->status));
}

if (property_exists($object, 'code')) {
if (!is_string($object->code)) {
throw new ValidationException(
'property "code" has to be a string, "' .
gettype($object->code) . '" given.'
);
}

$this->set('code', strval($object->code));
}

if (property_exists($object, 'title')) {
if (!is_string($object->title)) {
throw new ValidationException(
'property "title" has to be a string, "' .
gettype($object->title) . '" given.'
);
}

$this->set('title', strval($object->title));
}

if (property_exists($object, 'detail')) {
if (!is_string($object->detail)) {
throw new ValidationException(
'property "detail" has to be a string, "' .
gettype($object->detail) . '" given.'
);
}

$this->set('detail', strval($object->detail));
}

if (property_exists($object, 'source')) {
$this->set('source', $this->create('ErrorSource', $object->source));
}

if (property_exists($object, 'meta')) {
$this->set('meta', $this->create('Meta', $object->meta));
}
}

Expand Down
24 changes: 13 additions & 11 deletions src/V1/ErrorSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,22 @@ protected function parse(mixed $object): void
throw new ValidationException('ErrorSource has to be an object, "' . gettype($object) . '" given.');
}

if (property_exists($object, 'pointer')) {
if (!is_string($object->pointer)) {
throw new ValidationException('property "pointer" has to be a string, "' . gettype($object->pointer) . '" given.');
}
foreach (get_object_vars($object) as $key => $value) {
if ($key === 'pointer') {
if (!is_string($value)) {
throw new ValidationException('property "pointer" has to be a string, "' . gettype($value) . '" given.');
}

$this->set('pointer', strval($object->pointer));
}
$this->set('pointer', strval($value));
} elseif ($key === 'parameter') {
if (!is_string($value)) {
throw new ValidationException('property "parameter" has to be a string, "' . gettype($value) . '" given.');
}

if (property_exists($object, 'parameter')) {
if (!is_string($object->parameter)) {
throw new ValidationException('property "parameter" has to be a string, "' . gettype($object->parameter) . '" given.');
$this->set('parameter', strval($value));
} else {
$this->set($key, $value);
}

$this->set('parameter', strval($object->parameter));
}
}

Expand Down
19 changes: 11 additions & 8 deletions src/V1/Jsonapi.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,19 @@ protected function parse(mixed $object): void
throw new ValidationException('Jsonapi has to be an object, "' . gettype($object) . '" given.');
}

if (property_exists($object, 'version')) {
if (is_object($object->version) or is_array($object->version)) {
throw new ValidationException('property "version" cannot be an object or array, "' . gettype($object->version) . '" given.');
}

$this->set('version', strval($object->version));
if (property_exists($object, 'version') and (is_object($object->version) or is_array($object->version))) {
throw new ValidationException('property "version" cannot be an object or array, "' . gettype($object->version) . '" given.');
}

if (property_exists($object, 'meta')) {
$this->set('meta', $this->create('Meta', $object->meta));
foreach (get_object_vars($object) as $key => $value) {
$value = match ($key) {
/** @phpstan-ignore-next-line */
'version' => strval($value),
'meta' => $this->create('Meta', $value),
default => $value,
};

$this->set($key, $value);
}
}

Expand Down
8 changes: 1 addition & 7 deletions src/V1/Meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,7 @@ protected function parse(mixed $object): void
throw new ValidationException('Meta has to be an object, "' . gettype($object) . '" given.');
}

$object_vars = get_object_vars($object);

if (count($object_vars) === 0) {
return;
}

foreach ($object_vars as $name => $value) {
foreach (get_object_vars($object) as $name => $value) {
$this->set($name, $value);
}
}
Expand Down
29 changes: 14 additions & 15 deletions src/V1/Relationship.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,23 @@ protected function parse(mixed $object): void
throw new ValidationException('A Relationship object MUST contain at least one of the following properties: links, data, meta');
}

if (property_exists($object, 'data')) {
if ($object->data === null) {
$this->set('data', $object->data);
} elseif (is_array($object->data)) {
$this->set('data', $this->create('ResourceIdentifierCollection', $object->data));
foreach (get_object_vars($object) as $key => $value) {
if ($key === 'data') {
if ($value === null) {
$this->set('data', $value);
} elseif (is_array($value)) {
$this->set('data', $this->create('ResourceIdentifierCollection', $value));
} else {
$this->set('data', $this->create('ResourceIdentifier', $value));
}
} elseif ($key === 'meta') {
$this->set('meta', $this->create('Meta', $value));
} elseif ($key === 'links') {
$this->set('links', $this->create('RelationshipLink', $value));
} else {
$this->set('data', $this->create('ResourceIdentifier', $object->data));
$this->set($key, $value);
}
}

if (property_exists($object, 'meta')) {
$this->set('meta', $this->create('Meta', $object->meta));
}

// Parse 'links' after 'data'
if (property_exists($object, 'links')) {
$this->set('links', $this->create('RelationshipLink', $object->links));
}
}

/**
Expand Down
8 changes: 1 addition & 7 deletions src/V1/RelationshipCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,7 @@ protected function parse(mixed $object): void
throw new ValidationException('These properties are not allowed in attributes: `type`, `id`');
}

$object_vars = get_object_vars($object);

if (count($object_vars) === 0) {
return;
}

foreach ($object_vars as $name => $value) {
foreach (get_object_vars($object) as $name => $value) {
if ($this->getParent()->has('attributes.' . $name)) {
throw new ValidationException('"' . $name . '" property cannot be set because it exists already in parents Resource object.');
}
Expand Down
11 changes: 6 additions & 5 deletions src/V1/ResourceIdentifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,12 @@ protected function parse(mixed $object): void
throw new ValidationException('A resource id MUST be a string');
}

$this->set('type', $object->type);
$this->set('id', $object->id);

if (property_exists($object, 'meta')) {
$this->set('meta', $this->create('Meta', $object->meta));
foreach (get_object_vars($object) as $key => $value) {
if ($key === 'meta') {
$this->set('meta', $this->create('Meta', $value));
} else {
$this->set($key, $value);
}
}
}

Expand Down
Loading