1 | <?php declare(strict_types=1); |
||
15 | class ClassGenerator extends AbstractGenerator |
||
16 | { |
||
17 | use AbstractFinalTrait; |
||
18 | |||
19 | /** OOP public visibility */ |
||
20 | const VISIBILITY_PUBLIC = 'public'; |
||
21 | |||
22 | /** OOP protected visibility */ |
||
23 | const VISIBILITY_PROTECTED = 'protected'; |
||
24 | |||
25 | /** OOP private visibility */ |
||
26 | const VISIBILITY_PRIVATE = 'private'; |
||
27 | |||
28 | /** @var string Class name */ |
||
29 | protected $className; |
||
30 | |||
31 | /** @var string Parent class name */ |
||
32 | protected $parentClassName; |
||
33 | |||
34 | /** @var string Class namespace */ |
||
35 | protected $namespace; |
||
36 | |||
37 | /** @var array Collection of class uses */ |
||
38 | protected $uses = []; |
||
39 | |||
40 | /** @var array Collection of class interfaces */ |
||
41 | protected $interfaces = []; |
||
42 | |||
43 | /** @var array Collection of class used traits */ |
||
44 | protected $traits = []; |
||
45 | |||
46 | /** @var string Multi-line file description */ |
||
47 | protected $fileDescription; |
||
48 | |||
49 | /** |
||
50 | * ClassGenerator constructor. |
||
51 | * |
||
52 | * @param string $className Class name |
||
53 | * @param AbstractGenerator $parent Parent generator |
||
54 | */ |
||
55 | 24 | public function __construct(string $className = null, AbstractGenerator $parent = null) |
|
61 | |||
62 | /** |
||
63 | * Set class file description. |
||
64 | * |
||
65 | * @param array $description Collection of class file description lines |
||
66 | * |
||
67 | * @return ClassGenerator |
||
68 | */ |
||
69 | 1 | public function defDescription(array $description): ClassGenerator |
|
80 | |||
81 | /** |
||
82 | * Set class namespace. |
||
83 | * |
||
84 | * @param string $namespace |
||
85 | * |
||
86 | * @return ClassGenerator |
||
87 | */ |
||
88 | 23 | public function defNamespace(string $namespace): ClassGenerator |
|
94 | |||
95 | /** |
||
96 | * Set class name. |
||
97 | * |
||
98 | * @param string $className |
||
99 | * |
||
100 | * @return ClassGenerator |
||
101 | */ |
||
102 | 1 | public function defName(string $className): ClassGenerator |
|
108 | |||
109 | /** |
||
110 | * Set class use. |
||
111 | * |
||
112 | * @param string $use Use class name |
||
113 | * @param string $alias Use class name |
||
114 | * |
||
115 | * @return ClassGenerator |
||
116 | */ |
||
117 | 2 | public function defUse(string $use, string $alias = null): ClassGenerator |
|
128 | |||
129 | /** |
||
130 | * Set parent class. |
||
131 | * |
||
132 | * @param string $className Parent class name |
||
133 | * |
||
134 | * @return ClassGenerator |
||
135 | */ |
||
136 | 2 | public function defExtends(string $className): ClassGenerator |
|
142 | |||
143 | /** |
||
144 | * Set implements interfaces. |
||
145 | * |
||
146 | * @param string $interfaceName Interface name |
||
147 | * |
||
148 | * @return ClassGenerator |
||
149 | */ |
||
150 | 3 | public function defImplements(string $interfaceName): ClassGenerator |
|
156 | |||
157 | /** |
||
158 | * Set class trait use. |
||
159 | * |
||
160 | * @param string $trait Trait class name |
||
161 | * |
||
162 | * @return ClassGenerator |
||
163 | */ |
||
164 | 1 | public function defTrait(string $trait): ClassGenerator |
|
170 | |||
171 | /** |
||
172 | * Set protected class property. |
||
173 | * |
||
174 | * @param string $name Property name |
||
175 | * @param string $type Property type |
||
176 | * @param mixed $value Property value |
||
177 | * @param string $description Property description |
||
178 | * |
||
179 | * @return PropertyGenerator |
||
180 | */ |
||
181 | public function defProtectedProperty(string $name, string $type, $value, string $description = null): PropertyGenerator |
||
182 | { |
||
183 | return $this->defProperty($name, $type, $value, $description)->defProtected(); |
||
184 | } |
||
185 | |||
186 | /** |
||
187 | * Set class property. |
||
188 | * |
||
189 | * @param string $name Property name |
||
190 | * @param string $type Property type |
||
191 | * @param mixed $value Property value |
||
192 | * @param string $description Property description |
||
193 | * |
||
194 | * @return PropertyGenerator |
||
195 | */ |
||
196 | public function defProperty(string $name, string $type, $value = null, string $description = null): PropertyGenerator |
||
197 | { |
||
198 | return (new PropertyGenerator($name, $value, $this)) |
||
199 | ->setIndentation($this->indentation) |
||
200 | ->increaseIndentation() |
||
201 | ->defComment() |
||
202 | ->defVar($type, $description) |
||
203 | ->end(); |
||
204 | } |
||
205 | |||
206 | /** |
||
207 | * Set protected static class property. |
||
208 | * |
||
209 | * @param string $name Property name |
||
210 | * @param string $type Property type |
||
211 | * @param mixed $value Property value |
||
212 | * @param string $description Property description |
||
213 | * |
||
214 | * @return PropertyGenerator |
||
215 | */ |
||
216 | public function defProtectedStaticProperty(string $name, string $type, $value, string $description = null): PropertyGenerator |
||
217 | { |
||
218 | return $this->defStaticProperty($name, $type, $value, $description)->defProtected(); |
||
219 | } |
||
220 | |||
221 | /** |
||
222 | * Set static class property. |
||
223 | * |
||
224 | * @param string $name Property name |
||
225 | * @param string $type Property type |
||
226 | * @param mixed $value Property value |
||
227 | * @param string $description Property description |
||
228 | * |
||
229 | * @return PropertyGenerator |
||
230 | */ |
||
231 | public function defStaticProperty(string $name, string $type, $value, string $description = null): PropertyGenerator |
||
232 | { |
||
233 | return $this->defProperty($name, $type, $value, $description)->defStatic(); |
||
234 | } |
||
235 | |||
236 | /** |
||
237 | * Set protected class method. |
||
238 | * |
||
239 | * @param string $name Method name |
||
240 | * |
||
241 | * @return MethodGenerator |
||
242 | */ |
||
243 | 1 | public function defProtectedMethod(string $name): MethodGenerator |
|
247 | |||
248 | /** |
||
249 | * Set public class method. |
||
250 | * |
||
251 | * @param string $name Method name |
||
252 | * |
||
253 | * @return MethodGenerator |
||
254 | */ |
||
255 | 6 | public function defMethod(string $name): MethodGenerator |
|
259 | |||
260 | /** |
||
261 | * Set protected static class method. |
||
262 | * |
||
263 | * @param string $name Method name |
||
264 | * |
||
265 | * @return MethodGenerator |
||
266 | */ |
||
267 | 1 | public function defProtectedStaticMethod(string $name): MethodGenerator |
|
271 | |||
272 | /** |
||
273 | * Set public static class method. |
||
274 | * |
||
275 | * @param string $name Method name |
||
276 | * |
||
277 | * @return MethodGenerator |
||
278 | */ |
||
279 | 2 | public function defStaticMethod(string $name): MethodGenerator |
|
283 | |||
284 | /** |
||
285 | * Set class constant. |
||
286 | * |
||
287 | * @param string $name |
||
288 | * @param mixed $value |
||
289 | * @param string $type |
||
290 | * @param string $description |
||
291 | * |
||
292 | * @return $this|ClassConstantGenerator |
||
293 | */ |
||
294 | public function defConstant(string $name, $value, string $type, string $description): ClassConstantGenerator |
||
303 | |||
304 | /** |
||
305 | * @inheritdoc |
||
306 | * @throws \InvalidArgumentException |
||
307 | * @throws ClassNameNotFoundException |
||
308 | */ |
||
309 | 22 | public function code(): string |
|
337 | |||
338 | 21 | protected function buildNamespaceCode(array $formattedCode = []): array |
|
351 | |||
352 | 20 | protected function buildFileDescriptionCode(array $formattedCode): array |
|
361 | |||
362 | 20 | protected function buildUsesCode(array $formattedCode): array |
|
376 | |||
377 | /** |
||
378 | * Build class definition. |
||
379 | * |
||
380 | * @return string Function definition |
||
381 | */ |
||
382 | 19 | protected function buildDefinition() |
|
391 | |||
392 | /** |
||
393 | * Build traits definition code. |
||
394 | * |
||
395 | * @param array $formattedCode Collection of code |
||
396 | * @param string $innerIndentation Inner indentation |
||
397 | * |
||
398 | * @return array Collection of code with trait uses |
||
399 | */ |
||
400 | 20 | protected function buildTraitsCode(array $formattedCode, string $innerIndentation): array |
|
414 | |||
415 | /** |
||
416 | * Get generated class name. |
||
417 | * |
||
418 | * @return string Generated class name |
||
419 | */ |
||
420 | public function getClassName(): string |
||
424 | |||
425 | /** |
||
426 | * Get generated class namespace. |
||
427 | * |
||
428 | * @return string Generated class namespace |
||
429 | */ |
||
430 | public function getNamespace(): string |
||
434 | } |
||
435 |
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: