1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Ivory Google Map package. |
7
|
|
|
* |
8
|
|
|
* (c) Eric GELOEN <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please read the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Ivory\GoogleMap\Helper\Event; |
15
|
|
|
|
16
|
|
|
use SplObjectStorage; |
17
|
|
|
|
18
|
|
|
class ApiEvent extends AbstractEvent |
19
|
|
|
{ |
20
|
|
|
/** @var object[] */ |
21
|
|
|
private $objects; |
22
|
|
|
|
23
|
|
|
/** @var string[] */ |
24
|
|
|
private $sources = []; |
25
|
|
|
|
26
|
|
|
/** @var string[] */ |
27
|
|
|
private $libraries = []; |
28
|
|
|
|
29
|
|
|
/** @var SplObjectStorage */ |
30
|
|
|
private $callbacks; |
31
|
|
|
|
32
|
|
|
/** @var SplObjectStorage */ |
33
|
|
|
private $requirements; |
34
|
|
|
|
35
|
|
|
/** @param object[] $objects */ |
36
|
17 |
|
public function __construct(array $objects) |
37
|
|
|
{ |
38
|
17 |
|
$this->objects = $objects; |
39
|
17 |
|
$this->callbacks = new SplObjectStorage(); |
40
|
17 |
|
$this->requirements = new SplObjectStorage(); |
41
|
17 |
|
} |
42
|
|
|
|
43
|
2 |
|
public function hasObjects(?string $class = null): bool |
44
|
|
|
{ |
45
|
2 |
|
$objects = $this->getObjects($class); |
46
|
|
|
|
47
|
2 |
|
return !empty($objects); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** @return object[] */ |
51
|
2 |
|
public function getObjects(?string $class = null): array |
52
|
|
|
{ |
53
|
2 |
|
if (null === $class) { |
54
|
1 |
|
return $this->objects; |
55
|
|
|
} |
56
|
|
|
|
57
|
1 |
|
$objects = []; |
58
|
|
|
|
59
|
1 |
|
foreach ($this->objects as $object) { |
60
|
1 |
|
if ($object instanceof $class) { |
61
|
1 |
|
$objects[] = $object; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
1 |
|
return $objects; |
66
|
|
|
} |
67
|
|
|
|
68
|
5 |
|
public function hasSources(): bool |
69
|
|
|
{ |
70
|
5 |
|
return !empty($this->sources); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** @return string[] */ |
74
|
5 |
|
public function getSources(): array |
75
|
|
|
{ |
76
|
5 |
|
return $this->sources; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** @param string[] $sources */ |
80
|
2 |
|
public function setSources(array $sources): void |
81
|
|
|
{ |
82
|
2 |
|
$this->sources = []; |
83
|
2 |
|
$this->addSources($sources); |
84
|
2 |
|
} |
85
|
|
|
|
86
|
|
|
/** @param string[] $sources */ |
87
|
2 |
|
public function addSources(array $sources): void |
88
|
|
|
{ |
89
|
2 |
|
foreach ($sources as $source) { |
90
|
2 |
|
$this->addSource($source); |
91
|
|
|
} |
92
|
2 |
|
} |
93
|
|
|
|
94
|
4 |
|
public function hasSource(string $source): bool |
95
|
|
|
{ |
96
|
4 |
|
return in_array($source, $this->sources, true); |
97
|
|
|
} |
98
|
|
|
|
99
|
4 |
|
public function addSource(string $source): void |
100
|
|
|
{ |
101
|
4 |
|
if (!$this->hasSource($source)) { |
102
|
4 |
|
$this->sources[] = $source; |
103
|
|
|
} |
104
|
4 |
|
} |
105
|
|
|
|
106
|
1 |
|
public function removeSource(string $source): void |
107
|
|
|
{ |
108
|
1 |
|
unset($this->sources[array_search($source, $this->sources, true)]); |
109
|
1 |
|
$this->sources = empty($this->sources) ? [] : array_values($this->sources); |
110
|
1 |
|
} |
111
|
|
|
|
112
|
5 |
|
public function hasLibraries(): bool |
113
|
|
|
{ |
114
|
5 |
|
return !empty($this->libraries); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** @return string[] */ |
118
|
5 |
|
public function getLibraries(): array |
119
|
|
|
{ |
120
|
5 |
|
return $this->libraries; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** @param string[] $libraries */ |
124
|
2 |
|
public function setLibraries(array $libraries): void |
125
|
|
|
{ |
126
|
2 |
|
$this->libraries = []; |
127
|
2 |
|
$this->addLibraries($libraries); |
128
|
2 |
|
} |
129
|
|
|
|
130
|
|
|
/** @param string[] $libraries */ |
131
|
2 |
|
public function addLibraries(array $libraries): void |
132
|
|
|
{ |
133
|
2 |
|
foreach ($libraries as $library) { |
134
|
2 |
|
$this->addLibrary($library); |
135
|
|
|
} |
136
|
2 |
|
} |
137
|
|
|
|
138
|
4 |
|
public function hasLibrary(string $library): bool |
139
|
|
|
{ |
140
|
4 |
|
return in_array($library, $this->libraries, true); |
141
|
|
|
} |
142
|
|
|
|
143
|
4 |
|
public function addLibrary(string $library): void |
144
|
|
|
{ |
145
|
4 |
|
if (!$this->hasLibrary($library)) { |
146
|
4 |
|
$this->libraries[] = $library; |
147
|
|
|
} |
148
|
4 |
|
} |
149
|
|
|
|
150
|
1 |
|
public function removeLibrary(string $library): void |
151
|
|
|
{ |
152
|
1 |
|
unset($this->libraries[array_search($library, $this->libraries, true)]); |
153
|
1 |
|
$this->libraries = empty($this->libraries) ? [] : array_values($this->libraries); |
154
|
1 |
|
} |
155
|
|
|
|
156
|
3 |
|
public function hasCallbacks(): bool |
157
|
|
|
{ |
158
|
3 |
|
return count($this->callbacks) > 0; |
159
|
|
|
} |
160
|
|
|
|
161
|
1 |
|
public function getCallbacks(): SplObjectStorage |
162
|
|
|
{ |
163
|
1 |
|
return $this->callbacks; |
164
|
|
|
} |
165
|
|
|
|
166
|
2 |
|
public function hasCallback(string $callback, ?object $object = null): bool |
167
|
|
|
{ |
168
|
2 |
|
foreach ($this->callbacks as $rawObject) { |
169
|
2 |
|
if ($this->callbacks[$rawObject] === $callback && (null === $object || $object === $rawObject)) { |
170
|
2 |
|
return true; |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
174
|
2 |
|
return false; |
175
|
|
|
} |
176
|
|
|
|
177
|
2 |
|
public function hasCallbackObject(object $object, ?string $callback = null): bool |
178
|
|
|
{ |
179
|
2 |
|
return isset($this->callbacks[$object]) && (null === $callback || $this->callbacks[$object] === $callback); |
180
|
|
|
} |
181
|
|
|
|
182
|
2 |
|
public function getCallback(object $object): ?string |
183
|
|
|
{ |
184
|
2 |
|
return $this->hasCallbackObject($object) ? $this->callbacks[$object] : null; |
185
|
|
|
} |
186
|
|
|
|
187
|
2 |
|
public function getCallbackObject(string $callback): ?object |
188
|
|
|
{ |
189
|
2 |
|
foreach ($this->callbacks as $object) { |
190
|
2 |
|
if ($this->callbacks[$object] === $callback) { |
191
|
2 |
|
return $object; |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|
195
|
1 |
|
return null; |
196
|
|
|
} |
197
|
|
|
|
198
|
2 |
|
public function addCallback(object $object, string $callback): void |
199
|
|
|
{ |
200
|
2 |
|
if (!$this->hasCallback($callback, $object)) { |
201
|
2 |
|
$this->callbacks[$object] = $callback; |
202
|
|
|
} |
203
|
2 |
|
} |
204
|
|
|
|
205
|
1 |
|
public function removeCallbackObject(object $object): void |
206
|
|
|
{ |
207
|
1 |
|
unset($this->callbacks[$object]); |
208
|
1 |
|
} |
209
|
|
|
|
210
|
1 |
|
public function removeCallback(string $callback): void |
211
|
|
|
{ |
212
|
1 |
|
if ($this->hasCallback($callback)) { |
213
|
1 |
|
$this->removeCallbackObject($this->getCallbackObject($callback)); |
|
|
|
|
214
|
|
|
} |
215
|
1 |
|
} |
216
|
|
|
|
217
|
5 |
|
public function hasRequirements(?object $object = null): bool |
218
|
|
|
{ |
219
|
5 |
|
if (null === $object) { |
220
|
5 |
|
return count($this->requirements) > 0; |
221
|
|
|
} |
222
|
|
|
|
223
|
4 |
|
$requirements = $this->getRequirementsObject($object); |
224
|
|
|
|
225
|
4 |
|
return !empty($requirements); |
226
|
|
|
} |
227
|
|
|
|
228
|
1 |
|
public function getRequirements(): SplObjectStorage |
229
|
|
|
{ |
230
|
1 |
|
return $this->requirements; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** @return string[] */ |
234
|
4 |
|
public function getRequirementsObject(object $object): array |
235
|
|
|
{ |
236
|
4 |
|
return $this->hasRequirement($object) ? $this->requirements[$object] : []; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** @param string[] $requirements */ |
240
|
2 |
|
public function setRequirements(object $object, array $requirements): void |
241
|
|
|
{ |
242
|
2 |
|
$this->requirements = new SplObjectStorage(); |
243
|
2 |
|
$this->addRequirements($object, $requirements); |
244
|
2 |
|
} |
245
|
|
|
|
246
|
|
|
/** @param string[] $requirements */ |
247
|
2 |
|
public function addRequirements(object $object, array $requirements): void |
248
|
|
|
{ |
249
|
2 |
|
foreach ($requirements as $requirement) { |
250
|
2 |
|
$this->addRequirement($object, $requirement); |
251
|
|
|
} |
252
|
2 |
|
} |
253
|
|
|
|
254
|
4 |
|
public function hasRequirement(object $object, ?string $requirement = null): bool |
255
|
|
|
{ |
256
|
4 |
|
return isset($this->requirements[$object]) |
257
|
4 |
|
&& (null === $requirement || in_array($requirement, $this->requirements[$object], true)); |
258
|
|
|
} |
259
|
|
|
|
260
|
4 |
|
public function addRequirement(object $object, string $requirement): void |
261
|
|
|
{ |
262
|
4 |
|
if (!$this->hasRequirement($object)) { |
263
|
4 |
|
$this->requirements[$object] = []; |
264
|
|
|
} |
265
|
|
|
|
266
|
4 |
|
if (!$this->hasRequirement($object, $requirement)) { |
267
|
4 |
|
$this->requirements[$object] = array_merge( |
268
|
4 |
|
$this->requirements[$object], |
269
|
4 |
|
[$requirement] |
270
|
|
|
); |
271
|
|
|
} |
272
|
4 |
|
} |
273
|
|
|
|
274
|
1 |
|
public function removeRequirement(object $object, ?string $requirement = null): void |
275
|
|
|
{ |
276
|
1 |
|
if (null === $requirement) { |
277
|
1 |
|
unset($this->requirements[$object]); |
278
|
|
|
|
279
|
1 |
|
return; |
280
|
|
|
} |
281
|
|
|
|
282
|
1 |
|
if ($this->hasRequirement($object, $requirement)) { |
283
|
1 |
|
$requirements = $this->requirements[$object]; |
284
|
1 |
|
unset($requirements[array_search($requirement, $requirements, true)]); |
285
|
|
|
|
286
|
1 |
|
if (!empty($requirements)) { |
287
|
1 |
|
$this->requirements[$object] = array_values($requirements); |
288
|
|
|
} else { |
289
|
1 |
|
$this->removeRequirement($object); |
290
|
|
|
} |
291
|
|
|
} |
292
|
1 |
|
} |
293
|
|
|
} |
294
|
|
|
|