1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace alsvanzelf\jsonapi\objects; |
4
|
|
|
|
5
|
|
|
use alsvanzelf\jsonapi\exceptions\DuplicateException; |
6
|
|
|
use alsvanzelf\jsonapi\helpers\AtMemberManager; |
7
|
|
|
use alsvanzelf\jsonapi\helpers\ExtensionMemberManager; |
8
|
|
|
use alsvanzelf\jsonapi\helpers\Validator; |
9
|
|
|
use alsvanzelf\jsonapi\interfaces\ObjectInterface; |
10
|
|
|
use alsvanzelf\jsonapi\interfaces\RecursiveResourceContainerInterface; |
11
|
|
|
use alsvanzelf\jsonapi\objects\LinkObject; |
12
|
|
|
use alsvanzelf\jsonapi\objects\RelationshipObject; |
13
|
|
|
use alsvanzelf\jsonapi\objects\ResourceObject; |
14
|
|
|
|
15
|
|
|
class RelationshipsObject implements ObjectInterface, RecursiveResourceContainerInterface { |
16
|
|
|
use AtMemberManager, ExtensionMemberManager; |
17
|
|
|
|
18
|
|
|
/** @var RelationshipObject[] */ |
19
|
|
|
protected $relationships = []; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* human api |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param string $key |
27
|
|
|
* @param mixed $relation ResourceInterface | ResourceInterface[] | CollectionDocument |
28
|
|
|
* @param array $links optional |
29
|
|
|
* @param array $meta optional |
30
|
|
|
* @return RelationshipObject |
31
|
|
|
*/ |
32
|
1 |
|
public function add($key, $relation, array $links=[], array $meta=[]) { |
33
|
1 |
|
$relationshipObject = RelationshipObject::fromAnything($relation, $links, $meta); |
34
|
|
|
|
35
|
1 |
|
$this->addRelationshipObject($key, $relationshipObject); |
36
|
|
|
|
37
|
1 |
|
return $relationshipObject; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* spec api |
42
|
|
|
*/ |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param string $key |
46
|
|
|
* @param RelationshipObject $relationshipObject |
47
|
|
|
* |
48
|
|
|
* @throws DuplicateException if another relationship is already using that $key |
49
|
|
|
*/ |
50
|
31 |
|
public function addRelationshipObject($key, RelationshipObject $relationshipObject) { |
51
|
31 |
|
Validator::checkMemberName($key); |
52
|
|
|
|
53
|
30 |
|
if (isset($this->relationships[$key])) { |
54
|
1 |
|
throw new DuplicateException('relationship with key "'.$key.'" already set'); |
55
|
|
|
} |
56
|
|
|
|
57
|
30 |
|
$this->relationships[$key] = $relationshipObject; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* internal api |
62
|
|
|
*/ |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @internal |
66
|
|
|
* |
67
|
|
|
* @return string[] |
68
|
|
|
*/ |
69
|
25 |
|
public function getKeys() { |
70
|
25 |
|
return array_keys($this->relationships); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* ObjectInterface |
75
|
|
|
*/ |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @inheritDoc |
79
|
|
|
*/ |
80
|
24 |
|
public function isEmpty() { |
81
|
24 |
|
if ($this->relationships !== []) { |
82
|
23 |
|
return false; |
83
|
|
|
} |
84
|
3 |
|
if ($this->hasAtMembers()) { |
85
|
1 |
|
return false; |
86
|
|
|
} |
87
|
2 |
|
if ($this->hasExtensionMembers()) { |
88
|
1 |
|
return false; |
89
|
|
|
} |
90
|
|
|
|
91
|
1 |
|
return true; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @inheritDoc |
96
|
|
|
*/ |
97
|
26 |
|
public function toArray() { |
98
|
26 |
|
$array = []; |
99
|
|
|
|
100
|
26 |
|
if ($this->hasAtMembers()) { |
101
|
1 |
|
$array = array_merge($array, $this->getAtMembers()); |
102
|
|
|
} |
103
|
26 |
|
if ($this->hasExtensionMembers()) { |
104
|
1 |
|
$array = array_merge($array, $this->getExtensionMembers()); |
105
|
|
|
} |
106
|
|
|
|
107
|
26 |
|
foreach ($this->relationships as $key => $relationshipObject) { |
108
|
26 |
|
$array[$key] = $relationshipObject->toArray(); |
109
|
|
|
} |
110
|
|
|
|
111
|
26 |
|
return $array; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* RecursiveResourceContainerInterface |
116
|
|
|
*/ |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @inheritDoc |
120
|
|
|
*/ |
121
|
10 |
|
public function getNestedContainedResourceObjects() { |
122
|
10 |
|
$resourceObjects = []; |
123
|
|
|
|
124
|
10 |
|
foreach ($this->relationships as $relationship) { |
125
|
10 |
|
$resourceObjects = array_merge($resourceObjects, $relationship->getNestedContainedResourceObjects()); |
126
|
|
|
} |
127
|
|
|
|
128
|
10 |
|
return $resourceObjects; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|