Completed
Push — feature/issue-84 ( 44128e...1e0a90 )
by Mikaël
34:57
created

Struct::isUnion()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
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::setIsStruct()
58 548
     * @param Generator $generator
59
     * @param string $name the original name
60 548
     * @param bool $isStruct defines if it's a real sruct or not
61 548
     * @param bool $isRestriction defines if it's an enumeration or not
62 548
     */
63
    public function __construct(Generator $generator, $name, $isStruct = true, $isRestriction = false)
64
    {
65
        parent::__construct($generator, $name);
66
        $this->setIsStruct($isStruct)->setIsRestriction($isRestriction)->setAttributes(new StructAttributeContainer($generator))->setValues(new StructValueContainer($generator));
67
    }
68
    /**
69 260
     * Returns the contextual part of the class name for the package
70
     * @see AbstractModel::getContextualPart()
71 260
     * @uses Struct::getIsRestriction()
72 260
     * @return string
73 92
     */
74 260
    public function getContextualPart()
75 48
    {
76 48
        $part = $this->getGenerator()->getOptionStructsFolder();
77 260
        if ($this->getIsRestriction()) {
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 144
     * Must be overridden by sub classes
87
     * @see AbstractModel::getDocSubPackages()
88 144
     * @uses Struct::getIsRestriction()
89 144
     * @return array
90 52
     */
91 144
    public function getDocSubPackages()
92 28
    {
93 28
        $package = self::DOC_SUB_PACKAGE_STRUCTS;
94
        if ($this->getIsRestriction()) {
95 144
            $package = self::DOC_SUB_PACKAGE_ENUMERATIONS;
96 144
        } elseif ($this->isArray()) {
97
            $package = self::DOC_SUB_PACKAGE_ARRAYS;
98
        }
99
        return array(
100
            $package,
101
        );
102
    }
103
    /**
104 288
     * Returns true if the current struct is a collection of values (like an array)
105
     * @uses AbstractModel::getName()
106 288
     * @uses Struct::countOwnAttributes()
107
     * @return bool
108
     */
109
    public function isArray()
110
    {
111
        return ((($this->getIsStruct() && $this->countOwnAttributes() === 1) || (!$this->getIsStruct() && $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::getIsStruct()
117 300
     * @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 300
     * @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 300
     * @return StructAttributeContainer
121 300
     */
122 108
    public function getAttributes($includeInheritanceAttributes = false, $requiredFirst = false)
123
    {
124 300
        if ($includeInheritanceAttributes === false && $requiredFirst === false) {
125
            $attributes = $this->attributes;
126
        } else {
127
            $attributes = $this->getAllAttributes($includeInheritanceAttributes, $requiredFirst);
128
        }
129
        return $attributes;
130
    }
131 108
    /**
132
     * @param bool $includeInheritanceAttributes
133 108
     * @param bool $requiredFirst
134 108
     * @return StructAttributeContainer
135 4
     */
136 4
    protected function getAllAttributes($includeInheritanceAttributes, $requiredFirst)
137 108
    {
138 108
        $allAttributes = new StructAttributeContainer($this->getGenerator());
139 108
        if ($includeInheritanceAttributes === true) {
140 108
            $this->addInheritanceAttributes($allAttributes);
141 108
        }
142 108
        foreach ($this->attributes as $attribute) {
143
            $allAttributes->add($attribute);
144
        }
145 108
        if ($requiredFirst === true) {
146
            $attributes = $this->putRequiredFirst($allAttributes);
147
        } else {
148
            $attributes = $allAttributes;
149
        }
150 4
        return $attributes;
151
    }
152 4
    /**
153
     * @param StructAttributeContainer $attributes
154
     */
155
    protected function addInheritanceAttributes(StructAttributeContainer $attributes)
156
    {
157
        if ($this->getInheritance() != '' && ($model = $this->getInheritanceStruct()) instanceof Struct) {
158
            while ($model->getIsStruct()) {
159
                foreach ($model->getAttributes() as $attribute) {
160 4
                    $attributes->add($attribute);
161
                }
162
                $model = $this->getGenerator()->getStruct($model->getInheritance());
163
            }
164
        }
165 108
    }
166
    /**
167 108
     * @param StructAttributeContainer $allAttributes
168 108
     * @return StructAttributeContainer
169 108
     */
170 108
    protected function putRequiredFirst(StructAttributeContainer $allAttributes)
171 108
    {
172 28
        $attributes = new StructAttributeContainer($this->getGenerator());
173 28
        $requiredAttributes = new StructAttributeContainer($this->getGenerator());
174 100
        $notRequiredAttributes = new StructAttributeContainer($this->getGenerator());
175
        foreach ($allAttributes as $attribute) {
176 108
            if ($attribute->isRequired()) {
177 108
                $requiredAttributes->add($attribute);
178 28
            } else {
179 108
                $notRequiredAttributes->add($attribute);
180 108
            }
181 100
        }
182 108
        foreach ($requiredAttributes as $attribute) {
183 108
            $attributes->add($attribute);
184 108
        }
185
        foreach ($notRequiredAttributes as $attribute) {
186
            $attributes->add($attribute);
187
        }
188
        unset($requiredAttributes, $notRequiredAttributes);
189
        return $attributes;
190
    }
191 288
    /**
192
     * Returns the number of own attributes
193 288
     * @uses Struct::getAttributes()
194
     * @return int
195
     */
196
    public function countOwnAttributes()
197
    {
198
        return $this->getAttributes(false, false)->count();
199
    }
200 548
    /**
201
     * Sets the attributes of the struct
202 548
     * @param StructAttributeContainer $structAttributeContainer
203 548
     * @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 492
     * @throws \InvalidArgumentException
213
     * @param string $attributeName the attribute name
214 492
     * @param string $attributeType the attribute type
215 8
     * @return Struct
216
     */
217 484
    public function addAttribute($attributeName, $attributeType)
218 480
    {
219 480
        if (empty($attributeName) || empty($attributeType)) {
220 480
            throw new \InvalidArgumentException(sprintf('Attribute name "%s" and/or attribute type "%s" is invalid for Struct "%s"', $attributeName, $attributeType, $this->getName()), __LINE__);
221 484
        }
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 348
     * Returns the attribute by its name, otherwise null
230
     * @uses Struct::getAttributes()
231 348
     * @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 56
     * Returns the attribute by its cleaned name, otherwise null
240
     * @uses Struct::getAttributes()
241 56
     * @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 292
    }
248
    /**
249 292
     * Returns the isRestriction value
250
     * @return bool
251
     */
252
    public function getIsRestriction()
253
    {
254
        return $this->isRestriction;
255
    }
256 548
    /**
257
     * Sets the isRestriction value
258 548
     * @param bool $isRestriction
259 548
     * @return Struct
260
     */
261
    public function setIsRestriction($isRestriction = true)
262
    {
263
        $this->isRestriction = $isRestriction;
264
        return $this;
265 316
    }
266
    /**
267 316
     * Returns the isStruct value
268
     * @return bool
269
     */
270
    public function getIsStruct()
271
    {
272
        return $this->isStruct;
273
    }
274 548
    /**
275
     * Sets the isStruct value
276 548
     * @param bool $isStruct
277 548
     * @return Struct
278
     */
279
    public function setIsStruct($isStruct = true)
280
    {
281
        $this->isStruct = $isStruct;
282
        return $this;
283 308
    }
284
    /**
285 308
     * Returns the values for an enumeration
286
     * @return StructValueContainer
287
     */
288
    public function getValues()
289
    {
290
        return $this->values;
291
    }
292 548
    /**
293
     * Sets the values for an enumeration
294 548
     * @param StructValueContainer $structValueContainer
295 548
     * @return Struct
296
     */
297
    private function setValues(StructValueContainer $structValueContainer)
298
    {
299
        $this->values = $structValueContainer;
300
        return $this;
301
    }
302
    /**
303
     * Adds value to values array
304 312
     * @uses Struct::getValue()
305
     * @uses Struct::getValues()
306 312
     * @param mixed $value the original value
307 308
     * @return Struct
308 308
     */
309 308
    public function addValue($value)
310 308
    {
311 312
        if ($this->getValue($value) === null) {
312
            $this->values->add(new StructValue($this->getGenerator(), $value, $this->getValues()->count(), $this));
313
            $this->setIsRestriction(true);
314
            $this->setIsStruct(true);
315
        }
316
        return $this;
317
    }
318
    /**
319
     * Gets the value object for the given value
320 312
     * @uses Struct::getValues()
321
     * @uses AbstractModel::getName()
322 312
     * @param string $value Value name
323
     * @return StructValue|null
324
     */
325
    public function getValue($value)
326
    {
327
        return $this->values->getStructValueByName($value);
328
    }
329 136
    /**
330
     * Allows to define from which class the curent model extends
331 136
     * @param bool $short
332 136
     * @return string
333 28
     */
334 136
    public function getExtends($short = false)
335 88
    {
336 88
        $extends = '';
337 136
        if ($this->isArray()) {
338
            $extends = $this->getGenerator()->getOptionStructArrayClass();
339
        } elseif (!$this->getIsRestriction()) {
340
            $extends = $this->getGenerator()->getOptionStructClass();
341
        }
342 172
        return $short ? Utils::removeNamespace($extends) : $extends;
343
    }
344 172
    /**
345
     * @return Struct|null
346
     */
347
    public function getInheritanceStruct()
348
    {
349 100
        return $this->getGenerator()->getStruct(str_replace('[]', '', $this->getInheritance()));
350
    }
351 100
    /**
352 100
     * @return string
353 48
     */
354 48
    public function getTopInheritance()
355 36
    {
356 36
        $inheritance = $this->getInheritance();
357 16
        if (!empty($inheritance)) {
358 16
            $struct = $this->getInheritanceStruct();
359 36
            while ($struct instanceof Struct) {
360 36
                $structInheritance = $struct->getInheritance();
361 48
                if (!empty($structInheritance)) {
362 100
                    $inheritance = $structInheritance;
363
                }
364
                $struct = $struct->getInheritanceStruct();
365
            }
366
        }
367
        return $inheritance;
368 164
    }
369
    /**
370 164
     * @see \WsdlToPhp\PackageGenerator\Model\AbstractModel::getMeta()
371 164
     * @return string[]
372
     */
373
    public function getMeta()
374
    {
375
        $inheritanceStruct = $this->getInheritanceStruct();
376
        return array_merge_recursive(parent::getMeta(), ($inheritanceStruct && !$inheritanceStruct->getIsStruct()) ? $inheritanceStruct->getMeta() : array());
377 120
    }
378
    /**
379 120
     * @param $filename
380 120
     * @return StructReservedMethod|StructArrayReservedMethod
381 32
     */
382 32
    public function getReservedMethodsInstance($filename = null)
383 120
    {
384
        $instance = StructReservedMethod::instance($filename);
385
        if ($this->isArray()) {
386
            $instance = StructArrayReservedMethod::instance($filename);
387
        }
388
        return $instance;
389
    }
390
    /**
391
     * @return string[]
392
     */
393
    public function getTypes()
394
    {
395
        return $this->types;
396
    }
397
    /**
398
     * @return boolean
399
     */
400
    public function isUnion()
401
    {
402
        return count($this->types) > 0;
403
    }
404
    /**
405
     * @param string[] $types
406
     * @return Struct
407
     */
408
    public function setTypes($types)
409
    {
410
        $this->types = $types;
411
        return $this;
412
    }
413
}
414