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