1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Malezha\Menu; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Container\Container; |
6
|
|
|
use Malezha\Menu\Contracts\Attributes; |
7
|
|
|
use Malezha\Menu\Contracts\Builder as BuilderContract; |
8
|
|
|
use Malezha\Menu\Contracts\Menu as MenuContract; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class Menu |
12
|
|
|
* @package Malezha\Menu |
13
|
|
|
*/ |
14
|
|
|
class Menu implements MenuContract |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var BuilderContract[] |
18
|
|
|
*/ |
19
|
|
|
protected $menuList = []; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var Container |
23
|
|
|
*/ |
24
|
|
|
protected $container; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @inheritDoc |
28
|
|
|
*/ |
29
|
8 |
|
public function __construct(Container $container) |
30
|
|
|
{ |
31
|
8 |
|
$this->container = $container; |
32
|
8 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @inheritDoc |
36
|
|
|
*/ |
37
|
5 |
|
public function make($name, callable $callback = null, $type = Builder::UL, $attributes = [], $activeAttributes = []) |
38
|
|
|
{ |
39
|
5 |
|
$menu = $this->container->makeWith(BuilderContract::class, [ |
|
|
|
|
40
|
5 |
|
'container' => $this->container, |
41
|
5 |
|
'name' => $name, |
42
|
5 |
|
'type' => $type, |
43
|
5 |
|
'attributes' => $this->container->makeWith(Attributes::class, ['attributes' => $attributes]), |
|
|
|
|
44
|
5 |
|
'activeAttributes' => $this->container->makeWith(Attributes::class, ['attributes' => $activeAttributes]), |
|
|
|
|
45
|
|
|
]); |
46
|
|
|
|
47
|
5 |
|
call_if_callable($callback, $menu); |
48
|
|
|
|
49
|
5 |
|
$this->menuList[$name] = $menu; |
50
|
|
|
|
51
|
5 |
|
return $menu; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @inheritDoc |
56
|
|
|
*/ |
57
|
4 |
|
public function get($name) |
58
|
|
|
{ |
59
|
4 |
|
if (array_key_exists($name, $this->menuList) && ($menu = $this->menuList[$name]) instanceof BuilderContract) { |
60
|
3 |
|
return $menu; |
61
|
|
|
} |
62
|
|
|
|
63
|
1 |
|
throw new \RuntimeException('Menu not found'); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @inheritDoc |
68
|
|
|
*/ |
69
|
1 |
|
public function render($name, $view = null) |
70
|
|
|
{ |
71
|
1 |
|
return $this->get($name)->render($view); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @inheritDoc |
76
|
|
|
*/ |
77
|
3 |
|
public function has($name) |
78
|
|
|
{ |
79
|
3 |
|
return array_key_exists($name, $this->menuList); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @inheritDoc |
84
|
|
|
*/ |
85
|
1 |
|
public function forget($name) |
86
|
|
|
{ |
87
|
1 |
|
if ($this->has($name)) { |
88
|
1 |
|
unset($this->menuList[$name]); |
89
|
|
|
} |
90
|
1 |
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @inheritDoc |
94
|
|
|
*/ |
95
|
1 |
|
public function fromArray($name, array $builder) |
96
|
|
|
{ |
97
|
|
|
/** @var BuilderContract $menu */ |
98
|
1 |
|
$menu = $this->container->makeWith(BuilderContract::class, [ |
|
|
|
|
99
|
1 |
|
'attributes' => $this->container->makeWith(Attributes::class, ['attributes' => []]), |
|
|
|
|
100
|
1 |
|
'activeAttributes' => $this->container->makeWith(Attributes::class, ['attributes' => []]), |
|
|
|
|
101
|
|
|
]); |
102
|
|
|
|
103
|
1 |
|
$menu = $menu->fromArray($builder); |
104
|
|
|
|
105
|
1 |
|
$this->menuList[$name] = $menu; |
106
|
|
|
|
107
|
1 |
|
return $menu; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @inheritDoc |
112
|
|
|
*/ |
113
|
1 |
|
public function toArray($name) |
114
|
|
|
{ |
115
|
1 |
|
if (!$this->has($name)) { |
116
|
|
|
throw new \RuntimeException("Menu not found"); |
117
|
|
|
} |
118
|
|
|
|
119
|
1 |
|
return $this->menuList[$name]->toArray(); |
120
|
|
|
} |
121
|
|
|
} |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: