|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* ResourceIdentifierCollection.php |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright More in license.md |
|
6
|
|
|
* @license https://www.ipublikuj.eu |
|
7
|
|
|
* @author Adam Kadlec <[email protected]> |
|
8
|
|
|
* @package iPublikuj:JsonAPIClient! |
|
9
|
|
|
* @subpackage Objects |
|
10
|
|
|
* @since 1.0.0 |
|
11
|
|
|
* |
|
12
|
|
|
* @date 05.05.18 |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
declare(strict_types = 1); |
|
16
|
|
|
|
|
17
|
|
|
namespace IPub\JsonAPIClient\Objects; |
|
18
|
|
|
|
|
19
|
|
|
use IPub\JsonAPIClient\Exceptions; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Resource identifier object |
|
23
|
|
|
* |
|
24
|
|
|
* @package iPublikuj:JsonAPIClient! |
|
25
|
|
|
* @subpackage Objects |
|
26
|
|
|
* |
|
27
|
|
|
* @author Adam Kadlec <[email protected]> |
|
28
|
|
|
*/ |
|
29
|
|
|
class ResourceIdentifierCollection implements IResourceIdentifierCollection |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* @var array |
|
33
|
|
|
*/ |
|
34
|
|
|
private $stack = []; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param array $identifiers |
|
38
|
|
|
*/ |
|
39
|
|
|
public function __construct(array $identifiers = []) |
|
40
|
|
|
{ |
|
41
|
|
|
$this->addMany($identifiers); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param IResourceIdentifier $identifier |
|
46
|
|
|
* |
|
47
|
|
|
* @return void |
|
48
|
|
|
*/ |
|
49
|
|
|
public function add(IResourceIdentifier $identifier) : void |
|
50
|
|
|
{ |
|
51
|
|
|
if (!$this->has($identifier)) { |
|
52
|
|
|
$this->stack[] = $identifier; |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* {@inheritdoc} |
|
58
|
|
|
*/ |
|
59
|
|
|
public function has(IResourceIdentifier $identifier) : bool |
|
60
|
|
|
{ |
|
61
|
|
|
return in_array($identifier, $this->stack); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param IResourceIdentifier[] $identifiers |
|
66
|
|
|
* |
|
67
|
|
|
* @return void |
|
68
|
|
|
*/ |
|
69
|
|
|
public function addMany(array $identifiers) : void |
|
70
|
|
|
{ |
|
71
|
|
|
foreach ($identifiers as $identifier) { |
|
72
|
|
|
|
|
73
|
|
|
if (!$identifier instanceof IResourceIdentifier) { |
|
74
|
|
|
throw new Exceptions\InvalidArgumentException('Expecting only identifier objects.'); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
$this->add($identifier); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param array $identifiers |
|
83
|
|
|
* |
|
84
|
|
|
* @return void |
|
85
|
|
|
*/ |
|
86
|
|
|
public function setAll(array $identifiers) : void |
|
87
|
|
|
{ |
|
88
|
|
|
$this->clear(); |
|
89
|
|
|
$this->addMany($identifiers); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @return void |
|
94
|
|
|
*/ |
|
95
|
|
|
public function clear() : void |
|
96
|
|
|
{ |
|
97
|
|
|
$this->stack = []; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* {@inheritdoc} |
|
102
|
|
|
*/ |
|
103
|
|
|
public function getIterator() : \ArrayIterator |
|
104
|
|
|
{ |
|
105
|
|
|
return new \ArrayIterator($this->getAll()); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* {@inheritdoc} |
|
110
|
|
|
*/ |
|
111
|
|
|
public function count() : int |
|
112
|
|
|
{ |
|
113
|
|
|
return count($this->stack); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* {@inheritdoc} |
|
118
|
|
|
*/ |
|
119
|
|
|
public function isEmpty() : bool |
|
120
|
|
|
{ |
|
121
|
|
|
return empty($this->stack); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* {@inheritdoc} |
|
126
|
|
|
*/ |
|
127
|
|
|
public function isComplete() : bool |
|
128
|
|
|
{ |
|
129
|
|
|
/** @var IResourceIdentifier $identifier */ |
|
130
|
|
|
foreach ($this as $identifier) { |
|
131
|
|
|
if (!$identifier->isComplete()) { |
|
132
|
|
|
return FALSE; |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
return TRUE; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* {@inheritdoc} |
|
141
|
|
|
*/ |
|
142
|
|
|
public function isOnly($typeOrTypes) : bool |
|
143
|
|
|
{ |
|
144
|
|
|
/** @var IResourceIdentifier $identifier */ |
|
145
|
|
|
foreach ($this as $identifier) { |
|
146
|
|
|
if (!$identifier->isType($typeOrTypes)) { |
|
147
|
|
|
return FALSE; |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
return TRUE; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* {@inheritdoc} |
|
156
|
|
|
*/ |
|
157
|
|
|
public function map(?array $typeMap = NULL) |
|
158
|
|
|
{ |
|
159
|
|
|
$ret = []; |
|
160
|
|
|
|
|
161
|
|
|
/** @var IResourceIdentifier $identifier */ |
|
162
|
|
|
foreach ($this as $identifier) { |
|
163
|
|
|
|
|
164
|
|
|
$key = is_array($typeMap) ? $identifier->mapType($typeMap) : $identifier->getType(); |
|
165
|
|
|
|
|
166
|
|
|
if (!isset($ret[$key])) { |
|
167
|
|
|
$ret[$key] = []; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
$ret[$key][] = $identifier->getId(); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
return $ret; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* {@inheritdoc} |
|
178
|
|
|
*/ |
|
179
|
|
|
public function getAll() : array |
|
180
|
|
|
{ |
|
181
|
|
|
return $this->stack; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* {@inheritdoc} |
|
186
|
|
|
*/ |
|
187
|
|
|
public function getIds() : array |
|
188
|
|
|
{ |
|
189
|
|
|
$ids = []; |
|
190
|
|
|
|
|
191
|
|
|
/** @var IResourceIdentifier $identifier */ |
|
192
|
|
|
foreach ($this as $identifier) { |
|
193
|
|
|
$ids[] = $identifier->getId(); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
return $ids; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* @param array $input |
|
201
|
|
|
* |
|
202
|
|
|
* @return ResourceIdentifierCollection |
|
203
|
|
|
*/ |
|
204
|
|
|
public static function create(array $input) |
|
205
|
|
|
{ |
|
206
|
|
|
$collection = new static(); |
|
207
|
|
|
|
|
208
|
|
|
foreach ($input as $value) { |
|
209
|
|
|
$collection->add(new ResourceIdentifier($value)); |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
return $collection; |
|
213
|
|
|
} |
|
214
|
|
|
} |
|
215
|
|
|
|