1
|
|
|
<?php |
2
|
|
|
namespace Malezha\Menu\Factory; |
3
|
|
|
|
4
|
|
|
use Illuminate\Contracts\Config\Repository; |
5
|
|
|
use Illuminate\Contracts\Container\Container; |
6
|
|
|
use Malezha\Menu\Contracts\Element; |
7
|
|
|
use Malezha\Menu\Contracts\ElementFactory; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class AbstractElementFactory |
11
|
|
|
* @package Malezha\Menu\Factory |
12
|
|
|
*/ |
13
|
|
|
abstract class AbstractElementFactory implements ElementFactory |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var Container |
17
|
|
|
*/ |
18
|
|
|
protected $app; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
protected $parameters = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @inheritdoc |
27
|
|
|
*/ |
28
|
28 |
|
public function __construct(Container $container) |
29
|
|
|
{ |
30
|
28 |
|
$this->app = $container; |
31
|
28 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param string $class |
35
|
|
|
* @return array |
36
|
|
|
*/ |
37
|
28 |
|
protected function getElementConfig($class) |
38
|
|
|
{ |
39
|
28 |
|
return $this->app->make(Repository::class)->get('menu.elements')[$class]; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param string $name |
44
|
|
|
* @return mixed |
45
|
|
|
*/ |
46
|
9 |
|
protected function getParameter($name) |
47
|
|
|
{ |
48
|
9 |
|
if (array_key_exists($name, $this->parameters)) { |
49
|
9 |
|
return $this->parameters[$name]; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return null; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param string $name |
57
|
|
|
* @param mixed $value |
58
|
|
|
* @return $this |
59
|
|
|
*/ |
60
|
25 |
|
protected function setParameter($name, $value) |
61
|
|
|
{ |
62
|
25 |
|
$this->parameters[$name] = $value; |
63
|
|
|
|
64
|
25 |
|
return $this; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param string $name |
69
|
|
|
* @return $this |
70
|
|
|
*/ |
71
|
|
|
protected function unsetParameter($name) |
72
|
|
|
{ |
73
|
|
|
unset($this->parameters[$name]); |
74
|
|
|
|
75
|
|
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param string $name |
80
|
|
|
* @return bool |
81
|
|
|
*/ |
82
|
|
|
protected function existsParameter($name) |
83
|
|
|
{ |
84
|
|
|
return array_key_exists($name, $this->parameters); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param array $parameters |
89
|
|
|
* @return array |
90
|
|
|
*/ |
91
|
27 |
|
protected function mergeParameters($parameters = []) |
92
|
|
|
{ |
93
|
27 |
|
return array_merge($this->parameters, $parameters); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @inheritDoc |
98
|
|
|
*/ |
99
|
9 |
|
function __get($name) |
100
|
|
|
{ |
101
|
9 |
|
return $this->getParameter($name); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @inheritDoc |
106
|
|
|
*/ |
107
|
25 |
|
function __set($name, $value) |
108
|
|
|
{ |
109
|
25 |
|
$this->setParameter($name, $value); |
110
|
25 |
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @inheritDoc |
114
|
|
|
*/ |
115
|
|
|
function __isset($name) |
116
|
|
|
{ |
117
|
|
|
return $this->existsParameter($name); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @inheritDoc |
122
|
|
|
*/ |
123
|
|
|
function __unset($name) |
124
|
|
|
{ |
125
|
|
|
$this->unsetParameter($name); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @inheritDoc |
130
|
|
|
*/ |
131
|
|
|
public function toArray() |
132
|
|
|
{ |
133
|
|
|
return $this->build()->toArray(); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param Element $element |
138
|
|
|
*/ |
139
|
22 |
|
protected function setDisplayRule(Element $element) |
140
|
|
|
{ |
141
|
22 |
|
if (array_key_exists('displayRule', $this->parameters) && method_exists($element, 'setDisplayRule')) { |
142
|
22 |
|
$element->setDisplayRule($this->parameters['displayRule']); |
|
|
|
|
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
} |
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: