1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace Everlution\Navigation\Navigation; |
6
|
|
|
|
7
|
|
|
use Everlution\Navigation\Matcher\Matcher; |
8
|
|
|
use Everlution\Navigation\RootNavigationItem; |
9
|
|
|
use Everlution\Navigation\NavigationItem; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class BasicNavigation. |
13
|
|
|
* @author Ivan Barlog <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class BasicNavigation implements Navigation |
16
|
|
|
{ |
17
|
|
|
/** @var NavigationItem[] */ |
18
|
|
|
private $ancestors = []; |
19
|
|
|
/** @var NavigationItem */ |
20
|
|
|
private $current = null; |
21
|
|
|
|
22
|
|
|
public function __construct(RootNavigationItem $root, Matcher $matcher) |
23
|
|
|
{ |
24
|
|
|
$this->process($root, $matcher); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param NavigationItem $item |
29
|
|
|
* @return bool |
30
|
|
|
*/ |
31
|
|
|
public function isAncestor(NavigationItem $item): bool |
32
|
|
|
{ |
33
|
|
|
return in_array($item, $this->ancestors); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @return array |
38
|
|
|
*/ |
39
|
|
|
public function getBreadcrumbs(): array |
40
|
|
|
{ |
41
|
|
|
return $this->ancestors; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return NavigationItem |
46
|
|
|
* @throws CurrentItemNotMatchedException |
47
|
|
|
*/ |
48
|
|
|
public function getCurrent(): NavigationItem |
49
|
|
|
{ |
50
|
|
|
if (!$this->current instanceof NavigationItem) { |
51
|
|
|
throw new CurrentItemNotMatchedException(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return $this->current; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param NavigationItem $item |
59
|
|
|
* @param Matcher $matcher |
60
|
|
|
* @param int $depth |
61
|
|
|
* @return NavigationItem|null |
62
|
|
|
*/ |
63
|
|
|
private function process(NavigationItem &$item, Matcher &$matcher, int $depth = 0) |
64
|
|
|
{ |
65
|
|
|
$this->ancestors[$depth] = $item; |
66
|
|
|
|
67
|
|
|
if ($matcher->isCurrent($item)) { |
68
|
|
|
$this->current = $item; |
69
|
|
|
$this->cleanAncestors(); |
70
|
|
|
|
71
|
|
|
return $item; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$childDepth = ++$depth; |
75
|
|
|
/** @var NavigationItem $child */ |
76
|
|
|
foreach ($item->getChildren() as $child) { |
77
|
|
|
$result = $this->process($child, $matcher, $childDepth); |
78
|
|
|
|
79
|
|
|
if ($result instanceof NavigationItem) { |
80
|
|
|
return $result; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
unset($this->ancestors[$depth]); |
85
|
|
|
|
86
|
|
|
return null; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
private function cleanAncestors() |
90
|
|
|
{ |
91
|
|
|
array_shift($this->ancestors); |
92
|
|
|
array_pop($this->ancestors); |
93
|
|
|
$this->ancestors = array_values($this->ancestors); |
|
|
|
|
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..