Completed
Push — master ( 3e9cba...5c40ca )
by WEBEWEB
01:24
created

getAlphabeticalTreeNodeParent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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
     * Matcher.
62
     *
63
     * @var string
64
     */
65
    private $matcher;
66
67
    /**
68
     * Navigation nodes.
69
     *
70
     * @var AbstractNavigationNode[]
71
     */
72
    private $nodes;
73
74
    /**
75
     * Parent.
76
     *
77
     * @var AbstractNavigationNode
78
     */
79
    private $parent;
80
81
    /**
82
     * Target.
83
     *
84
     * @var string
85
     */
86
    private $target;
87
88
    /**
89
     * URI.
90
     *
91
     * @var string
92
     */
93
    private $uri;
94
95
    /**
96
     * Visible ?
97
     *
98
     * @var bool
99
     */
100
    private $visible;
101
102
    /**
103
     * Constructor.
104
     *
105
     * @param string $name The name.
106
     * @param string|null $icon The icon.
107
     * @param string|null $uri The URI.
108
     * @param string $matcher The matcher.
109
     */
110
    protected function __construct($name, $icon = null, $uri = null, $matcher = self::NAVIGATION_MATCHER_URL) {
111
        $this->setActive(false);
112
        $this->setEnable(false);
113
        $this->setIcon($icon);
114
        $this->setId($name);
115
        $this->setIndex([]);
116
        $this->setMatcher($matcher);
117
        $this->setNodes([]);
118
        $this->setParent(null);
119
        $this->setTarget(null);
120
        $this->setUri($uri);
121
        $this->setVisible(true);
122
    }
123
124
    /**
125
     * Add a navigation node.
126
     *
127
     * @param AbstractNavigationNode $node The navigation node.
128
     * @return AbstractNavigationNode Returns this navigation node.
129
     */
130
    public function addNode(AbstractNavigationNode $node) {
131
        $this->index[$node->getId()] = $this->size();
132
        $this->nodes[]               = $node->setParent($this);
133
        return $this;
134
    }
135
136
    /**
137
     * Clear the navigation nodes.
138
     *
139
     * @return AbstractNavigationNode Returns this navigation node.
140
     */
141
    public function clearNodes() {
142
        foreach ($this->getNodes() as $node) {
143
            $this->removeNode($node);
144
        }
145
        return $this;
146
    }
147
148
    /**
149
     * Get the active.
150
     *
151
     * @return bool Returns the active.
152
     */
153
    public function getActive() {
154
        return $this->active;
155
    }
156
157
    /**
158
     * {@inheritdoc}
159
     */
160
    public function getAlphabeticalTreeNodeLabel() {
161
        return $this->getId();
162
    }
163
164
    /**
165
     * {@inheritdoc}
166
     */
167
    public function getAlphabeticalTreeNodeParent() {
168
        return $this->getParent();
169
    }
170
171
    /**
172
     * Get the enable.
173
     *
174
     * @return bool Returns the enable.
175
     */
176
    public function getEnable() {
177
        return $this->enable;
178
    }
179
180
    /**
181
     * Get the first navigation node.
182
     *
183
     * @return AbstractNavigationNode|null Returns the first navigation node in case of success, null otherwise.
184
     */
185
    public function getFirstNode() {
186
        return $this->getNodeAt(0);
187
    }
188
189
    /**
190
     * Get the icon.
191
     *
192
     * @return string Returns the icon.
193
     */
194
    public function getIcon() {
195
        return $this->icon;
196
    }
197
198
    /**
199
     * Get the id.
200
     *
201
     * @return string Returns the id.
202
     */
203
    public function getId() {
204
        return $this->id;
205
    }
206
207
    /**
208
     * Get the last navigation node.
209
     *
210
     * @return AbstractNavigationNode|null Returns the last navigation node in case of success, null otherwise.
211
     */
212
    public function getLastNode() {
213
        return $this->getNodeAt($this->size() - 1);
214
    }
215
216
    /**
217
     * Get the matcher.
218
     *
219
     * @return string Returns the matcher.
220
     */
221
    public function getMatcher() {
222
        return $this->matcher;
223
    }
224
225
    /**
226
     * Get a navigation node at.
227
     *
228
     * @param int $position The position.
229
     * @return AbstractNavigationNode|null Returns the navigation node in case of success, null otherwise.
230
     */
231
    public function getNodeAt($position) {
232
233
        if ($position < 0 || $this->size() <= $position) {
234
            return null;
235
        }
236
237
        return $this->getNodes()[$position];
238
    }
239
240
    /**
241
     * Get a navigation node by id.
242
     *
243
     * @param string $id The id.
244
     * @param bool $recursively Recursively ?
245
     * @return AbstractNavigationNode Returns the navigation node in case of success, null otherwise.
246
     */
247
    public function getNodeById($id, $recursively = false) {
248
249
        if (true === array_key_exists($id, $this->index)) {
250
            return $this->getNodeAt($this->index[$id]);
251
        }
252
253
        if (false === $recursively) {
254
            return null;
255
        }
256
257
        foreach ($this->getNodes() as $current) {
258
259
            $found = $current->getNodeById($id, true);
260
            if (null === $found) {
261
                continue;
262
            }
263
264
            return $found;
265
        }
266
267
        return null;
268
    }
