-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathRelationshipCollection.php
More file actions
60 lines (51 loc) · 1.81 KB
/
Copy pathRelationshipCollection.php
File metadata and controls
60 lines (51 loc) · 1.81 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
<?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;
/**
* Relationship Collection Object
*
* @see http://jsonapi.org/format/#document-resource-object-relationships
*/
final class RelationshipCollection extends AbstractElement
{
/**
* Parses the data for this element
*
* @throws ValidationException
*/
protected function parse(mixed $object): void
{
if (!is_object($object)) {
throw new ValidationException('Relationships has to be an object, "' . gettype($object) . '" given.');
}
if (property_exists($object, 'type') or property_exists($object, 'id')) {
throw new ValidationException('These properties are not allowed in attributes: `type`, `id`');
}
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.');
}
$this->set($name, $this->create('Relationship', $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 relationship collection.');
}
}
}