Completed
Push — develop ( ad1531...c4ba27 )
by Mikaël
35:37
created

Struct::getIsStruct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
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
10
/**
11
 * Class Struct stands for an available struct described in the WSDL
12
 */
13
class Struct extends AbstractModel
14
{
15
    /**
16
     * @var string
17
     */
18
    const DOC_SUB_PACKAGE_STRUCTS = 'Structs';
19
    /**
20
     * @var string
21
     */
22
    const DOC_SUB_PACKAGE_ENUMERATIONS = 'Enumerations';
23
    /**
24
     * @var string
25
     */
26
    const DOC_SUB_PACKAGE_ARRAYS = 'Arrays';
27
    /**
28
     * Attributes of the struct
29
     * @var StructAttributeContainer
30
     */
31
    private $attributes;
32
    /**
33
     * Is the struct a restriction with defined values  ?
34
     * @var bool
35
     */
36
    private $isRestriction = false;
37
    /**
38
     * If the struct is a restriction with values, then store values
39
     * @var StructValueContainer
40
     */
41
    private $values;
42
    /**
43
     * Define if the urrent struct is a concrete struct or just a virtual struct to store meta informations
44
     * @var bool
45
     */
46
    private $isStruct = false;
47
    /**
48
     * Main constructor
49
     * @see AbstractModel::__construct()
50
     * @uses Struct::setIsStruct()
51
     * @param Generator $generator
52
     * @param string $name the original name
53
     * @param bool $isStruct defines if it's a real sruct or not
54
     * @param bool $isRestriction defines if it's an enumeration or not
55
     */
56 508
    public function __construct(Generator $generator, $name, $isStruct = true, $isRestriction = false)
57
    {
58 508
        parent::__construct($generator, $name);
59 381
        $this
60 508
            ->setIsStruct($isStruct)
61 508
            ->setIsRestriction($isRestriction)
62 508
            ->setAttributes(new StructAttributeContainer($generator))
63 508
            ->setValues(new StructValueContainer($generator));
64 508
    }
65
    /**
66
     * Returns the contextual part of the class name for the package
67
     * @see AbstractModel::getContextualPart()
68
     * @uses Struct::getIsRestriction()
69
     * @return string
70
     */
71 240
    public function getContextualPart()
72
    {
73 240
        $part = $this->getGenerator()->getOptionStructsFolder();
74 240
        if ($this->getIsRestriction()) {
75 88
            $part = $this->getGenerator()->getOptionEnumsFolder();
76 231
        } elseif ($this->isArray()) {
77 48
            $part = $this->getGenerator()->getOptionArraysFolder();
78 36
        }
79 240
        return $part;
80
    }
81
    /**
82
     * Returns the sub package name which the model belongs to
83
     * Must be overridden by sub classes
84
     * @see AbstractModel::getDocSubPackages()
85
     * @uses Struct::getIsRestriction()
86
     * @return array
87
     */
88 136
    public function getDocSubPackages()
89
    {
90 136
        $package = self::DOC_SUB_PACKAGE_STRUCTS;
91 136
        if ($this->getIsRestriction()) {
92 52
            $package = self::DOC_SUB_PACKAGE_ENUMERATIONS;
93 128
        } elseif ($this->isArray()) {
94 28
            $package = self::DOC_SUB_PACKAGE_ARRAYS;
95 21
        }
96
        return array(
97 136
            $package,
98 102
        );
99
    }
100
    /**
101
     * Returns true if the current struct is a collection of values (like an array)
102
     * @uses AbstractModel::getName()
103
     * @uses Struct::countOwnAttributes()
104
     * @return bool
105
     */
106 256
    public function isArray()
107
    {
108 256
        return ((($this->getIsStruct() && $this->countOwnAttributes() === 1) || (!$this->getIsStruct() && $this->countOwnAttributes() <= 1)) && stripos($this->getName(), 'array') !== false);
109
    }
110
    /**
111
     * Returns the attributes of the struct and potentially from the parent class
112
     * @uses AbstractModel::getInheritance()
113
     * @uses Struct::getIsStruct()
114
     * @uses Struct::getAttributes()
115
     * @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.
116
     * @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
117
     * @return StructAttributeContainer
118
     */
119 268
    public function getAttributes($includeInheritanceAttributes = false, $requiredFirst = false)
120
    {
121 268
        if ($includeInheritanceAttributes === false && $requiredFirst === false) {
122 268
            $attributes = $this->attributes;
123 201
        } else {
124 100
            $attributes = $this->getAllAttributes($includeInheritanceAttributes, $requiredFirst);
125
        }
126 268
        return $attributes;
127
    }
128
    /**
129
     * @param bool $includeInheritanceAttributes
130
     * @param bool $requiredFirst
131
     * @return StructAttributeContainer
132
     */
133 100
    protected function getAllAttributes($includeInheritanceAttributes, $requiredFirst)
134
    {
135 100
        $allAttributes = new StructAttributeContainer($this->getGenerator());
136 100
        if ($includeInheritanceAttributes === true) {
137 4
            $this->addInheritanceAttributes($allAttributes);
138 3
        }
139 100
        foreach ($this->attributes as $attribute) {
140 100
            $allAttributes->add($attribute);
141 75
        }
142 100
        if ($requiredFirst === true) {
143 100
            $attributes = $this->putRequiredFirst($allAttributes);
144 75
        } else {
145
            $attributes = $allAttributes;
146
        }
147 100
        return $attributes;
148
    }
149
    /**
150
     * @param StructAttributeContainer $attributes
151
     */
152 4
    protected function addInheritanceAttributes(StructAttributeContainer $attributes)
153
    {
154 4
        if ($this->getInheritance() != '' && ($model = $this->getInheritanceStruct()) instanceof Struct) {
155
            while ($model->getIsStruct()) {
156
                foreach ($model->getAttributes() as $attribute) {
157
                    $attributes->add($attribute);
158
                }
159
                $model = $this->getGenerator()->getStruct($model->getInheritance());
160
            }
161
        }
162 4
    }
163
    /**
164
     * @param StructAttributeContainer $allAttributes
165
     * @return StructAttributeContainer
166
     */
167 100
    protected function putRequiredFirst(StructAttributeContainer $allAttributes)
168
    {
169 100
        $attributes = new StructAttributeContainer($this->getGenerator());
170 100
        $requiredAttributes = new StructAttributeContainer($this->getGenerator());
171 100
        $notRequiredAttributes = new StructAttributeContainer($this->getGenerator());
172 100
        foreach ($allAttributes as $attribute) {
173 100
            if ($attribute->isRequired()) {
174 28
                $requiredAttributes->add($attribute);
175 21
            } else {
176 94
                $notRequiredAttributes->add($attribute);
177
            }
178 75
        }
179 100
        foreach ($requiredAttributes as $attribute) {
180 28
            $attributes->add($attribute);
181 75
        }
182 100
        foreach ($notRequiredAttributes as $attribute) {
183 92
            $attributes->add($attribute);
184 75
        }
185 100
        unset($requiredAttributes, $notRequiredAttributes);
186 100
        return $attributes;
187
    }
188
    /**
189
     * Returns the number of own attributes
190
     * @uses Struct::getAttributes()
191
     * @return int
192
     */
193 256
    public function countOwnAttributes()
194
    {
195 256
        return $this->getAttributes(false, false)->count();
196
    }
197
    /**
198
     * Sets the attributes of the struct
199
     * @param StructAttributeContainer $structAttributeContainer
200
     * @return Struct
201
     */
202 508
    public function setAttributes(StructAttributeContainer $structAttributeContainer)
203
    {
204 508
        $this->attributes = $structAttributeContainer;
205 508
        return $this;
206
    }
207
    /**
208
     * Adds attribute based on its original name
209
     * @throws \InvalidArgumentException
210
     * @param string $attributeName the attribute name
211
     * @param string $attributeType the attribute type
212
     * @return Struct
213
     */
214 456
    public function addAttribute($attributeName, $attributeType)
215
    {
216 456
        if (empty($attributeName) || empty($attributeType)) {
217 8
            throw new \InvalidArgumentException(sprintf('Attribute name "%s" and/or attribute type "%s" is invalid for Struct "%s"', $attributeName, $attributeType, $this->getName()), __LINE__);
218
        }
219 448
        if ($this->attributes->getStructAttributeByName($attributeName) === null) {
220 444
            $structAttribute = new StructAttribute($this->getGenerator(), $attributeName, $attributeType, $this);
221 444
            $this->attributes->add($structAttribute);
222 333
        }
223 448
        return $this;
224
    }
225
    /**
226
     * Returns the attribute by its name, otherwise null
227
     * @uses Struct::getAttributes()
228
     * @param string $attributeName the original attribute name
229
     * @return StructAttribute|null
230
     */
231 316
    public function getAttribute($attributeName)
232
    {
233 316
        return $this->attributes->getStructAttributeByName($attributeName);
234
    }
235
    /**
236
     * Returns the isRestriction value
237
     * @return bool
238
     */
239 272
    public function getIsRestriction()
240
    {
241 272
        return $this->isRestriction;
242
    }
243
    /**
244
     * Sets the isRestriction value
245
     * @param bool $isRestriction
246
     * @return Struct
247
     */
248 508
    public function setIsRestriction($isRestriction = true)
249
    {
250 508
        $this->isRestriction = $isRestriction;
251 508
        return $this;
252
    }
253
    /**
254
     * Returns the isStruct value
255
     * @return bool
256
     */
257 204
    public function getIsStruct()
258
    {
259 204
        return $this->isStruct;
260
    }
261
    /**
262
     * Sets the isStruct value
263
     * @param bool $isStruct
264
     * @return Struct
265
     */
266 508
    public function setIsStruct($isStruct = true)
267
    {
268 508
        $this->isStruct = $isStruct;
269 508
        return $this;
270
    }
271
    /**
272
     * Returns the values for an enumeration
273
     * @return StructValueContainer
274
     */
275 292
    public function getValues()
276
    {
277 292
        return $this->values;
278
    }
279
    /**
280
     * Sets the values for an enumeration
281
     * @param StructValueContainer $structValueContainer
282
     * @return Struct
283
     */
284 508
    private function setValues(StructValueContainer $structValueContainer)
285
    {
286 508
        $this->values = $structValueContainer;
287 508
        return $this;
288
    }
289
    /**
290
     * Adds value to values array
291
     * @uses Struct::getValue()
292
     * @uses Struct::getValues()
293
     * @param mixed $value the original value
294
     * @return Struct
295
     */
296 296
    public function addValue($value)
297
    {
298 296
        if ($this->getValue($value) === null) {
299 292
            $this->values->add(new StructValue($this->getGenerator(), $value, $this->getValues()->count(), $this));
300 292
            $this->setIsRestriction(true);
301 292
            $this->setIsStruct(true);
302 219
        }
303 296
        return $this;
304
    }
305
    /**
306
     * Gets the value object for the given value
307
     * @uses Struct::getValues()
308
     * @uses AbstractModel::getName()
309
     * @param string $value Value name
310
     * @return StructValue|null
311
     */
312 296
    public function getValue($value)
313
    {
314 296
        return $this->values->getStructValueByName($value);
315
    }
316
    /**
317
     * Allows to define from which class the curent model extends
318
     * @param bool $short
319
     * @return string
320
     */
321 128
    public function getExtends($short = false)
322
    {
323 128
        $extends = '';
324 128
        if ($this->isArray()) {
325 28
            $extends = $this->getGenerator()->getOptionStructArrayClass();
326 123
        } elseif (!$this->getIsRestriction()) {
327 80
            $extends = $this->getGenerator()->getOptionStructClass();
328 60
        }
329 128
        return $short ? Utils::removeNamespace($extends) : $extends;
330
    }
331
    /**
332
     * @return Struct|null
333
     */
334 156
    public function getInheritanceStruct()
335
    {
336 156
        return $this->getGenerator()->getStruct(str_replace('[]', '', $this->getInheritance()));
337
    }
338
    /**
339
     * @return Struct|null
340
     */
341 92
    public function getTopInheritance()
342
    {
343 92
        $inheritance = $this->getInheritance();
344 92
        if (!empty($inheritance)) {
345 40
            $struct = $this->getInheritanceStruct();
346 40
            while ($struct instanceof Struct) {
347 24
                $structInheritance = $struct->getInheritance();
348 24
                if (!empty($structInheritance)) {
349 16
                    $inheritance = $structInheritance;
350 12
                }
351 24
                $struct = $struct->getInheritanceStruct();
352 18
            }
353 30
        }
354 92
        return $inheritance;
355
    }
356
    /**
357
     * @see \WsdlToPhp\PackageGenerator\Model\AbstractModel::getMeta()
358
     * @return string[]
359
     */
360 156
    public function getMeta()
361
    {
362 156
        $inheritanceStruct = $this->getInheritanceStruct();
363 156
        return array_merge_recursive(parent::getMeta(), ($inheritanceStruct && !$inheritanceStruct->getIsStruct()) ? $inheritanceStruct->getMeta() : array());
364
    }
365
}
366