1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace WsdlToPhp\PackageGenerator\Container\Model; |
6
|
|
|
|
7
|
|
|
use WsdlToPhp\PackageGenerator\Model\Struct as Model; |
8
|
|
|
|
9
|
|
|
final class Struct extends AbstractModel |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Only for virtually-considered objects (in order to avoid duplications in objects property). |
13
|
|
|
*/ |
14
|
|
|
protected array $virtualObjects = []; |
15
|
|
|
|
16
|
490 |
|
public function getStructByName(string $name): ?Model |
17
|
|
|
{ |
18
|
490 |
|
return $this->get($name); |
19
|
|
|
} |
20
|
|
|
|
21
|
168 |
|
public function getStructByNameAndType(string $name, string $type): ?Model |
22
|
|
|
{ |
23
|
168 |
|
return $this->getByType($name, $type); |
24
|
|
|
} |
25
|
|
|
|
26
|
112 |
|
public function addVirtualStruct(string $structName, string $structType = ''): self |
27
|
|
|
{ |
28
|
112 |
|
return $this->addStruct($structName, false, $structType); |
29
|
|
|
} |
30
|
|
|
|
31
|
116 |
|
public function addStruct(string $structName, bool $isStruct = true, string $structType = ''): self |
32
|
|
|
{ |
33
|
116 |
|
if (null === (empty($structType) ? $this->get($structName) : $this->getByType($structName, $structType))) { |
34
|
112 |
|
$model = new Model($this->generator, $structName, $isStruct); |
35
|
112 |
|
$this->add($model->setInheritance($structType)); |
36
|
|
|
} |
37
|
|
|
|
38
|
116 |
|
return $this; |
39
|
|
|
} |
40
|
|
|
|
41
|
116 |
|
public function addStructWithAttribute(string $structName, string $attributeName, string $attributeType): self |
42
|
|
|
{ |
43
|
116 |
|
$this->addStruct($structName); |
44
|
116 |
|
if (($struct = $this->getStructByName($structName)) instanceof Model) { |
45
|
116 |
|
$struct->addAttribute($attributeName, $attributeType); |
46
|
|
|
} |
47
|
|
|
|
48
|
116 |
|
return $this; |
49
|
|
|
} |
50
|
|
|
|
51
|
20 |
|
public function addUnionStruct(string $structName, array $types): self |
52
|
|
|
{ |
53
|
20 |
|
$this->addVirtualStruct($structName); |
54
|
20 |
|
if (($struct = $this->getStructByName($structName)) instanceof Model) { |
55
|
20 |
|
$struct->setTypes($types); |
56
|
|
|
} |
57
|
|
|
|
58
|
20 |
|
return $this; |
59
|
|
|
} |
60
|
|
|
|
61
|
372 |
|
public function getVirtual(string $value): ?Model |
62
|
|
|
{ |
63
|
372 |
|
$key = $this->getVirtualKey($value); |
64
|
|
|
|
65
|
372 |
|
return array_key_exists($key, $this->virtualObjects) ? $this->virtualObjects[$key] : null; |
66
|
|
|
} |
67
|
|
|
|
68
|
226 |
|
public function getByType(string $value, string $type): ?Model |
69
|
|
|
{ |
70
|
226 |
|
$key = $this->getTypeKey($value, $type); |
71
|
|
|
|
72
|
226 |
|
return array_key_exists($key, $this->objects) ? $this->objects[$key] : null; |
73
|
|
|
} |
74
|
|
|
|
75
|
316 |
|
public function getObjectKeyWithType(object $object, string $type): string |
76
|
|
|
{ |
77
|
316 |
|
return $this->getTypeKey($this->getObjectKey($object), $type); |
78
|
|
|
} |
79
|
|
|
|
80
|
316 |
|
public function getObjectKeyWithVirtual(object $object): string |
81
|
|
|
{ |
82
|
316 |
|
return $this->getVirtualKey($this->getObjectKey($object)); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* The key must not conflict with possible key values. |
87
|
|
|
*/ |
88
|
346 |
|
public function getTypeKey(string $name, string $type): string |
89
|
|
|
{ |
90
|
346 |
|
return sprintf('struct_name_%s-type_%s', $name, $type); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* The key must not conflict with possible key values. |
95
|
|
|
*/ |
96
|
470 |
|
public function getVirtualKey(string $name): string |
97
|
|
|
{ |
98
|
470 |
|
return sprintf('virtual_struct_name_%s', $name); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* By overriding this method, we ensure that each time a new object is stored, it is stored with our new key if the inheritance is defined. |
103
|
|
|
* |
104
|
|
|
* @param Model $object |
105
|
|
|
* |
106
|
|
|
* @return Struct |
107
|
|
|
*/ |
108
|
324 |
|
public function add(object $object): self |
109
|
|
|
{ |
110
|
324 |
|
$inheritance = $object->getInheritance(); |
111
|
324 |
|
if (!empty($inheritance)) { |
112
|
316 |
|
$this->virtualObjects[$this->getObjectKeyWithVirtual($object)] = $this->objects[$this->getObjectKeyWithType($object, $object->getInheritance())] = $object; |
113
|
|
|
} else { |
114
|
322 |
|
parent::add($object); |
115
|
|
|
} |
116
|
|
|
|
117
|
324 |
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
322 |
|
protected function objectClass(): string |
121
|
|
|
{ |
122
|
322 |
|
return Model::class; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|