|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Murtukov\PHPCodeGenerator; |
|
6
|
|
|
|
|
7
|
|
|
use function join; |
|
8
|
|
|
|
|
9
|
|
|
class PhpClass extends OOPStructure |
|
10
|
|
|
{ |
|
11
|
|
|
protected string $extends = ''; |
|
12
|
|
|
protected bool $isAbstract = false; |
|
13
|
|
|
protected bool $isFinal = false; |
|
14
|
|
|
protected array $implements = []; |
|
15
|
|
|
|
|
16
|
3 |
|
public function setExtends(string $fqcn): self |
|
17
|
|
|
{ |
|
18
|
3 |
|
$this->extends = $this->resolveQualifier($fqcn); |
|
19
|
|
|
|
|
20
|
3 |
|
return $this; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
2 |
|
public function addImplements(string ...$classNames): self |
|
24
|
|
|
{ |
|
25
|
2 |
|
foreach ($classNames as $name) { |
|
26
|
2 |
|
$this->implements[] = $this->resolveQualifier($name); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
2 |
|
return $this; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
1 |
|
public function removeImplements(): self |
|
33
|
|
|
{ |
|
34
|
1 |
|
$this->implements = []; |
|
35
|
|
|
|
|
36
|
1 |
|
return $this; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
10 |
|
protected function buildImplements(): string |
|
40
|
|
|
{ |
|
41
|
10 |
|
return !empty($this->implements) ? ' implements '.join(', ', $this->implements) : ''; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
10 |
|
protected function buildExtends(): string |
|
45
|
|
|
{ |
|
46
|
10 |
|
if ($this->extends) { |
|
47
|
4 |
|
return " extends $this->extends"; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
6 |
|
return ''; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
1 |
|
public function setName(string $name): self |
|
54
|
|
|
{ |
|
55
|
1 |
|
$this->name = $name; |
|
56
|
|
|
|
|
57
|
1 |
|
return $this; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param mixed $value |
|
62
|
|
|
*/ |
|
63
|
2 |
|
public function addConst(string $name, $value, string $modifier = Modifier::PUBLIC): self |
|
64
|
|
|
{ |
|
65
|
|
|
return $this |
|
66
|
2 |
|
->append(Property::new($name, $modifier, '', $value)->setConst()); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
2 |
|
public function addProperty(string $name, string $modifier = Modifier::PUBLIC, string $type = '', $defaulValue = ''): self |
|
70
|
|
|
{ |
|
71
|
2 |
|
return $this->append(new Property($name, $modifier, $type, $defaulValue)); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
1 |
|
public function addMethod(string $name, string $modifier = 'public', string $returnType = ''): self |
|
75
|
|
|
{ |
|
76
|
|
|
return $this |
|
77
|
1 |
|
->append(new Method($name, $modifier, $returnType)) |
|
78
|
1 |
|
->emptyLine() |
|
79
|
|
|
; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
2 |
|
public function createMethod(string $name, string $modifier = 'public', string $returnType = ''): Method |
|
83
|
|
|
{ |
|
84
|
2 |
|
$method = new Method($name, $modifier, $returnType); |
|
85
|
|
|
|
|
86
|
2 |
|
$this->append($method)->emptyLine(); |
|
87
|
|
|
|
|
88
|
2 |
|
return $method; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
1 |
|
public function createConstructor(string $modifier = 'public'): Method |
|
92
|
|
|
{ |
|
93
|
1 |
|
$constructor = new Method('__construct', $modifier, ''); |
|
94
|
|
|
|
|
95
|
1 |
|
$this->append($constructor)->emptyLine(); |
|
96
|
|
|
|
|
97
|
1 |
|
return $constructor; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
10 |
|
public function generate(): string |
|
101
|
|
|
{ |
|
102
|
|
|
return <<<CODE |
|
103
|
10 |
|
{$this->buildDocBlock()}{$this->buildPrefix()}class $this->name{$this->buildExtends()}{$this->buildImplements()} |
|
104
|
|
|
{ |
|
105
|
10 |
|
{$this->generateContent()} |
|
106
|
|
|
} |
|
107
|
|
|
CODE; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
10 |
|
private function buildPrefix(): string |
|
111
|
|
|
{ |
|
112
|
10 |
|
$prefix = ''; |
|
113
|
|
|
|
|
114
|
10 |
|
if ($this->isFinal) { |
|
115
|
1 |
|
$prefix .= 'final '; |
|
116
|
9 |
|
} elseif ($this->isAbstract) { |
|
117
|
1 |
|
$prefix .= 'abstract '; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
10 |
|
return $prefix; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
1 |
|
public function isFinal(): bool |
|
124
|
|
|
{ |
|
125
|
1 |
|
return $this->isFinal; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
1 |
|
public function setFinal(): self |
|
129
|
|
|
{ |
|
130
|
1 |
|
$this->isFinal = true; |
|
131
|
|
|
|
|
132
|
|
|
// Class cannot be final and abstract at the same time |
|
133
|
1 |
|
$this->isAbstract = false; |
|
134
|
|
|
|
|
135
|
1 |
|
return $this; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
1 |
|
public function unsetFinal(): self |
|
139
|
|
|
{ |
|
140
|
1 |
|
$this->isFinal = false; |
|
141
|
|
|
|
|
142
|
1 |
|
return $this; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
1 |
|
public function isAbstract(): bool |
|
146
|
|
|
{ |
|
147
|
1 |
|
return $this->isAbstract; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
2 |
|
public function setAbstract(): self |
|
151
|
|
|
{ |
|
152
|
2 |
|
$this->isAbstract = true; |
|
153
|
|
|
|
|
154
|
|
|
// Class cannot be final and abstract at the same time |
|
155
|
2 |
|
$this->isFinal = false; |
|
156
|
|
|
|
|
157
|
2 |
|
return $this; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
1 |
|
public function unsetAbstract(): self |
|
161
|
|
|
{ |
|
162
|
1 |
|
$this->isAbstract = false; |
|
163
|
|
|
|
|
164
|
1 |
|
return $this; |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
|