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