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