1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the puli/manager package. |
5
|
|
|
* |
6
|
|
|
* (c) Bernhard Schussek <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Puli\Manager\Api\Php; |
13
|
|
|
|
14
|
|
|
use Webmozart\Assert\Assert; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* An argument of a {@link Method}. |
18
|
|
|
* |
19
|
|
|
* @since 1.0 |
20
|
|
|
* |
21
|
|
|
* @author Bernhard Schussek <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class Argument |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
private $name; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string|null |
32
|
|
|
*/ |
33
|
|
|
private $typeHint; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
private $type = 'mixed'; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
private $description; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var string |
47
|
|
|
*/ |
48
|
|
|
private $defaultValue; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Creates a new argument. |
52
|
|
|
* |
53
|
|
|
* @param string $name The name of the argument. |
54
|
|
|
*/ |
55
|
69 |
|
public function __construct($name) |
56
|
|
|
{ |
57
|
69 |
|
Assert::stringNotEmpty($name, 'The argument name must be a non-empty string. Got: %s'); |
58
|
|
|
|
59
|
69 |
|
$this->name = $name; |
60
|
69 |
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Returns the name of the argument. |
64
|
|
|
* |
65
|
|
|
* @return string The argument name. |
66
|
|
|
*/ |
67
|
46 |
|
public function getName() |
68
|
|
|
{ |
69
|
46 |
|
return $this->name; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Returns the type hint of the argument. |
74
|
|
|
* |
75
|
|
|
* @return string|null The type hint or `null` if the argument has no type |
76
|
|
|
* hint. |
77
|
|
|
*/ |
78
|
2 |
|
public function getTypeHint() |
79
|
|
|
{ |
80
|
2 |
|
return $this->typeHint; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Returns whether the argument has a type hint. |
85
|
|
|
* |
86
|
|
|
* @return bool Returns `true` if the argument has a type hint and `false` |
87
|
|
|
* otherwise. |
88
|
|
|
*/ |
89
|
1 |
|
public function hasTypeHint() |
90
|
|
|
{ |
91
|
1 |
|
return null !== $this->typeHint; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Sets the type hint of the argument. |
96
|
|
|
* |
97
|
|
|
* @param string $typeHint The type hint. |
98
|
|
|
* |
99
|
|
|
* @return static The current instance. |
100
|
|
|
*/ |
101
|
35 |
|
public function setTypeHint($typeHint) |
102
|
|
|
{ |
103
|
35 |
|
Assert::stringNotEmpty($typeHint, 'The argument type hint must be a non-empty string. Got: %s'); |
104
|
|
|
|
105
|
32 |
|
$this->typeHint = $typeHint; |
106
|
|
|
|
107
|
32 |
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Removes the type hint of the argument. |
112
|
|
|
* |
113
|
|
|
* @return static The current instance. |
114
|
|
|
*/ |
115
|
2 |
|
public function removeTypeHint() |
116
|
|
|
{ |
117
|
2 |
|
$this->typeHint = null; |
118
|
|
|
|
119
|
2 |
|
return $this; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Returns the type of the argument. |
124
|
|
|
* |
125
|
|
|
* @return string The argument type. |
126
|
|
|
*/ |
127
|
30 |
|
public function getType() |
128
|
|
|
{ |
129
|
30 |
|
return $this->type; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Sets the type of the argument. |
134
|
|
|
* |
135
|
|
|
* @param string $type The argument type. |
136
|
|
|
* |
137
|
|
|
* @return static The current instance. |
138
|
|
|
*/ |
139
|
33 |
|
public function setType($type) |
140
|
|
|
{ |
141
|
33 |
|
Assert::stringNotEmpty($type, 'The argument type must be a non-empty string. Got: %s'); |
142
|
|
|
|
143
|
30 |
|
$this->type = $type; |
144
|
|
|
|
145
|
30 |
|
return $this; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Returns the description of the argument. |
150
|
|
|
* |
151
|
|
|
* @return string The argument description. |
152
|
|
|
*/ |
153
|
30 |
|
public function getDescription() |
154
|
|
|
{ |
155
|
30 |
|
return $this->description; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Sets the description of the argument. |
160
|
|
|
* |
161
|
|
|
* @param string $description The argument description. |
162
|
|
|
* |
163
|
|
|
* @return static The current instance. |
164
|
|
|
*/ |
165
|
33 |
|
public function setDescription($description) |
166
|
|
|
{ |
167
|
33 |
|
Assert::stringNotEmpty($description, 'The argument description must be a non-empty string. Got: %s'); |
168
|
|
|
|
169
|
30 |
|
$this->description = $description; |
170
|
|
|
|
171
|
30 |
|
return $this; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Returns the default value of the argument. |
176
|
|
|
* |
177
|
|
|
* @return string|null The default value as source code or `null` if the |
178
|
|
|
* argument has no default value. |
179
|
|
|
*/ |
180
|
2 |
|
public function getDefaultValue() |
181
|
|
|
{ |
182
|
2 |
|
return $this->defaultValue; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Sets the default value of the argument. |
187
|
|
|
* |
188
|
|
|
* @param string $defaultValue The default value as source code. |
189
|
|
|
* |
190
|
|
|
* @return static The current instance. |
191
|
|
|
*/ |
192
|
7 |
|
public function setDefaultValue($defaultValue) |
193
|
|
|
{ |
194
|
7 |
|
Assert::stringNotEmpty($defaultValue, 'The argument default value must be a non-empty string. Got: %s'); |
195
|
|
|
|
196
|
4 |
|
$this->defaultValue = $defaultValue; |
197
|
|
|
|
198
|
4 |
|
return $this; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Returns whether the argument has a default value. |
203
|
|
|
* |
204
|
|
|
* @return bool Returns `true` if the argument has a default value and |
205
|
|
|
* `false` otherwise. |
206
|
|
|
*/ |
207
|
1 |
|
public function hasDefaultValue() |
208
|
|
|
{ |
209
|
1 |
|
return null !== $this->defaultValue; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Removes the default value of the argument. |
214
|
|
|
* |
215
|
|
|
* If the argument has no default value, this method does nothing. |
216
|
|
|
* |
217
|
|
|
* @return static The current instance. |
218
|
|
|
*/ |
219
|
2 |
|
public function removeDefaultValue() |
220
|
|
|
{ |
221
|
2 |
|
$this->defaultValue = null; |
222
|
|
|
|
223
|
2 |
|
return $this; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Returns the source code of the argument. |
228
|
|
|
* |
229
|
|
|
* @return string The source code. |
230
|
|
|
*/ |
231
|
29 |
|
public function __toString() |
232
|
|
|
{ |
233
|
29 |
|
$string = '$'.$this->name; |
234
|
|
|
|
235
|
29 |
|
if ($this->typeHint) { |
|
|
|
|
236
|
22 |
|
$string = $this->typeHint.' '.$string; |
237
|
|
|
} |
238
|
|
|
|
239
|
29 |
|
if ($this->defaultValue) { |
240
|
1 |
|
$string .= ' = '.$this->defaultValue; |
241
|
|
|
} |
242
|
|
|
|
243
|
29 |
|
return $string; |
244
|
|
|
} |
245
|
|
|
} |
246
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: