1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace WsdlToPhp\PhpGenerator\Element; |
6
|
|
|
|
7
|
|
|
class PhpClass extends AbstractElement |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var string |
11
|
|
|
*/ |
12
|
|
|
const PHP_DECLARATION = 'class'; |
13
|
|
|
/** |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
const PHP_ABSTRACT_KEYWORD = 'abstract'; |
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
const PHP_IMPLEMENTS_KEYWORD = 'implements'; |
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
const PHP_EXTENDS_KEYWORD = 'extends'; |
25
|
|
|
/** |
26
|
|
|
* @var bool |
27
|
|
|
*/ |
28
|
|
|
protected $abstract; |
29
|
|
|
/** |
30
|
|
|
* @var string|PhpClass |
31
|
|
|
*/ |
32
|
|
|
protected $extends; |
33
|
|
|
/** |
34
|
|
|
* @var string[]|PhpClass[] |
35
|
|
|
*/ |
36
|
|
|
protected $interfaces; |
37
|
|
|
/** |
38
|
|
|
* @param string $name |
39
|
|
|
* @param bool $abstract |
40
|
|
|
* @param string|PhpClass|null $extends |
41
|
|
|
* @param string[]|PhpClass[] $interfaces |
42
|
|
|
*/ |
43
|
106 |
|
public function __construct($name, $abstract = false, $extends = null, array $interfaces = []) |
44
|
|
|
{ |
45
|
106 |
|
parent::__construct($name); |
46
|
|
|
$this |
47
|
102 |
|
->setAbstract($abstract) |
48
|
102 |
|
->setExtends($extends) |
49
|
102 |
|
->setInterfaces($interfaces); |
50
|
102 |
|
} |
51
|
|
|
/** |
52
|
|
|
* @throws \InvalidArgumentException |
53
|
|
|
* @param bool $abstract |
54
|
|
|
* @return PhpClass |
55
|
|
|
*/ |
56
|
102 |
|
public function setAbstract($abstract): PhpClass |
57
|
|
|
{ |
58
|
102 |
|
if (!is_bool($abstract)) { |
59
|
2 |
|
throw new \InvalidArgumentException(sprintf('Abstract must be a boolean, "%s" given', gettype($abstract))); |
60
|
|
|
} |
61
|
102 |
|
$this->abstract = $abstract; |
62
|
102 |
|
return $this; |
63
|
|
|
} |
64
|
|
|
/** |
65
|
|
|
* @return bool |
66
|
|
|
*/ |
67
|
42 |
|
public function getAbstract(): bool |
68
|
|
|
{ |
69
|
42 |
|
return $this->abstract; |
70
|
|
|
} |
71
|
|
|
/** |
72
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
56 |
|
protected function getPhpAbstract(): string |
75
|
|
|
{ |
76
|
56 |
|
return $this->getAbstract() === false ? '' : static::PHP_ABSTRACT_KEYWORD . ' '; |
77
|
|
|
} |
78
|
|
|
/** |
79
|
|
|
* @throws \InvalidArgumentException |
80
|
|
|
* @param string|PhpClass|null $extends |
81
|
|
|
* @return PhpClass |
82
|
|
|
*/ |
83
|
102 |
|
public function setExtends($extends): PhpClass |
84
|
|
|
{ |
85
|
102 |
|
if (!self::extendsIsValid($extends)) { |
86
|
|
|
throw new \InvalidArgumentException('Extends must be a string or a PhpClass instance'); |
87
|
|
|
} |
88
|
102 |
|
$this->extends = $extends; |
89
|
102 |
|
return $this; |
90
|
|
|
} |
91
|
|
|
/** |
92
|
|
|
* @param string|PhpClass|null $extends |
93
|
|
|
* @return bool |
94
|
|
|
*/ |
95
|
102 |
|
public static function extendsIsValid($extends): bool |
96
|
|
|
{ |
97
|
102 |
|
return $extends === null || self::stringIsValid($extends, true, true) || $extends instanceof PhpClass; |
98
|
|
|
} |
99
|
|
|
/** |
100
|
|
|
* @return string|PhpClass |
101
|
|
|
*/ |
102
|
44 |
|
public function getExtends() |
103
|
|
|
{ |
104
|
44 |
|
return $this->extends; |
105
|
|
|
} |
106
|
|
|
/** |
107
|
|
|
* @return string |
108
|
|
|
*/ |
109
|
56 |
|
protected function getPhpExtends(): string |
110
|
|
|
{ |
111
|
56 |
|
$extends = $this->getExtends(); |
112
|
56 |
|
return empty($extends) ? '' : sprintf(' %s %s', static::PHP_EXTENDS_KEYWORD, ($extends instanceof PhpClass ? $extends->getName() : $extends)); |
113
|
|
|
} |
114
|
|
|
/** |
115
|
|
|
* @throws \InvalidArgumentException |
116
|
|
|
* @param string[]|PhpClass[] $interfaces |
117
|
|
|
* @return PhpClass |
118
|
|
|
*/ |
119
|
102 |
|
public function setInterfaces(array $interfaces = []): PhpClass |
120
|
|
|
{ |
121
|
102 |
|
if (!self::interfacesAreValid($interfaces)) { |
122
|
|
|
throw new \InvalidArgumentException('Interfaces are not valid'); |
123
|
|
|
} |
124
|
102 |
|
$this->interfaces = $interfaces; |
125
|
102 |
|
return $this; |
126
|
|
|
} |
127
|
|
|
/** |
128
|
|
|
* @param string[]|PhpClass[] $interfaces |
129
|
|
|
* @return bool |
130
|
|
|
*/ |
131
|
102 |
|
public static function interfacesAreValid(array $interfaces = []): bool |
132
|
|
|
{ |
133
|
102 |
|
$valid = true; |
134
|
102 |
|
foreach ($interfaces as $interface) { |
135
|
24 |
|
$valid &= self::interfaceIsValid($interface); |
136
|
|
|
} |
137
|
102 |
|
return (bool) $valid; |
138
|
|
|
} |
139
|
|
|
/** |
140
|
|
|
* @param string|PhpClass $interface |
141
|
|
|
* @return bool |
142
|
|
|
*/ |
143
|
24 |
|
public static function interfaceIsValid($interface): bool |
144
|
|
|
{ |
145
|
24 |
|
return self::stringIsValid($interface) || $interface instanceof PhpClass; |
146
|
|
|
} |
147
|
|
|
/** |
148
|
|
|
* |
149
|
|
|
* @return string[]|PhpClass[] |
150
|
|
|
*/ |
151
|
58 |
|
public function getInterfaces(): array |
152
|
|
|
{ |
153
|
58 |
|
return $this->interfaces; |
154
|
|
|
} |
155
|
|
|
/** |
156
|
|
|
* @return string |
157
|
|
|
*/ |
158
|
56 |
|
protected function getPhpInterfaces(): string |
159
|
|
|
{ |
160
|
56 |
|
$interfaces = []; |
161
|
56 |
|
foreach ($this->getInterfaces() as $interface) { |
162
|
22 |
|
$interfaces[] = $this->getPhpInterface($interface); |
163
|
|
|
} |
164
|
56 |
|
return empty($interfaces) ? '' : sprintf(' %s%s', static::PHP_IMPLEMENTS_KEYWORD, implode(',', $interfaces)); |
165
|
|
|
} |
166
|
|
|
/** |
167
|
|
|
* @param string|PhpClass $interface |
168
|
|
|
* @return string |
169
|
|
|
*/ |
170
|
22 |
|
protected function getPhpInterface($interface): string |
171
|
|
|
{ |
172
|
22 |
|
return sprintf(' %s', is_string($interface) ? $interface : $interface->getName()); |
173
|
|
|
} |
174
|
|
|
/** |
175
|
|
|
* @return string |
176
|
|
|
*/ |
177
|
56 |
|
public function getPhpDeclaration(): string |
178
|
|
|
{ |
179
|
56 |
|
return trim(sprintf('%s%s %s%s%s', $this->getPhpAbstract(), static::PHP_DECLARATION, $this->getPhpName(), $this->getPhpExtends(), $this->getPhpInterfaces())); |
180
|
|
|
} |
181
|
|
|
/** |
182
|
|
|
* defines authorized children element types |
183
|
|
|
* @return string[] |
184
|
|
|
*/ |
185
|
26 |
|
public function getChildrenTypes(): array |
186
|
|
|
{ |
187
|
|
|
return [ |
188
|
26 |
|
'string', |
189
|
|
|
PhpAnnotationBlock::class, |
190
|
|
|
PhpMethod::class, |
191
|
|
|
PhpConstant::class, |
192
|
|
|
PhpProperty::class, |
193
|
|
|
]; |
194
|
|
|
} |
195
|
|
|
/** |
196
|
|
|
* Allows to indicate that children are contained by brackets, |
197
|
|
|
* in the case the method returns true, getBracketBeforeChildren |
198
|
|
|
* is called instead of getLineBeforeChildren and getBracketAfterChildren |
199
|
|
|
* is called instead of getLineAfterChildren, but be aware that these methods |
200
|
|
|
* call the two others |
201
|
|
|
* @return bool |
202
|
|
|
*/ |
203
|
24 |
|
public function useBracketsForChildren(): bool |
204
|
|
|
{ |
205
|
24 |
|
return true; |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|