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