1
|
|
|
<?php |
2
|
|
|
namespace gossi\codegen\model\parts; |
3
|
|
|
|
4
|
|
|
use gossi\codegen\model\PhpParameter; |
5
|
|
|
use gossi\docblock\tags\ParamTag; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Parameters Part |
9
|
|
|
* |
10
|
|
|
* For all models that can have parameters |
11
|
|
|
* |
12
|
|
|
* @author Thomas Gossmann |
13
|
|
|
*/ |
14
|
|
|
trait ParametersPart { |
15
|
|
|
|
16
|
|
|
/** @var array */ |
17
|
|
|
private $parameters = []; |
18
|
|
|
|
19
|
47 |
|
private function initParameters() { |
20
|
|
|
// $this->parameters = new ArrayList(); |
21
|
47 |
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Sets a collection of parameters |
25
|
|
|
* |
26
|
|
|
* Note: clears all parameters before setting the new ones |
27
|
|
|
* |
28
|
|
|
* @param PhpParameter[] $parameters |
29
|
|
|
* @return $this |
30
|
|
|
*/ |
31
|
1 |
|
public function setParameters(array $parameters) { |
32
|
1 |
|
$this->parameters = []; |
33
|
1 |
|
foreach ($parameters as $parameter) { |
34
|
1 |
|
$this->addParameter($parameter); |
35
|
1 |
|
} |
36
|
|
|
|
37
|
1 |
|
return $this; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Adds a parameter |
42
|
|
|
* |
43
|
|
|
* @param PhpParameter $parameter |
44
|
|
|
* @return $this |
45
|
|
|
*/ |
46
|
22 |
|
public function addParameter(PhpParameter $parameter) { |
47
|
22 |
|
$this->parameters[] = $parameter; |
48
|
|
|
|
49
|
22 |
|
return $this; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Checks whether a parameter exists |
54
|
|
|
* |
55
|
|
|
* @param string $name parameter name |
56
|
|
|
* @return bool `true` if a parameter exists and `false` if not |
57
|
|
|
*/ |
58
|
2 |
|
public function hasParameter($name) { |
59
|
2 |
|
foreach ($this->parameters as $param) { |
60
|
2 |
|
if ($param->getName() == $name) { |
61
|
2 |
|
return true; |
62
|
|
|
} |
63
|
2 |
|
} |
64
|
|
|
|
65
|
2 |
|
return false; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* A quick way to add a parameter which is created from the given parameters |
70
|
|
|
* |
71
|
|
|
* @param string $name |
72
|
|
|
* @param null|string $type |
73
|
|
|
* @param mixed $defaultValue omit the argument to define no default value |
74
|
|
|
* |
75
|
|
|
* @return $this |
76
|
|
|
*/ |
77
|
1 |
|
public function addSimpleParameter($name, $type = null, $defaultValue = null) { |
78
|
1 |
|
$parameter = new PhpParameter($name); |
79
|
1 |
|
$parameter->setType($type); |
80
|
|
|
|
81
|
1 |
|
if (2 < func_num_args()) { |
82
|
1 |
|
$parameter->setValue($defaultValue); |
83
|
1 |
|
} |
84
|
|
|
|
85
|
1 |
|
$this->addParameter($parameter); |
86
|
1 |
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* A quick way to add a parameter with description which is created from the given parameters |
91
|
|
|
* |
92
|
|
|
* @param string $name |
93
|
|
|
* @param null|string $type |
94
|
|
|
* @param null|string $typeDescription |
95
|
|
|
* @param mixed $defaultValue omit the argument to define no default value |
96
|
|
|
* |
97
|
|
|
* @return $this |
98
|
|
|
*/ |
99
|
1 |
|
public function addSimpleDescParameter($name, $type = null, $typeDescription = null, $defaultValue = null) { |
100
|
1 |
|
$parameter = new PhpParameter($name); |
101
|
1 |
|
$parameter->setType($type); |
102
|
1 |
|
$parameter->setTypeDescription($typeDescription); |
103
|
|
|
|
104
|
1 |
|
if (3 < func_num_args() == 3) { |
105
|
1 |
|
$parameter->setValue($defaultValue); |
106
|
1 |
|
} |
107
|
|
|
|
108
|
1 |
|
$this->addParameter($parameter); |
109
|
1 |
|
return $this; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Returns a parameter by index or name |
114
|
|
|
* |
115
|
|
|
* @param string|int $nameOrIndex |
116
|
|
|
* @throws \InvalidArgumentException |
117
|
|
|
* @return PhpParameter |
118
|
|
|
*/ |
119
|
9 |
|
public function getParameter($nameOrIndex) { |
120
|
9 |
|
if (is_int($nameOrIndex)) { |
121
|
2 |
|
$this->checkPosition($nameOrIndex); |
122
|
1 |
|
return $this->parameters[$nameOrIndex]; |
123
|
|
|
} |
124
|
|
|
|
125
|
8 |
|
foreach ($this->parameters as $param) { |
126
|
7 |
|
if ($param->getName() === $nameOrIndex) { |
127
|
7 |
|
return $param; |
128
|
|
|
} |
129
|
7 |
|
} |
130
|
|
|
|
131
|
1 |
|
throw new \InvalidArgumentException(sprintf('There is no parameter named "%s".', $nameOrIndex)); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Replaces a parameter at a given position |
136
|
|
|
* |
137
|
|
|
* @param int $position |
138
|
|
|
* @param PhpParameter $parameter |
139
|
|
|
* @throws \InvalidArgumentException |
140
|
|
|
* @return $this |
141
|
|
|
*/ |
142
|
2 |
|
public function replaceParameter($position, PhpParameter $parameter) { |
143
|
2 |
|
$this->checkPosition($position); |
144
|
1 |
|
$this->parameters[$position] = $parameter; |
145
|
|
|
|
146
|
1 |
|
return $this; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Remove a parameter at a given position |
151
|
|
|
* |
152
|
|
|
* @param int|string|PhpParameter $param |
153
|
|
|
* @return $this |
154
|
|
|
*/ |
155
|
2 |
|
public function removeParameter($param) { |
156
|
2 |
|
if (is_int($param)) { |
157
|
2 |
|
$this->removeParameterByPosition($param); |
158
|
1 |
|
} else if (is_string($param)) { |
159
|
|
|
$this->removeParameterByName($param); |
160
|
|
|
} else if ($param instanceof PhpParameter) { |
161
|
|
|
$this->removeParameterByName($param->getName()); |
162
|
|
|
} |
163
|
|
|
|
164
|
1 |
|
return $this; |
165
|
|
|
} |
166
|
|
|
|
167
|
2 |
|
private function removeParameterByPosition($position) { |
168
|
2 |
|
$this->checkPosition($position); |
169
|
1 |
|
unset($this->parameters[$position]); |
170
|
1 |
|
$this->parameters = array_values($this->parameters); |
171
|
1 |
|
} |
172
|
|
|
|
173
|
|
|
private function removeParameterByName($name) { |
174
|
|
|
$position = null; |
175
|
|
|
foreach ($this->parameters as $index => $param) { |
176
|
|
|
if ($param->getName() == $name) { |
177
|
|
|
$position = $index; |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
if ($position !== null) { |
182
|
|
|
$this->removeParameterByPosition($position); |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|
186
|
4 |
|
private function checkPosition($position) { |
187
|
4 |
|
if ($position < 0 || $position > count($this->parameters)) { |
188
|
3 |
|
throw new \InvalidArgumentException(sprintf('The position must be in the range [0, %d].', count($this->parameters))); |
189
|
|
|
} |
190
|
1 |
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Returns a collection of parameters |
194
|
|
|
* |
195
|
|
|
* @return array |
196
|
|
|
*/ |
197
|
16 |
|
public function getParameters() { |
198
|
16 |
|
return $this->parameters; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Returns the docblock |
203
|
|
|
* |
204
|
|
|
* @return Docblock |
205
|
|
|
*/ |
206
|
|
|
abstract protected function getDocblock(); |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Generates docblock for params |
210
|
|
|
*/ |
211
|
14 |
|
protected function generateParamDocblock() { |
212
|
14 |
|
$docblock = $this->getDocblock(); |
213
|
14 |
|
$tags = $docblock->getTags('param'); |
214
|
14 |
|
foreach ($this->parameters as $param) { |
215
|
7 |
|
$ptag = $param->getDocblockTag(); |
216
|
|
|
|
217
|
7 |
|
$tag = $tags->find($ptag, function (ParamTag $tag, ParamTag $ptag) { |
218
|
1 |
|
return $tag->getVariable() == $ptag->getVariable(); |
219
|
7 |
|
}); |
220
|
|
|
|
221
|
|
|
// try to update existing docblock first |
222
|
7 |
|
if ($tag !== null) { |
223
|
1 |
|
$tag->setDescription($ptag->getDescription()); |
224
|
1 |
|
$tag->setType($ptag->getType()); |
225
|
1 |
|
} |
226
|
|
|
|
227
|
|
|
// ... append if it doesn't exist |
228
|
|
|
else { |
229
|
6 |
|
$docblock->appendTag($ptag); |
230
|
|
|
} |
231
|
14 |
|
} |
232
|
14 |
|
} |
233
|
|
|
|
234
|
|
|
} |
235
|
|
|
|