1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dazzle\Util\Factory; |
4
|
|
|
|
5
|
|
|
use Dazzle\Throwable\Exception\Logic\IllegalCallException; |
6
|
|
|
use Dazzle\Throwable\Exception\Logic\IllegalFieldException; |
7
|
|
|
|
8
|
|
|
trait FactoryTrait |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var mixed[] |
12
|
|
|
*/ |
13
|
|
|
protected $params; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var callable[] |
17
|
|
|
*/ |
18
|
|
|
protected $definitions; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* |
22
|
|
|
*/ |
23
|
56 |
|
public function __construct() |
24
|
|
|
{ |
25
|
56 |
|
$this->params = []; |
26
|
56 |
|
$this->definitions = []; |
27
|
56 |
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* |
31
|
|
|
*/ |
32
|
56 |
|
public function __destruct() |
33
|
|
|
{ |
34
|
56 |
|
unset($this->params); |
35
|
56 |
|
unset($this->definitions); |
36
|
56 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @see FactoryInterface::bindParam |
40
|
|
|
*/ |
41
|
14 |
|
public function bindParam($name, $value) |
42
|
|
|
{ |
43
|
14 |
|
$this->params[$name] = $value; |
44
|
|
|
|
45
|
14 |
|
return $this; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @see FactoryInterface::unbindParam |
50
|
|
|
*/ |
51
|
6 |
|
public function unbindParam($name) |
52
|
|
|
{ |
53
|
6 |
|
unset($this->params[$name]); |
54
|
|
|
|
55
|
6 |
|
return $this; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @see FactoryInterface::getParam |
60
|
|
|
*/ |
61
|
6 |
View Code Duplication |
public function getParam($name) |
|
|
|
|
62
|
|
|
{ |
63
|
6 |
|
if (!array_key_exists($name, $this->params)) |
64
|
|
|
{ |
65
|
2 |
|
throw new IllegalFieldException("Factory does not posses param [$name]."); |
66
|
|
|
} |
67
|
|
|
|
68
|
4 |
|
return $this->invoke($this->params[$name]); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @see FactoryInterface::hasParam |
73
|
|
|
*/ |
74
|
10 |
|
public function hasParam($param) |
75
|
|
|
{ |
76
|
10 |
|
return array_key_exists($param, $this->params); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @see FactoryInterface::getParams |
81
|
|
|
*/ |
82
|
4 |
|
public function getParams() |
83
|
|
|
{ |
84
|
4 |
|
return $this->params; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @see FactoryInterface::define |
89
|
|
|
*/ |
90
|
14 |
|
public function define($name, callable $factoryMethod) |
91
|
|
|
{ |
92
|
14 |
|
$this->definitions[$name] = $factoryMethod; |
93
|
|
|
|
94
|
14 |
|
return $this; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @see FactoryInterface::remove |
99
|
|
|
*/ |
100
|
6 |
|
public function remove($name) |
101
|
|
|
{ |
102
|
6 |
|
unset($this->definitions[$name]); |
103
|
|
|
|
104
|
6 |
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @see FactoryInterface::getDefinition |
109
|
|
|
*/ |
110
|
4 |
|
public function getDefinition($name) |
111
|
|
|
{ |
112
|
4 |
|
if (!isset($this->definitions[$name])) |
113
|
|
|
{ |
114
|
2 |
|
throw new IllegalFieldException("Factory does not posses definition [$name]."); |
115
|
|
|
} |
116
|
|
|
|
117
|
2 |
|
return $this->definitions[$name]; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @see FactoryInterface::hasDefinition |
122
|
|
|
*/ |
123
|
10 |
|
public function hasDefinition($name) |
124
|
|
|
{ |
125
|
10 |
|
return array_key_exists($name, $this->definitions); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @see FactoryInterface::getDefinitions |
130
|
|
|
*/ |
131
|
4 |
|
public function getDefinitions() |
132
|
|
|
{ |
133
|
4 |
|
return $this->definitions; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @see FactoryInterface::create |
138
|
|
|
*/ |
139
|
4 |
|
public function create($name, $args = []) |
140
|
|
|
{ |
141
|
4 |
|
if (!isset($this->definitions[$name])) |
142
|
|
|
{ |
143
|
2 |
|
throw new IllegalCallException("Factory does not posses definition for [$name]."); |
144
|
|
|
} |
145
|
|
|
|
146
|
2 |
|
return call_user_func_array($this->definitions[$name], (array) $args); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param mixed $value |
151
|
|
|
* @return mixed |
152
|
|
|
*/ |
153
|
4 |
|
private function invoke($value) |
154
|
|
|
{ |
155
|
4 |
|
return is_callable($value) ? $value() : $value; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.