269
270
    /**
271
     * Get the navigation nodes.
272
     *
273
     * @return AbstractNavigationNode[] Returns the navigation nodes.
274
     */
275
    public function getNodes() {
276
        return $this->nodes;
277
    }
278
279
    /**
280
     * Get the parent.
281
     *
282
     * @return AbstractNavigationNode Returns the parent.
283
     */
284
    public function getParent() {
285
        return $this->parent;
286
    }
287
288
    /**
289
     * Get the target.
290
     *
291
     * @return string Returns the target.
292
     */
293
    public function getTarget() {
294
        return $this->target;
295
    }
296
297
    /**
298
     * Get the URI.
299
     *
300
     * @return string Returns the URI.
301
     */
302
    public function getUri() {
303
        return $this->uri;
304
    }
305
306
    /**
307
     * Get the visible.
308
     *
309
     * @return bool Returns the visible.
310
     */
311
    public function getVisible() {
312
        return $this->visible;
313
    }
314
315
    /**
316
     * Determines if this node is displayable.
317
     *
318
     * @return bool Returns true in case of success, false otherwise.
319
     */
320
    public function isDisplayable() {
321
322
        if (true === $this->getEnable() && $this->getVisible()) {
323
            return true;
324
        }
325
326
        foreach ($this->getNodes() as $current) {
327
            if (false === $current->isDisplayable()) {
328
                continue;
329
            }
330
            return true;
331
        }
332
333
        return false;
334
    }
335
336
    /**
337
     * Remove a navigation node.
338
     *
339
     * @param AbstractNavigationNode $node The navigation node.
340
     * @return AbstractNavigationNode Returns this navigation node.
341
     */
342
    public function removeNode(AbstractNavigationNode $node) {
343
344
        if (false === array_key_exists($node->getId(), $this->index)) {
345
            return $this;
346
        }
347
348
        unset($this->nodes[$this->index[$node->getId()]]);
349
        unset($this->index[$node->setParent(null)->getId()]);
350
351
        return $this;
352
    }
353
354
    /**
355
     * Set the active.
356
     *
357
     * @param bool $active Active ?
358
     * @return AbstractNavigationNode Returns this navigation node.
359
     */
360
    public function setActive($active) {
361
        $this->active = $active;
362
        return $this;
363
    }
364
365
    /**
366
     * Set the enable.
367
     *
368
     * @param bool $enable Enable ?
369
     * @return AbstractNavigationNode Returns this navigation node.
370
     */
371
    public function setEnable($enable) {
372
        $this->enable = $enable;
373
        return $this;
374
    }
375
376
    /**
377
     * Set the icon.
378
     *
379
     * @param string $icon The icon.
380
     * @return AbstractNavigationNode Returns this navigation node.
381
     */
382
    public function setIcon($icon) {
383
        $this->icon = $icon;
384
        return $this;
385
    }
386
387
    /**
388
     * Set the id.
389
     *
390
     * @param string $id The id.
391
     * @return AbstractNavigationNode Returns this navigation node.
392
     */
393
    protected function setId($id) {
394
        $this->id = $id;
395
        return $this;
396
    }
397
398
    /**
399
     * Set the index.
400
     *
401
     * @param array $index The index.
402
     * @return AbstractNavigationNode Returns this navigation node.
403
     */
404
    protected function setIndex(array $index) {
405
        $this->index = $index;
406
        return $this;
407
    }
408
409
    /**
410
     * Set the mather.
411
     *
412
     * @param string $matcher The matcher.
413
     * @return AbstractNavigationNode Returns this navigation node.
414
     */
415
    public function setMatcher($matcher) {
416
        $this->matcher = $matcher;
417
        return $this;
418
    }
419
420
    /**
421
     * Set the navigation nodes.
422
     *
423
     * @param AbstractNavigationNode[] $nodes The navigation nodes.
424
     * @return AbstractNavigationNode Returns this navigation node.
425
     */
426
    protected function setNodes(array $nodes) {
427
        $this->nodes = $nodes;
428
        return $this;
429
    }
430
431
    /**
432
     * Set the parent.
433
     *
434
     * @param AbstractNavigationNode $parent The parent.
435
     * @return AbstractNavigationNode Returns this navigation node.
436
     */
437
    protected function setParent(AbstractNavigationNode $parent = null) {
438
        $this->parent = $parent;
439
        return $this;
440
    }
441
442
    /**
443
     * Set the target.
444
     *
445
     * @param string $target The target.
446
     * @return AbstractNavigationNode Returns this navigation node.
447
     */
448
    public function setTarget($target) {
449
        $this->target = $target;
450
        return $this;
451
    }
452
453
    /**
454
     * Set the URI.
455
     *
456
     * @param string $uri The URI.
457
     * @return AbstractNavigationNode Returns this navigation node.
458
     */
459
    public function setUri($uri) {
460
        $this->uri = $uri;
461
        return $this;
462
    }
463
464
    /**
465
     * Set the visible.
466
     *
467
     * @param bool $visible Visible ?
468
     * @return AbstractNavigationNode Returns this navigation node.
469
     */
470
    protected function setVisible($visible) {
471
        $this->visible = $visible;
472
        return $this;
473
    }
474
475
    /**
476
     * Size.
477
     *
478
     * @return int Returns the size.
479
     */
480
    public function size() {
481
        return count($this->getNodes());
482
    }
483
}
484