1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WsdlToPhp\PackageGenerator\Container\Model; |
4
|
|
|
|
5
|
|
|
use WsdlToPhp\PackageGenerator\Model\Struct as Model; |
6
|
|
|
|
7
|
|
|
class Struct extends AbstractModel |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Only for virtually-considered objects (in order to avoid duplications in objects property) |
11
|
|
|
* @var array $virtualObjects |
12
|
|
|
*/ |
13
|
|
|
protected $virtualObjects = []; |
14
|
|
|
/** |
15
|
|
|
* @see \WsdlToPhp\PackageGenerator\Container\Model\Model::objectClass() |
16
|
|
|
* @return string |
17
|
|
|
*/ |
18
|
318 |
|
protected function objectClass() |
19
|
|
|
{ |
20
|
318 |
|
return 'WsdlToPhp\PackageGenerator\Model\Struct'; |
21
|
|
|
} |
22
|
|
|
/** |
23
|
|
|
* @param string $name |
24
|
|
|
* @return Model|null |
25
|
|
|
*/ |
26
|
474 |
|
public function getStructByName($name) |
27
|
|
|
{ |
28
|
474 |
|
return $this->get($name); |
29
|
|
|
} |
30
|
|
|
/** |
31
|
|
|
* @param string $name |
32
|
|
|
* @param string $type |
33
|
|
|
* @return Model|null |
34
|
|
|
*/ |
35
|
164 |
|
public function getStructByNameAndType($name, $type) |
36
|
|
|
{ |
37
|
164 |
|
return $this->getByType($name, $type); |
38
|
|
|
} |
39
|
|
|
/** |
40
|
|
|
* Adds a virtual struct |
41
|
|
|
* @param string $structName the original struct name |
42
|
|
|
* @param string $structType the original struct type |
43
|
|
|
* @return Struct |
44
|
|
|
*/ |
45
|
114 |
|
public function addVirtualStruct($structName, $structType = '') |
46
|
|
|
{ |
47
|
114 |
|
return $this->addStruct($structName, false, $structType); |
48
|
|
|
} |
49
|
|
|
/** |
50
|
|
|
* Adds type to structs |
51
|
|
|
* @param string $structName the original struct name |
52
|
|
|
* @param bool $isStruct whether the Struct has to be generated or not |
53
|
|
|
* @param string $structType the original struct type |
54
|
|
|
* @return Struct |
55
|
|
|
*/ |
56
|
118 |
|
public function addStruct($structName, $isStruct = true, $structType = '') |
57
|
|
|
{ |
58
|
118 |
|
if (null === (empty($structType) ? $this->get($structName) : $this->getByType($structName, $structType))) { |
59
|
114 |
|
$model = new Model($this->generator, $structName, $isStruct); |
60
|
114 |
|
$this->add($model->setInheritance($structType)); |
61
|
|
|
} |
62
|
118 |
|
return $this; |
63
|
|
|
} |
64
|
|
|
/** |
65
|
|
|
* Adds type to structs and its attribute |
66
|
|
|
* @param string $structName the original struct name |
67
|
|
|
* @param string $attributeName the attribute name |
68
|
|
|
* @param string $attributeType the attribute type |
69
|
|
|
* @return Struct |
70
|
|
|
*/ |
71
|
118 |
|
public function addStructWithAttribute($structName, $attributeName, $attributeType) |
72
|
|
|
{ |
73
|
118 |
|
$this->addStruct($structName); |
74
|
118 |
|
if (($struct = $this->getStructByName($structName)) instanceof Model) { |
75
|
118 |
|
$struct->addAttribute($attributeName, $attributeType); |
76
|
|
|
} |
77
|
118 |
|
return $this; |
78
|
|
|
} |
79
|
|
|
/** |
80
|
|
|
* Adds a union struct |
81
|
|
|
* @param string $structName |
82
|
|
|
* @param string[] $types |
83
|
|
|
* @return Struct |
84
|
|
|
*/ |
85
|
20 |
|
public function addUnionStruct($structName, $types) |
86
|
|
|
{ |
87
|
20 |
|
$this->addVirtualStruct($structName); |
88
|
20 |
|
if (($struct = $this->getStructByName($structName)) instanceof Model) { |
89
|
20 |
|
$struct->setTypes($types); |
90
|
|
|
} |
91
|
20 |
|
return $this; |
92
|
|
|
} |
93
|
|
|
/** |
94
|
|
|
* @see \WsdlToPhp\PackageGenerator\Model\AbstractModel::get() |
95
|
|
|
* @param string $value |
96
|
|
|
* @return Model|null |
97
|
|
|
*/ |
98
|
474 |
|
public function get($value) |
99
|
|
|
{ |
100
|
474 |
|
return parent::get($value); |
101
|
|
|
} |
102
|
|
|
/** |
103
|
|
|
* @see parent::get() |
104
|
|
|
* @throws \InvalidArgumentException |
105
|
|
|
* @param string $value |
106
|
|
|
* @return mixed |
107
|
|
|
*/ |
108
|
368 |
|
public function getVirtual($value) |
109
|
|
|
{ |
110
|
368 |
|
if (!is_scalar($value)) { |
|
|
|
|
111
|
|
|
throw new \InvalidArgumentException(sprintf('Value "%s" can\'t be used to get an object from "%s"', is_object($value) ? get_class($value) : var_export($value, true), __CLASS__), __LINE__); |
112
|
|
|
} |
113
|
368 |
|
$key = $this->getVirtualKey($value); |
114
|
368 |
|
return array_key_exists($key, $this->virtualObjects) ? $this->virtualObjects[$key] : null; |
115
|
|
|
} |
116
|
|
|
/** |
117
|
|
|
* @see parent::get() |
118
|
|
|
* @throws \InvalidArgumentException |
119
|
|
|
* @param string $value |
120
|
|
|
* @param string $type |
121
|
|
|
* @return mixed |
122
|
|
|
*/ |
123
|
224 |
|
public function getByType($value, $type) |
124
|
|
|
{ |
125
|
224 |
|
if (!is_scalar($value)) { |
|
|
|
|
126
|
|
|
throw new \InvalidArgumentException(sprintf('Value "%s" can\'t be used to get an object from "%s"', is_object($value) ? get_class($value) : var_export($value, true), __CLASS__), __LINE__); |
127
|
|
|
} |
128
|
224 |
|
if (!is_scalar($type)) { |
|
|
|
|
129
|
2 |
|
throw new \InvalidArgumentException(sprintf('Type "%s" can\'t be used to get an object from "%s"', is_object($value) ? get_class($value) : var_export($type, true), __CLASS__), __LINE__); |
130
|
|
|
} |
131
|
222 |
|
$key = $this->getTypeKey($value, $type); |
132
|
222 |
|
return array_key_exists($key, $this->objects) ? $this->objects[$key] : null; |
133
|
|
|
} |
134
|
|
|
/** |
135
|
|
|
* @param $object |
136
|
|
|
* @param $type |
137
|
|
|
* @return string |
138
|
|
|
*/ |
139
|
310 |
|
public function getObjectKeyWithType($object, $type) |
140
|
|
|
{ |
141
|
310 |
|
return $this->getTypeKey($this->getObjectKey($object), $type); |
142
|
|
|
} |
143
|
|
|
/** |
144
|
|
|
* @param $object |
145
|
|
|
* @return string |
146
|
|
|
*/ |
147
|
310 |
|
public function getObjectKeyWithVirtual($object) |
148
|
|
|
{ |
149
|
310 |
|
return $this->getVirtualKey($this->getObjectKey($object)); |
150
|
|
|
} |
151
|
|
|
/** |
152
|
|
|
* The key must not conflict with possible key values |
153
|
|
|
* @param $name |
154
|
|
|
* @param $type |
155
|
|
|
* @return string |
156
|
|
|
*/ |
157
|
338 |
|
public function getTypeKey($name, $type) |
158
|
|
|
{ |
159
|
338 |
|
return sprintf('struct_name_%s-type_%s', $name, $type); |
160
|
|
|
} |
161
|
|
|
/** |
162
|
|
|
* The key must not conflict with possible key values |
163
|
|
|
* @param $type |
164
|
|
|
* @return string |
165
|
|
|
*/ |
166
|
466 |
|
public function getVirtualKey($name) |
167
|
|
|
{ |
168
|
466 |
|
return sprintf('virtual_struct_name_%s', $name); |
169
|
|
|
} |
170
|
|
|
/** |
171
|
|
|
* 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. |
172
|
|
|
* @param Model $object |
173
|
|
|
* @return Struct |
174
|
|
|
*/ |
175
|
320 |
|
public function add($object) |
176
|
|
|
{ |
177
|
320 |
|
$inheritance = $object->getInheritance(); |
178
|
320 |
|
if (!empty($inheritance)) { |
179
|
310 |
|
$this->virtualObjects[$this->getObjectKeyWithVirtual($object)] = $this->objects[$this->getObjectKeyWithType($object, $object->getInheritance())] = $object; |
180
|
|
|
} else { |
181
|
318 |
|
parent::add($object); |
182
|
|
|
} |
183
|
320 |
|
return $this; |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|