Completed
Push — feature/issue-54 ( 74298b...f633ee )
by Mikaël
32:39
created

Struct::setIsRestriction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

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