-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathResourceIdentifier.php
More file actions
72 lines (61 loc) · 2 KB
/
Copy pathResourceIdentifier.php
File metadata and controls
72 lines (61 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
declare(strict_types=1);
// SPDX-FileCopyrightText: 2015-2023 Artur Weigandt https://wlabs.de/kontakt
//
// SPDX-License-Identifier: GPL-3.0-or-later
namespace Art4\JsonApiClient\V1;
use Art4\JsonApiClient\Exception\AccessException;
use Art4\JsonApiClient\Exception\ValidationException;
use Art4\JsonApiClient\Helper\AbstractElement;
use Art4\JsonApiClient\Helper\AccessKey;
/**
* Resource Identifier Object
*
* @see http://jsonapi.org/format/#document-resource-identifier-objects
*/
final class ResourceIdentifier extends AbstractElement
{
/**
* Parses the data for this element
*
* @throws ValidationException
*/
protected function parse(mixed $object): void
{
if (!is_object($object)) {
throw new ValidationException('Resource has to be an object, "' . gettype($object) . '" given.');
}
if (!property_exists($object, 'type')) {
throw new ValidationException('A resource object MUST contain a type');
}
if (!is_string($object->type)) {
throw new ValidationException('A resource type MUST be a string');
}
if (!property_exists($object, 'id')) {
throw new ValidationException('A resource object MUST contain an id');
}
if (!is_string($object->id)) {
throw new ValidationException('A resource id MUST be a string');
}
foreach (get_object_vars($object) as $key => $value) {
if ($key === 'meta') {
$this->set('meta', $this->create('Meta', $value));
} else {
$this->set($key, $value);
}
}
}
/**
* Get a value by the key of this object
*
* @param int|string|AccessKey<string> $key The key of the value
*/
public function get($key): mixed
{
try {
return parent::get($key);
} catch (AccessException $e) {
throw new AccessException('"' . $key . '" doesn\'t exist in this identifier.');
}
}
}