1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the core-bundle package. |
5
|
|
|
* |
6
|
|
|
* (c) 2018 WEBEWEB |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace WBW\Bundle\CoreBundle\Navigation; |
13
|
|
|
|
14
|
|
|
use WBW\Library\Core\Sorting\AlphabeticalTreeNodeInterface; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Abstract navigation node. |
18
|
|
|
* |
19
|
|
|
* @author webeweb <https://github.com/webeweb/> |
20
|
|
|
* @package WBW\Bundle\CoreBundle\Navigation |
21
|
|
|
* @abstract |
22
|
|
|
*/ |
23
|
|
|
abstract class AbstractNavigationNode implements NavigationInterface, AlphabeticalTreeNodeInterface { |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Active ? |
27
|
|
|
* |
28
|
|
|
* @var bool |
29
|
|
|
*/ |
30
|
|
|
private $active; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Enable ? |
34
|
|
|
* |
35
|
|
|
* @var bool |
36
|
|
|
*/ |
37
|
|
|
private $enable; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Icon. |
41
|
|
|
* |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
private $icon; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Id. |
48
|
|
|
* |
49
|
|
|
* @var string |
50
|
|
|
*/ |
51
|
|
|
private $id; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Index. |
55
|
|
|
* |
56
|
|
|
* @var array |
57
|
|
|
*/ |
58
|
|
|
private $index; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Label. |
62
|
|
|
* |
63
|
|
|
* @var string |
64
|
|
|
*/ |
65
|
|
|
private $label; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Matcher. |
69
|
|
|
* |
70
|
|
|
* @var string |
71
|
|
|
*/ |
72
|
|
|
private $matcher; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Navigation nodes. |
76
|
|
|
* |
77
|
|
|
* @var AbstractNavigationNode[] |
78
|
|
|
*/ |
79
|
|
|
private $nodes; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Parent. |
83
|
|
|
* |
84
|
|
|
* @var AbstractNavigationNode |
85
|
|
|
*/ |
86
|
|
|
private $parent; |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Target. |
90
|
|
|
* |
91
|
|
|
* @var string |
92
|
|
|
*/ |
93
|
|
|
private $target; |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* URI. |
97
|
|
|
* |
98
|
|
|
* @var string |
99
|
|
|
*/ |
100
|
|
|
private $uri; |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Visible ? |
104
|
|
|
* |
105
|
|
|
* @var bool |
106
|
|
|
*/ |
107
|
|
|
private $visible; |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Constructor. |
111
|
|
|
* |
112
|
|
|
* @param string $label The label. |
113
|
|
|
* @param string|null $icon The icon. |
114
|
|
|
* @param string|null $uri The URI. |
115
|
|
|
* @param string $matcher The matcher. |
116
|
|
|
*/ |
117
|
|
|
protected function __construct($label, $icon = null, $uri = null, $matcher = self::NAVIGATION_MATCHER_URL) { |
118
|
|
|
$this->setActive(false); |
119
|
|
|
$this->setEnable(false); |
120
|
|
|
$this->setIcon($icon); |
121
|
|
|
$this->setId(uniqid("nav")); |
122
|
|
|
$this->setIndex([]); |
123
|
|
|
$this->setLabel($label); |
124
|
|
|
$this->setMatcher($matcher); |
125
|
|
|
$this->setNodes([]); |
126
|
|
|
$this->setParent(null); |
127
|
|
|
$this->setTarget(null); |
128
|
|
|
$this->setUri($uri); |
129
|
|
|
$this->setVisible(true); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Add a navigation node. |
134
|
|
|
* |
135
|
|
|
* @param AbstractNavigationNode $node The navigation node. |
136
|
|
|
* @return AbstractNavigationNode Returns this navigation node. |
137
|
|
|
*/ |
138
|
|
|
public function addNode(AbstractNavigationNode $node) { |
139
|
|
|
$this->index[$node->getId()] = $this->size(); |
140
|
|
|
$this->nodes[] = $node->setParent($this); |
141
|
|
|
return $this; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Clear the navigation nodes. |
146
|
|
|
* |
147
|
|
|
* @return AbstractNavigationNode Returns this navigation node. |
148
|
|
|
*/ |
149
|
|
|
public function clearNodes() { |
150
|
|
|
foreach ($this->getNodes() as $node) { |
151
|
|
|
$this->removeNode($node); |
152
|
|
|
} |
153
|
|
|
return $this; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Get the active. |
158
|
|
|
* |
159
|
|
|
* @return bool Returns the active. |
160
|
|
|
*/ |
161
|
|
|
public function getActive() { |
162
|
|
|
return $this->active; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* {@inheritdoc} |
167
|
|
|
*/ |
168
|
|
|
public function getAlphabeticalTreeNodeLabel() { |
169
|
|
|
return $this->getId(); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* {@inheritdoc} |
174
|
|
|
*/ |
175
|
|
|
public function getAlphabeticalTreeNodeParent() { |
176
|
|
|
return $this->getParent(); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Get the enable. |
181
|
|
|
* |
182
|
|
|
* @return bool Returns the enable. |
183
|
|
|
*/ |
184
|
|
|
public function getEnable() { |
185
|
|
|
return $this->enable; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Get the first navigation node. |
190
|
|
|
* |
191
|
|
|
* @return AbstractNavigationNode|null Returns the first navigation node in case of success, null otherwise. |
192
|
|
|
*/ |
193
|
|
|
public function getFirstNode() { |
194
|
|
|
return $this->getNodeAt(0); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Get the icon. |
199
|
|
|
* |
200
|
|
|
* @return string Returns the icon. |
201
|
|
|
*/ |
202
|
|
|
public function getIcon() { |
203
|
|
|
return $this->icon; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Get the id. |
208
|
|
|
* |
209
|
|
|
* @return string Returns the id. |
210
|
|
|
*/ |
211
|
|
|
public function getId() { |
212
|
|
|
return $this->id; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Get the label. |
217
|
|
|
* |
218
|
|
|
* @return string Returns the label. |
219
|
|
|
*/ |
220
|
|
|
public function getLabel(): string { |
221
|
|
|
return $this->label; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Get the last navigation node. |
226
|
|
|
* |
227
|
|
|
* @return AbstractNavigationNode|null Returns the last navigation node in case of success, null otherwise. |
228
|
|
|
*/ |
229
|
|
|
public function getLastNode() { |
230
|
|
|
return $this->getNodeAt($this->size() - 1); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Get the matcher. |
235
|
|
|
* |
236
|
|
|
* @return string Returns the matcher. |
237
|
|
|
*/ |
238
|
|
|
public function getMatcher() { |
239
|
|
|
return $this->matcher; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Get a navigation node at. |
244
|
|
|
* |
245
|
|
|
* @param int $position The position. |
246
|
|
|
* @return AbstractNavigationNode|null Returns the navigation node in case of success, null otherwise. |
247
|
|
|
*/ |
248
|
|
|
public function getNodeAt($position) { |
249
|
|
|
|
250
|
|
|
if ($position < 0 || $this->size() <= $position) { |
251
|
|
|
return null; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
return $this->getNodes()[$position]; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Get a navigation node by id. |
259
|
|
|
* |
260
|
|
|
* @param string $id The id. |
261
|
|
|
* @param bool $recursively Recursively ? |
262
|
|
|
* @return AbstractNavigationNode Returns the navigation node in case of success, null otherwise. |
263
|
|
|
*/ |
264
|
|
|
public function getNodeById($id, $recursively = false) { |
265
|
|
|
|
266
|
|
|
if (true === array_key_exists($id, $this->index)) { |
267
|
|
|
return $this->getNodeAt($this->index[$id]); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
if (false === $recursively) { |
271
|
|
|
return null; |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
foreach ($this->getNodes() as $current) { |
275
|
|
|
|
276
|
|
|
$found = $current->getNodeById($id, true); |
277
|
|
|
if (null === $found) { |
278
|
|
|
continue; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
return $found; |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
return null; |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* Get the navigation nodes. |
289
|
|
|
* |
290
|
|
|
* @return AbstractNavigationNode[] Returns the navigation nodes. |
291
|
|
|
*/ |
292
|
|
|
public function getNodes() { |
293
|
|
|
return $this->nodes; |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* Get the parent. |
298
|
|
|
* |
299
|
|
|
* @return AbstractNavigationNode Returns the parent. |
300
|
|
|
*/ |
301
|
|
|
public function getParent() { |
302
|
|
|
return $this->parent; |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* Get the target. |
307
|
|
|
* |
308
|
|
|
* @return string Returns the target. |
309
|
|
|
*/ |
310
|
|
|
public function getTarget() { |
311
|
|
|
return $this->target; |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* Get the URI. |
316
|
|
|
* |
317
|
|
|
* @return string Returns the URI. |
318
|
|
|
*/ |
319
|
|
|
public function getUri() { |
320
|
|
|
return $this->uri; |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
/** |
324
|
|
|
* Get the visible. |
325
|
|
|
* |
326
|
|
|
* @return bool Returns the visible. |
327
|
|
|
*/ |
328
|
|
|
public function getVisible() { |
329
|
|
|
return $this->visible; |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
/** |
333
|
|
|
* Determines if this node is displayable. |
334
|
|
|
* |
335
|
|
|
* @return bool Returns true in case of success, false otherwise. |
336
|
|
|
*/ |
337
|
|
|
public function isDisplayable() { |
338
|
|
|
|
339
|
|
|
if (true === $this->getEnable() && $this->getVisible()) { |
340
|
|
|
return true; |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
foreach ($this->getNodes() as $current) { |
344
|
|
|
if (false === $current->isDisplayable()) { |
345
|
|
|
continue; |
346
|
|
|
} |
347
|
|
|
return true; |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
return false; |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
/** |
354
|
|
|
* Remove a navigation node. |
355
|
|
|
* |
356
|
|
|
* @param AbstractNavigationNode $node The navigation node. |
357
|
|
|
* @return AbstractNavigationNode Returns this navigation node. |
358
|
|
|
*/ |
359
|
|
|
public function removeNode(AbstractNavigationNode $node) { |
360
|
|
|
|
361
|
|
|
if (false === array_key_exists($node->getId(), $this->index)) { |
362
|
|
|
return $this; |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
unset($this->nodes[$this->index[$node->getId()]]); |
366
|
|
|
unset($this->index[$node->setParent(null)->getId()]); |
367
|
|
|
|
368
|
|
|
return $this; |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
/** |
372
|
|
|
* Set the active. |
373
|
|
|
* |
374
|
|
|
* @param bool $active Active ? |
375
|
|
|
* @return AbstractNavigationNode Returns this navigation node. |
376
|
|
|
*/ |
377
|
|
|
public function setActive($active) { |
378
|
|
|
$this->active = $active; |
379
|
|
|
return $this; |
380
|
|
|
} |
381
|
|
|
|
382
|
|
|
/** |
383
|
|
|
* Set the enable. |
384
|
|
|
* |
385
|
|
|
* @param bool $enable Enable ? |
386
|
|
|
* @return AbstractNavigationNode Returns this navigation node. |
387
|
|
|
*/ |
388
|
|
|
public function setEnable($enable) { |
389
|
|
|
$this->enable = $enable; |
390
|
|
|
return $this; |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
/** |
394
|
|
|
* Set the icon. |
395
|
|
|
* |
396
|
|
|
* @param string $icon The icon. |
397
|
|
|
* @return AbstractNavigationNode Returns this navigation node. |
398
|
|
|
*/ |
399
|
|
|
public function setIcon($icon) { |
400
|
|
|
$this->icon = $icon; |
401
|
|
|
return $this; |
402
|
|
|
} |
403
|
|
|
|
404
|
|
|
/** |
405
|
|
|
* Set the id. |
406
|
|
|
* |
407
|
|
|
* @param string $id The id. |
408
|
|
|
* @return AbstractNavigationNode Returns this navigation node. |
409
|
|
|
*/ |
410
|
|
|
protected function setId($id) { |
411
|
|
|
$this->id = $id; |
412
|
|
|
return $this; |
413
|
|
|
} |
414
|
|
|
|
415
|
|
|
/** |
416
|
|
|
* Set the index. |
417
|
|
|
* |
418
|
|
|
* @param array $index The index. |
419
|
|
|
* @return AbstractNavigationNode Returns this navigation node. |
420
|
|
|
*/ |
421
|
|
|
protected function setIndex(array $index) { |
422
|
|
|
$this->index = $index; |
423
|
|
|
return $this; |
424
|
|
|
} |
425
|
|
|
|
426
|
|
|
/** |
427
|
|
|
* Set the label. |
428
|
|
|
* |
429
|
|
|
* @param string $label The label. |
430
|
|
|
* @return AbstractNavigationNode Returns this navigation node. |
431
|
|
|
*/ |
432
|
|
|
public function setLabel(string $label): AbstractNavigationNode { |
433
|
|
|
$this->label = $label; |
434
|
|
|
return $this; |
435
|
|
|
} |
436
|
|
|
|
437
|
|
|
/** |
438
|
|
|
* Set the mather. |
439
|
|
|
* |
440
|
|
|
* @param string $matcher The matcher. |
441
|
|
|
* @return AbstractNavigationNode Returns this navigation node. |
442
|
|
|
*/ |
443
|
|
|
public function setMatcher($matcher) { |
444
|
|
|
$this->matcher = $matcher; |
445
|
|
|
return $this; |
446
|
|
|
} |
447
|
|
|
|
448
|
|
|
/** |
449
|
|
|
* Set the navigation nodes. |
450
|
|
|
* |
451
|
|
|
* @param AbstractNavigationNode[] $nodes The navigation nodes. |
452
|
|
|
* @return AbstractNavigationNode Returns this navigation node. |
453
|
|
|
*/ |
454
|
|
|
protected function setNodes(array $nodes) { |
455
|
|
|
$this->nodes = $nodes; |
456
|
|
|
return $this; |
457
|
|
|
} |
458
|
|
|
|
459
|
|
|
/** |
460
|
|
|
* Set the parent. |
461
|
|
|
* |
462
|
|
|
* @param AbstractNavigationNode $parent The parent. |
463
|
|
|
* @return AbstractNavigationNode Returns this navigation node. |
464
|
|
|
*/ |
465
|
|
|
protected function setParent(AbstractNavigationNode $parent = null) { |
466
|
|
|
$this->parent = $parent; |
467
|
|
|
return $this; |
468
|
|
|
} |
469
|
|
|
|
470
|
|
|
/** |
471
|
|
|
* Set the target. |
472
|
|
|
* |
473
|
|
|
* @param string $target The target. |
474
|
|
|
* @return AbstractNavigationNode Returns this navigation node. |
475
|
|
|
*/ |
476
|
|
|
public function setTarget($target) { |
477
|
|
|
$this->target = $target; |
478
|
|
|
return $this; |
479
|
|
|
} |
480
|
|
|
|
481
|
|
|
/** |
482
|
|
|
* Set the URI. |
483
|
|
|
* |
484
|
|
|
* @param string $uri The URI. |
485
|
|
|
* @return AbstractNavigationNode Returns this navigation node. |
486
|
|
|
*/ |
487
|
|
|
public function setUri($uri) { |
488
|
|
|
$this->uri = $uri; |
489
|
|
|
return $this; |
490
|
|
|
} |
491
|
|
|
|
492
|
|
|
/** |
493
|
|
|
* Set the visible. |
494
|
|
|
* |
495
|
|
|
* @param bool $visible Visible ? |
496
|
|
|
* @return AbstractNavigationNode Returns this navigation node. |
497
|
|
|
*/ |
498
|
|
|
protected function setVisible($visible) { |
499
|
|
|
$this->visible = $visible; |
500
|
|
|
return $this; |
501
|
|
|
} |
502
|
|
|
|
503
|
|
|
/** |
504
|
|
|
* Size. |
505
|
|
|
* |
506
|
|
|
* @return int Returns the size. |
507
|
|
|
*/ |
508
|
|
|
public function size() { |
509
|
|
|
return count($this->getNodes()); |
510
|
|
|
} |
511
|
|
|
} |
512
|
|
|
|