|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Murtukov\PHPCodeGenerator; |
|
6
|
|
|
|
|
7
|
|
|
class Property extends DependencyAwareGenerator |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* Special value to represent that there is no argument passed. |
|
11
|
|
|
*/ |
|
12
|
|
|
public const NO_PARAM = INF; |
|
13
|
|
|
|
|
14
|
|
|
public string $name; |
|
15
|
|
|
public ?Comment $docBlock = null; |
|
16
|
|
|
public bool $isNullable = false; |
|
17
|
|
|
public bool $isStatic = false; |
|
18
|
|
|
public bool $isConst = false; |
|
19
|
|
|
|
|
20
|
|
|
private string $value = ''; |
|
21
|
|
|
private string $modifier; |
|
22
|
|
|
private string $typeHint; |
|
23
|
|
|
|
|
24
|
6 |
|
public function __construct(string $name, ?string $modifier, string $typeHint = '', $defaultValue = self::NO_PARAM) |
|
25
|
|
|
{ |
|
26
|
6 |
|
$this->name = $name; |
|
27
|
6 |
|
$this->modifier = $modifier ?? Modifier::PUBLIC; |
|
28
|
6 |
|
$this->typeHint = $this->resolveQualifier($typeHint); |
|
29
|
|
|
|
|
30
|
6 |
|
if (INF !== $defaultValue) { |
|
31
|
4 |
|
$this->value = Utils::stringify($defaultValue); |
|
32
|
|
|
|
|
33
|
4 |
|
if (null === $defaultValue) { |
|
34
|
2 |
|
$this->isNullable = true; |
|
35
|
|
|
} |
|
36
|
|
|
} |
|
37
|
6 |
|
} |
|
38
|
|
|
|
|
39
|
4 |
|
public static function new( |
|
40
|
|
|
string $name, |
|
41
|
|
|
?string $modifier = Modifier::PUBLIC, |
|
42
|
|
|
string $typeHint = '', |
|
43
|
|
|
$value = self::NO_PARAM |
|
44
|
|
|
) { |
|
45
|
4 |
|
return new static($name, $modifier, $typeHint, $value); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
8 |
|
public function generate(): string |
|
49
|
|
|
{ |
|
50
|
8 |
|
$docBlock = $this->docBlock ? "$this->docBlock\n" : ''; |
|
51
|
8 |
|
$value = $this->value ? " = $this->value" : ''; |
|
52
|
8 |
|
$isStatic = $this->isStatic ? 'static ' : ''; |
|
53
|
|
|
|
|
54
|
8 |
|
$typeHint = ''; |
|
55
|
8 |
|
if ($this->typeHint) { |
|
56
|
4 |
|
if ($this->isNullable) { |
|
57
|
4 |
|
$typeHint = "?"; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
4 |
|
$typeHint .= "$this->typeHint "; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
8 |
|
if ($this->isConst) { |
|
64
|
5 |
|
return "$docBlock$this->modifier const $this->name$value"; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
6 |
|
return "{$docBlock}{$this->modifier} {$isStatic}{$typeHint}$$this->name{$value}"; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
1 |
|
public function getName(): string |
|
71
|
|
|
{ |
|
72
|
1 |
|
return $this->name; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
1 |
|
public function setName(string $name): self |
|
76
|
|
|
{ |
|
77
|
1 |
|
$this->name = $name; |
|
78
|
|
|
|
|
79
|
1 |
|
return $this; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
1 |
|
public function getModifier(): string |
|
83
|
|
|
{ |
|
84
|
1 |
|
return $this->modifier; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
1 |
|
public function getDefaultValue(): string |
|
88
|
|
|
{ |
|
89
|
1 |
|
return $this->value; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
1 |
|
public function setDefaultValue($value): self |
|
93
|
|
|
{ |
|
94
|
1 |
|
$this->value = Utils::stringify($value); |
|
95
|
|
|
|
|
96
|
1 |
|
return $this; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
1 |
|
public function setStatic(): self |
|
100
|
|
|
{ |
|
101
|
1 |
|
$this->isStatic = true; |
|
102
|
1 |
|
$this->isConst = false; |
|
103
|
|
|
|
|
104
|
1 |
|
return $this; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
1 |
|
public function unsetStatic(): self |
|
108
|
|
|
{ |
|
109
|
1 |
|
$this->isStatic = false; |
|
110
|
|
|
|
|
111
|
1 |
|
return $this; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
4 |
|
public function setConst(): self |
|
115
|
|
|
{ |
|
116
|
4 |
|
$this->isConst = true; |
|
117
|
4 |
|
$this->isStatic = false; |
|
118
|
|
|
|
|
119
|
4 |
|
return $this; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
1 |
|
public function unsetConst() |
|
123
|
|
|
{ |
|
124
|
1 |
|
$this->isConst = false; |
|
125
|
|
|
|
|
126
|
1 |
|
return $this; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
1 |
|
public function setPublic(): self |
|
130
|
|
|
{ |
|
131
|
1 |
|
$this->modifier = 'public'; |
|
132
|
|
|
|
|
133
|
1 |
|
return $this; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
1 |
|
public function setPrivate(): self |
|
137
|
|
|
{ |
|
138
|
1 |
|
$this->modifier = 'private'; |
|
139
|
|
|
|
|
140
|
1 |
|
return $this; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
1 |
|
public function setProtected(): self |
|
144
|
|
|
{ |
|
145
|
1 |
|
$this->modifier = 'protected'; |
|
146
|
|
|
|
|
147
|
1 |
|
return $this; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
1 |
|
public function getTypeHint(): string |
|
151
|
|
|
{ |
|
152
|
1 |
|
return $this->typeHint; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
1 |
|
public function setTypeHint(string $typeHint): self |
|
156
|
|
|
{ |
|
157
|
1 |
|
$this->typeHint = $this->resolveQualifier($typeHint); |
|
158
|
|
|
|
|
159
|
1 |
|
return $this; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
1 |
|
public function setNullable() |
|
163
|
|
|
{ |
|
164
|
1 |
|
$this->isNullable = true; |
|
165
|
1 |
|
} |
|
166
|
|
|
|
|
167
|
1 |
|
public function unsetNullable() |
|
168
|
|
|
{ |
|
169
|
1 |
|
$this->isNullable = false; |
|
170
|
1 |
|
} |
|
171
|
|
|
} |
|
172
|
|
|
|