1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WsdlToPhp\PackageGenerator\Model; |
4
|
|
|
|
5
|
|
|
use WsdlToPhp\PackageGenerator\Generator\Utils; |
6
|
|
|
use WsdlToPhp\PackageGenerator\Container\Model\StructValue as StructValueContainer; |
7
|
|
|
use WsdlToPhp\PackageGenerator\Container\Model\StructAttribute as StructAttributeContainer; |
8
|
|
|
use WsdlToPhp\PackageGenerator\Generator\Generator; |
9
|
|
|
use WsdlToPhp\PackageGenerator\ConfigurationReader\StructReservedMethod; |
10
|
|
|
use WsdlToPhp\PackageGenerator\ConfigurationReader\StructArrayReservedMethod; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class Struct stands for an available struct described in the WSDL |
14
|
|
|
*/ |
15
|
|
|
class Struct extends AbstractModel |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
const DOC_SUB_PACKAGE_STRUCTS = 'Structs'; |
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
const DOC_SUB_PACKAGE_ENUMERATIONS = 'Enumerations'; |
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
const DOC_SUB_PACKAGE_ARRAYS = 'Arrays'; |
29
|
|
|
/** |
30
|
|
|
* Attributes of the struct |
31
|
|
|
* @var StructAttributeContainer |
32
|
|
|
*/ |
33
|
|
|
private $attributes; |
34
|
|
|
/** |
35
|
|
|
* Is the struct a restriction with defined values ? |
36
|
|
|
* @var bool |
37
|
|
|
*/ |
38
|
|
|
private $isRestriction = false; |
39
|
|
|
/** |
40
|
|
|
* If the struct is a restriction with values, then store values |
41
|
|
|
* @var StructValueContainer |
42
|
|
|
*/ |
43
|
|
|
private $values; |
44
|
|
|
/** |
45
|
|
|
* If the struct is a union with types, then store types |
46
|
|
|
* @var string[] |
47
|
|
|
*/ |
48
|
|
|
private $types; |
49
|
|
|
/** |
50
|
|
|
* Define if the urrent struct is a concrete struct or just a virtual struct to store meta informations |
51
|
|
|
* @var bool |
52
|
|
|
*/ |
53
|
|
|
private $isStruct = false; |
54
|
|
|
/** |
55
|
|
|
* Main constructor |
56
|
|
|
* @see AbstractModel::__construct() |
57
|
|
|
* @uses Struct::setStruct() |
58
|
|
|
* @param Generator $generator |
59
|
|
|
* @param string $name the original name |
60
|
|
|
* @param bool $isStruct defines if it's a real sruct or not |
61
|
|
|
* @param bool $isRestriction defines if it's an enumeration or not |
62
|
|
|
*/ |
63
|
|
|
public function __construct(Generator $generator, $name, $isStruct = true, $isRestriction = false) |
64
|
|
|
{ |
65
|
|
|
parent::__construct($generator, $name); |
66
|
|
|
$this->setStruct($isStruct)->setRestriction($isRestriction)->setAttributes(new StructAttributeContainer($generator))->setValues(new StructValueContainer($generator)); |
67
|
|
|
} |
68
|
|
|
/** |
69
|
|
|
* Returns the contextual part of the class name for the package |
70
|
|
|
* @see AbstractModel::getContextualPart() |
71
|
|
|
* @uses Struct::isRestriction() |
72
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
|
|
public function getContextualPart() |
75
|
|
|
{ |
76
|
|
|
$part = $this->getGenerator()->getOptionStructsFolder(); |
77
|
|
|
if ($this->isRestriction()) { |
78
|
|
|
$part = $this->getGenerator()->getOptionEnumsFolder(); |
79
|
|
|
} elseif ($this->isArray()) { |
80
|
|
|
$part = $this->getGenerator()->getOptionArraysFolder(); |
81
|
|
|
} |
82
|
|
|
return $part; |
83
|
|
|
} |
84
|
|
|
/** |
85
|
|
|
* Returns the sub package name which the model belongs to |
86
|
|
|
* Must be overridden by sub classes |
87
|
|
|
* @see AbstractModel::getDocSubPackages() |
88
|
|
|
* @uses Struct::isRestriction() |
89
|
|
|
* @return array |
90
|
|
|
*/ |
91
|
|
|
public function getDocSubPackages() |
92
|
|
|
{ |
93
|
|
|
$package = self::DOC_SUB_PACKAGE_STRUCTS; |
94
|
|
|
if ($this->isRestriction()) { |
95
|
|
|
$package = self::DOC_SUB_PACKAGE_ENUMERATIONS; |
96
|
|
|
} elseif ($this->isArray()) { |
97
|
|
|
$package = self::DOC_SUB_PACKAGE_ARRAYS; |
98
|
|
|
} |
99
|
|
|
return array( |
100
|
|
|
$package, |
101
|
|
|
); |
102
|
|
|
} |
103
|
|
|
/** |
104
|
|
|
* Returns true if the current struct is a collection of values (like an array) |
105
|
|
|
* @uses AbstractModel::getName() |
106
|
|
|
* @uses Struct::countOwnAttributes() |
107
|
|
|
* @return bool |
108
|
|
|
*/ |
109
|
|
|
public function isArray() |
110
|
|
|
{ |
111
|
|
|
return ((($this->isStruct() && $this->countOwnAttributes() === 1) || (!$this->isStruct() && $this->countOwnAttributes() <= 1)) && stripos($this->getName(), 'array') !== false); |
112
|
|
|
} |
113
|
|
|
/** |
114
|
|
|
* Returns the attributes of the struct and potentially from the parent class |
115
|
|
|
* @uses AbstractModel::getInheritance() |
116
|
|
|
* @uses Struct::isStruct() |
117
|
|
|
* @uses Struct::getAttributes() |
118
|
|
|
* @param bool $includeInheritanceAttributes include the attributes of parent class, default parent attributes are not included. If true, then the array is an associative array containing and index "attribute" for the StructAttribute object and an index "model" for the Struct object. |
119
|
|
|
* @param bool $requiredFirst places the required attributes first, then the not required in order to have the _contrust method with the required attribute at first |
120
|
|
|
* @return StructAttributeContainer |
121
|
|
|
*/ |
122
|
|
|
public function getAttributes($includeInheritanceAttributes = false, $requiredFirst = false) |
123
|
|
|
{ |
124
|
|
|
if ($includeInheritanceAttributes === false && $requiredFirst === false) { |
125
|
|
|
$attributes = $this->attributes; |
126
|
|
|
} else { |
127
|
|
|
$attributes = $this->getAllAttributes($includeInheritanceAttributes, $requiredFirst); |
128
|
|
|
} |
129
|
|
|
return $attributes; |
130
|
|
|
} |
131
|
|
|
/** |
132
|
|
|
* @param bool $includeInheritanceAttributes |
133
|
|
|
* @param bool $requiredFirst |
134
|
|
|
* @return StructAttributeContainer |
135
|
|
|
*/ |
136
|
|
|
protected function getAllAttributes($includeInheritanceAttributes, $requiredFirst) |
137
|
|
|
{ |
138
|
|
|
$allAttributes = new StructAttributeContainer($this->getGenerator()); |
139
|
|
|
if ($includeInheritanceAttributes === true) { |
140
|
|
|
$this->addInheritanceAttributes($allAttributes); |
141
|
|
|
} |
142
|
|
|
foreach ($this->attributes as $attribute) { |
143
|
|
|
$allAttributes->add($attribute); |
144
|
|
|
} |
145
|
|
|
if ($requiredFirst === true) { |
146
|
|
|
$attributes = $this->putRequiredFirst($allAttributes); |
147
|
|
|
} else { |
148
|
|
|
$attributes = $allAttributes; |
149
|
|
|
} |
150
|
|
|
return $attributes; |
151
|
|
|
} |
152
|
|
|
/** |
153
|
|
|
* @param StructAttributeContainer $attributes |
154
|
|
|
*/ |
155
|
|
|
protected function addInheritanceAttributes(StructAttributeContainer $attributes) |
156
|
|
|
{ |
157
|
|
|
if ($this->getInheritance() != '' && ($model = $this->getInheritanceStruct()) instanceof Struct) { |
158
|
|
|
while ($model instanceof Struct && $model->isStruct()) { |
159
|
|
|
foreach ($model->getAttributes() as $attribute) { |
160
|
|
|
$attributes->add($attribute); |
161
|
|
|
} |
162
|
|
|
$model = $model->getInheritanceStruct(); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
/** |
167
|
|
|
* @param StructAttributeContainer $allAttributes |
168
|
|
|
* @return StructAttributeContainer |
169
|
|
|
*/ |
170
|
|
|
protected function putRequiredFirst(StructAttributeContainer $allAttributes) |
171
|
|
|
{ |
172
|
|
|
$attributes = new StructAttributeContainer($this->getGenerator()); |
173
|
|
|
$requiredAttributes = new StructAttributeContainer($this->getGenerator()); |
174
|
|
|
$notRequiredAttributes = new StructAttributeContainer($this->getGenerator()); |
175
|
|
|
foreach ($allAttributes as $attribute) { |
176
|
|
|
if ($attribute->isRequired()) { |
177
|
|
|
$requiredAttributes->add($attribute); |
178
|
|
|
} else { |
179
|
|
|
$notRequiredAttributes->add($attribute); |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
foreach ($requiredAttributes as $attribute) { |
183
|
|
|
$attributes->add($attribute); |
184
|
|
|
} |
185
|
|
|
foreach ($notRequiredAttributes as $attribute) { |
186
|
|
|
$attributes->add($attribute); |
187
|
|
|
} |
188
|
|
|
unset($requiredAttributes, $notRequiredAttributes); |
189
|
|
|
return $attributes; |
190
|
|
|
} |
191
|
|
|
/** |
192
|
|
|
* Returns the number of own attributes |
193
|
|
|
* @uses Struct::getAttributes() |
194
|
|
|
* @return int |
195
|
|
|
*/ |
196
|
|
|
public function countOwnAttributes() |
197
|
|
|
{ |
198
|
|
|
return $this->getAttributes(false, false)->count(); |
199
|
|
|
} |
200
|
|
|
/** |
201
|
|
|
* Sets the attributes of the struct |
202
|
|
|
* @param StructAttributeContainer $structAttributeContainer |
203
|
|
|
* @return Struct |
204
|
|
|
*/ |
205
|
|
|
public function setAttributes(StructAttributeContainer $structAttributeContainer) |
206
|
|
|
{ |
207
|
|
|
$this->attributes = $structAttributeContainer; |
208
|
|
|
return $this; |
209
|
|
|
} |
210
|
|
|
/** |
211
|
|
|
* Adds attribute based on its original name |
212
|
|
|
* @throws \InvalidArgumentException |
213
|
|
|
* @param string $attributeName the attribute name |
214
|
|
|
* @param string $attributeType the attribute type |
215
|
|
|
* @return Struct |
216
|
|
|
*/ |
217
|
|
|
public function addAttribute($attributeName, $attributeType) |
218
|
|
|
{ |
219
|
|
|
if (empty($attributeName) || empty($attributeType)) { |
220
|
|
|
throw new \InvalidArgumentException(sprintf('Attribute name "%s" and/or attribute type "%s" is invalid for Struct "%s"', $attributeName, $attributeType, $this->getName()), __LINE__); |
221
|
|
|
} |
222
|
|
|
if ($this->attributes->getStructAttributeByName($attributeName) === null) { |
223
|
|
|
$structAttribute = new StructAttribute($this->getGenerator(), $attributeName, $attributeType, $this); |
224
|
|
|
$this->attributes->add($structAttribute); |
225
|
|
|
} |
226
|
|
|
return $this; |
227
|
|
|
} |
228
|
|
|
/** |
229
|
|
|
* Returns the attribute by its name, otherwise null |
230
|
|
|
* @uses Struct::getAttributes() |
231
|
|
|
* @param string $attributeName the original attribute name |
232
|
|
|
* @return StructAttribute|null |
233
|
|
|
*/ |
234
|
|
|
public function getAttribute($attributeName) |
235
|
|
|
{ |
236
|
|
|
return $this->attributes->getStructAttributeByName($attributeName); |
237
|
|
|
} |
238
|
|
|
/** |
239
|
|
|
* Returns the attribute by its cleaned name, otherwise null |
240
|
|
|
* @uses Struct::getAttributes() |
241
|
|
|
* @param string $attributeCleanName the cleaned attribute name |
242
|
|
|
* @return StructAttribute|null |
243
|
|
|
*/ |
244
|
|
|
public function getAttributeByCleanName($attributeCleanName) |
245
|
|
|
{ |
246
|
|
|
return $this->attributes->getStructAttributeByCleanName($attributeCleanName); |
247
|
|
|
} |
248
|
|
|
/** |
249
|
|
|
* Returns the isRestriction value |
250
|
|
|
* @return bool |
251
|
|
|
*/ |
252
|
|
|
public function isRestriction() |
253
|
|
|
{ |
254
|
|
|
return $this->isRestriction; |
255
|
|
|
} |
256
|
|
|
/** |
257
|
|
|
* Sets the isRestriction value |
258
|
|
|
* @param bool $isRestriction |
259
|
|
|
* @return Struct |
260
|
|
|
*/ |
261
|
|
|
public function setRestriction($isRestriction = true) |
262
|
|
|
{ |
263
|
|
|
$this->isRestriction = $isRestriction; |
264
|
|
|
return $this; |
265
|
|
|
} |
266
|
|
|
/** |
267
|
|
|
* Alias to setRestriction |
268
|
|
|
* @param bool $isRestriction |
269
|
|
|
* @return Struct |
270
|
|
|
*/ |
271
|
|
|
public function setIsRestriction($isRestriction = true) |
272
|
|
|
{ |
273
|
|
|
return $this->setRestriction($isRestriction); |
274
|
|
|
} |
275
|
|
|
/** |
276
|
|
|
* Returns the isStruct value |
277
|
|
|
* @return bool |
278
|
|
|
*/ |
279
|
|
|
public function isStruct() |
280
|
|
|
{ |
281
|
|
|
return $this->isStruct; |
282
|
|
|
} |
283
|
|
|
/** |
284
|
|
|
* Sets the isStruct value |
285
|
|
|
* @param bool $isStruct |
286
|
|
|
* @return Struct |
287
|
|
|
*/ |
288
|
|
|
public function setStruct($isStruct = true) |
289
|
|
|
{ |
290
|
|
|
$this->isStruct = $isStruct; |
291
|
|
|
return $this; |
292
|
|
|
} |
293
|
|
|
/** |
294
|
|
|
* Alias to setStruct |
295
|
|
|
* @param bool $isStruct |
296
|
|
|
* @return Struct |
297
|
|
|
*/ |
298
|
|
|
public function setIsStruct($isStruct = true) |
299
|
|
|
{ |
300
|
|
|
return $this->setStruct($isStruct); |
301
|
|
|
} |
302
|
|
|
/** |
303
|
|
|
* Returns the values for an enumeration |
304
|
|
|
* @return StructValueContainer |
305
|
|
|
*/ |
306
|
|
|
public function getValues() |
307
|
|
|
{ |
308
|
|
|
return $this->values; |
309
|
|
|
} |
310
|
|
|
/** |
311
|
|
|
* Sets the values for an enumeration |
312
|
|
|
* @param StructValueContainer $structValueContainer |
313
|
|
|
* @return Struct |
314
|
|
|
*/ |
315
|
|
|
protected function setValues(StructValueContainer $structValueContainer) |
316
|
|
|
{ |
317
|
|
|
$this->values = $structValueContainer; |
318
|
|
|
return $this; |
319
|
|
|
} |
320
|
|
|
/** |
321
|
|
|
* Adds value to values array |
322
|
|
|
* @uses Struct::getValue() |
323
|
|
|
* @uses Struct::getValues() |
324
|
|
|
* @param mixed $value the original value |
325
|
|
|
* @return Struct |
326
|
|
|
*/ |
327
|
|
|
public function addValue($value) |
328
|
|
|
{ |
329
|
|
|
if ($this->getValue($value) === null) { |
330
|
|
|
$this->values->add(new StructValue($this->getGenerator(), $value, $this->getValues()->count(), $this)); |
331
|
|
|
$this->setRestriction(true); |
332
|
|
|
$this->setStruct(true); |
333
|
|
|
} |
334
|
|
|
return $this; |
335
|
|
|
} |
336
|
|
|
/** |
337
|
|
|
* Gets the value object for the given value |
338
|
|
|
* @uses Struct::getValues() |
339
|
|
|
* @uses AbstractModel::getName() |
340
|
|
|
* @param string $value Value name |
341
|
|
|
* @return StructValue|null |
342
|
|
|
*/ |
343
|
|
|
public function getValue($value) |
344
|
|
|
{ |
345
|
|
|
return $this->values->getStructValueByName($value); |
346
|
|
|
} |
347
|
|
|
/** |
348
|
|
|
* Allows to define from which class the curent model extends |
349
|
|
|
* @param bool $short |
350
|
|
|
* @return string |
351
|
|
|
*/ |
352
|
|
|
public function getExtends($short = false) |
353
|
|
|
{ |
354
|
|
|
$extends = ''; |
355
|
|
|
if ($this->isArray()) { |
356
|
|
|
$extends = $this->getGenerator()->getOptionStructArrayClass(); |
357
|
|
|
} elseif (!$this->isRestriction()) { |
358
|
|
|
$extends = $this->getGenerator()->getOptionStructClass(); |
359
|
|
|
} |
360
|
|
|
return $short ? Utils::removeNamespace($extends) : $extends; |
361
|
|
|
} |
362
|
|
|
/** |
363
|
|
|
* @return Struct|null |
364
|
|
|
*/ |
365
|
|
|
public function getInheritanceStruct() |
366
|
|
|
{ |
367
|
|
|
return $this->getGenerator()->getStruct(str_replace('[]', '', $this->getInheritance())); |
368
|
|
|
} |
369
|
|
|
/** |
370
|
|
|
* @return string |
371
|
|
|
*/ |
372
|
|
|
public function getTopInheritance() |
373
|
|
|
{ |
374
|
|
|
$inheritance = $this->getInheritance(); |
375
|
|
|
if (!empty($inheritance)) { |
376
|
|
|
$struct = $this->getInheritanceStruct(); |
377
|
|
|
while ($struct instanceof Struct) { |
378
|
|
|
$structInheritance = $struct->getInheritance(); |
379
|
|
|
if (!empty($structInheritance)) { |
380
|
|
|
$inheritance = $structInheritance; |
381
|
|
|
} |
382
|
|
|
$struct = $struct->getInheritanceStruct(); |
383
|
|
|
} |
384
|
|
|
} |
385
|
|
|
return $inheritance; |
386
|
|
|
} |
387
|
|
|
/** |
388
|
|
|
* @see \WsdlToPhp\PackageGenerator\Model\AbstractModel::getMeta() |
389
|
|
|
* @return string[] |
390
|
|
|
*/ |
391
|
|
|
public function getMeta() |
392
|
|
|
{ |
393
|
|
|
$inheritanceStruct = $this->getInheritanceStruct(); |
394
|
|
|
return array_merge_recursive(parent::getMeta(), ($inheritanceStruct && !$inheritanceStruct->isStruct()) ? $inheritanceStruct->getMeta() : array()); |
395
|
|
|
} |
396
|
|
|
/** |
397
|
|
|
* @param $filename |
398
|
|
|
* @return StructReservedMethod|StructArrayReservedMethod |
399
|
|
|
*/ |
400
|
|
|
public function getReservedMethodsInstance($filename = null) |
401
|
|
|
{ |
402
|
|
|
$instance = StructReservedMethod::instance($filename); |
403
|
|
|
if ($this->isArray()) { |
404
|
|
|
$instance = StructArrayReservedMethod::instance($filename); |
405
|
|
|
} |
406
|
|
|
return $instance; |
407
|
|
|
} |
408
|
|
|
/** |
409
|
|
|
* @return string[] |
410
|
|
|
*/ |
411
|
|
|
public function getTypes() |
412
|
|
|
{ |
413
|
|
|
return $this->types; |
414
|
|
|
} |
415
|
|
|
/** |
416
|
|
|
* @return boolean |
417
|
|
|
*/ |
418
|
|
|
public function isUnion() |
419
|
|
|
{ |
420
|
|
|
return count($this->types) > 0; |
421
|
|
|
} |
422
|
|
|
/** |
423
|
|
|
* @param string[] $types |
424
|
|
|
* @return Struct |
425
|
|
|
*/ |
426
|
|
|
public function setTypes($types) |
427
|
|
|
{ |
428
|
|
|
$this->types = $types; |
429
|
|
|
return $this; |
430
|
|
|
} |
431
|
|
|
/** |
432
|
|
|
* {@inheritDoc} |
433
|
|
|
* @see \WsdlToPhp\PackageGenerator\Model\AbstractModel::toJsonSerialize() |
434
|
|
|
*/ |
435
|
|
|
protected function toJsonSerialize() |
436
|
|
|
{ |
437
|
|
|
return array( |
438
|
|
|
'attributes' => $this->attributes, |
439
|
|
|
'isRestriction' => $this->isRestriction, |
440
|
|
|
'isStruct' => $this->isStruct, |
441
|
|
|
'types' => $this->types, |
442
|
|
|
'values' => $this->values, |
443
|
|
|
); |
444
|
|
|
} |
445
|
|
|
/** |
446
|
|
|
* @param array $attributes |
447
|
|
|
*/ |
448
|
|
|
public function setAttributesFromSerializedJson(array $attributes) |
449
|
|
|
{ |
450
|
|
|
foreach ($attributes as $attribute) { |
451
|
|
|
$this->attributes->add(self::instanceFromSerializedJson($this->generator, $attribute)->setOwner($this)); |
452
|
|
|
} |
453
|
|
|
} |
454
|
|
|
/** |
455
|
|
|
* @param array $values |
456
|
|
|
*/ |
457
|
|
|
public function setValuesFromSerializedJson(array $values) |
458
|
|
|
{ |
459
|
|
|
foreach ($values as $value) { |
460
|
|
|
$this->values->add(self::instanceFromSerializedJson($this->generator, $value)->setOwner($this)); |
461
|
|
|
} |
462
|
|
|
} |
463
|
|
|
} |
464
|
|
|
|