1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace gossi\codegen\generator\builder; |
5
|
|
|
|
6
|
|
|
use gossi\codegen\generator\builder\parts\StructBuilderPart; |
7
|
|
|
use gossi\codegen\model\AbstractModel; |
8
|
|
|
use gossi\codegen\model\PhpClass; |
9
|
|
|
|
10
|
|
|
class ClassBuilder extends AbstractBuilder { |
11
|
|
|
|
12
|
|
|
use StructBuilderPart; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* {@inheritDoc} |
16
|
|
|
* @param PhpClass $model |
17
|
|
|
*/ |
18
|
13 |
|
public function build(AbstractModel $model): void { |
19
|
13 |
|
$this->sort($model); |
20
|
|
|
|
21
|
13 |
|
$this->buildHeader($model); |
22
|
|
|
|
23
|
|
|
// signature |
24
|
13 |
|
$this->buildSignature($model); |
25
|
|
|
|
26
|
|
|
// body |
27
|
13 |
|
$this->writer->writeln(" {\n")->indent(); |
28
|
13 |
|
$this->buildTraits($model); |
29
|
13 |
|
$this->buildConstants($model); |
30
|
13 |
|
$this->buildProperties($model); |
31
|
13 |
|
$this->buildMethods($model); |
32
|
13 |
|
$this->writer->outdent()->rtrim()->write('}'); |
33
|
13 |
|
} |
34
|
|
|
|
35
|
13 |
|
private function buildSignature(PhpClass $model) { |
36
|
13 |
|
if ($model->isAbstract()) { |
37
|
2 |
|
$this->writer->write('abstract '); |
38
|
|
|
} |
39
|
|
|
|
40
|
13 |
|
if ($model->isFinal()) { |
41
|
1 |
|
$this->writer->write('final '); |
42
|
|
|
} |
43
|
|
|
|
44
|
13 |
|
$this->writer->write('class '); |
45
|
13 |
|
$this->writer->write($model->getName()); |
46
|
|
|
|
47
|
13 |
|
if ($parentClassName = $model->getParentClassName()) { |
48
|
3 |
|
$this->writer->write(' extends ' . $parentClassName); |
49
|
|
|
} |
50
|
|
|
|
51
|
13 |
|
if ($model->hasInterfaces()) { |
52
|
3 |
|
$this->writer->write(' implements '); |
53
|
3 |
|
$this->writer->write(implode(', ', $model->getInterfaces()->toArray())); |
54
|
|
|
} |
55
|
13 |
|
} |
56
|
|
|
|
57
|
13 |
|
private function sort(PhpClass $model) { |
58
|
13 |
|
$this->sortUseStatements($model); |
59
|
13 |
|
$this->sortConstants($model); |
60
|
13 |
|
$this->sortProperties($model); |
61
|
13 |
|
$this->sortMethods($model); |
62
|
13 |
|
} |
63
|
|
|
|
64
|
|
|
} |
65
|
|
|
|