Completed
Push — feature/issue-82 ( aeff8b )
by Mikaël
02:06
created

Struct::getAttributeByCleanName()   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

Importance

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