Completed
Push — 1.x ( 9a8594...e12d75 )
by Mikaël
68:19 queued 33:27
created

Struct::getMeta()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
cc 3
eloc 3
nc 1
nop 0
crap 3
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 536
    public function __construct(Generator $generator, $name, $isStruct = true, $isRestriction = false)
59
    {
60 536
        parent::__construct($generator, $name);
61 536
        $this->setIsStruct($isStruct)->setIsRestriction($isRestriction)->setAttributes(new StructAttributeContainer($generator))->setValues(new StructValueContainer($generator));
62 536
    }
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 256
    public function getContextualPart()
70
    {
71 256
        $part = $this->getGenerator()->getOptionStructsFolder();
72 256
        if ($this->getIsRestriction()) {
73 88
            $part = $this->getGenerator()->getOptionEnumsFolder();
74 247
        } elseif ($this->isArray()) {
75 48
            $part = $this->getGenerator()->getOptionArraysFolder();
76 36
        }
77 256
        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 140
    public function getDocSubPackages()
87
    {
88 140
        $package = self::DOC_SUB_PACKAGE_STRUCTS;
89 140
        if ($this->getIsRestriction()) {
90 52
            $package = self::DOC_SUB_PACKAGE_ENUMERATIONS;
91 132
        } elseif ($this->isArray()) {
92 28
            $package = self::DOC_SUB_PACKAGE_ARRAYS;
93 21
        }
94
        return array(
95 140
            $package,
96 105
        );
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 284
    public function isArray()
105
    {
106 284
        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 296
    public function getAttributes($includeInheritanceAttributes = false, $requiredFirst = false)
118
    {
119 296
        if ($includeInheritanceAttributes === false && $requiredFirst === false) {
120 296
            $attributes = $this->attributes;
121 222
        } else {
122 104
            $attributes = $this->getAllAttributes($includeInheritanceAttributes, $requiredFirst);
123
        }
124 296
        return $attributes;
125
    }
126
    /**
127
     * @param bool $includeInheritanceAttributes
128
     * @param bool $requiredFirst
129
     * @return StructAttributeContainer
130
     */
131 104
    protected function getAllAttributes($includeInheritanceAttributes, $requiredFirst)
132
    {
133 104
        $allAttributes = new StructAttributeContainer($this->getGenerator());
134 104
        if ($includeInheritanceAttributes === true) {
135 4
            $this->addInheritanceAttributes($allAttributes);
136 3
        }
137 104
        foreach ($this->attributes as $attribute) {
138 104
            $allAttributes->add($attribute);
139 78
        }
140 104
        if ($requiredFirst === true) {
141 104
            $attributes = $this->putRequiredFirst($allAttributes);
142 78
        } else {
143
            $attributes = $allAttributes;
144
        }
145 104
        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 104
    protected function putRequiredFirst(StructAttributeContainer $allAttributes)
166
    {
167 104
        $attributes = new StructAttributeContainer($this->getGenerator());
168 104
        $requiredAttributes = new StructAttributeContainer($this->getGenerator());
169 104
        $notRequiredAttributes = new StructAttributeContainer($this->getGenerator());
170 104
        foreach ($allAttributes as $attribute) {
171 104
            if ($attribute->isRequired()) {
172 28
                $requiredAttributes->add($attribute);
173 21
            } else {
174 98
                $notRequiredAttributes->add($attribute);
175
            }
176 78
        }
177 104
        foreach ($requiredAttributes as $attribute) {
178 28
            $attributes->add($attribute);
179 78
        }
180 104
        foreach ($notRequiredAttributes as $attribute) {
181 96
            $attributes->add($attribute);
182 78
        }
183 104
        unset($requiredAttributes, $notRequiredAttributes);
184 104
        return $attributes;
185
    }
186
    /**
187
     * Returns the number of own attributes
188
     * @uses Struct::getAttributes()
189
     * @return int
190
     */
191 284
    public function countOwnAttributes()
192
    {
193 284
        return $this->getAttributes(false, false)->count();
194
    }
195
    /**
196
     * Sets the attributes of the struct
197
     * @param StructAttributeContainer $structAttributeContainer
198
     * @return Struct
199
     */
200 536
    public function setAttributes(StructAttributeContainer $structAttributeContainer)
201
    {
202 536
        $this->attributes = $structAttributeContainer;
203 536
        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 480
    public function addAttribute($attributeName, $attributeType)
213
    {
214 480
        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 472
        if ($this->attributes->getStructAttributeByName($attributeName) === null) {
218 468
            $structAttribute = new StructAttribute($this->getGenerator(), $attributeName, $attributeType, $this);
219 468
            $this->attributes->add($structAttribute);
220 351
        }
221 472
        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 336
    public function getAttribute($attributeName)
230
    {
231 336
        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 288
     * @return StructAttribute|null
238
     */
239 288
    public function getAttributeByCleanName($attributeCleanName)
240
    {
241
        return $this->attributes->getStructAttributeByCleanName($attributeCleanName);
242
    }
243
    /**
244
     * Returns the isRestriction value
245
     * @return bool
246 536
     */
247
    public function getIsRestriction()
248 536
    {
249 536
        return $this->isRestriction;
250
    }
251
    /**
252
     * Sets the isRestriction value
253
     * @param bool $isRestriction
254
     * @return Struct
255 312
     */
256
    public function setIsRestriction($isRestriction = true)
257 312
    {
258
        $this->isRestriction = $isRestriction;
259
        return $this;
260
    }
261
    /**
262
     * Returns the isStruct value
263
     * @return bool
264 536
     */
265
    public function getIsStruct()
266 536
    {
267 536
        return $this->isStruct;
268
    }
269
    /**
270
     * Sets the isStruct value
271
     * @param bool $isStruct
272
     * @return Struct
273 300
     */
274
    public function setIsStruct($isStruct = true)
275 300
    {
276
        $this->isStruct = $isStruct;
277
        return $this;
278
    }
279
    /**
280
     * Returns the values for an enumeration
281
     * @return StructValueContainer
282 536
     */
283
    public function getValues()
284 536
    {
285 536
        return $this->values;
286
    }
287
    /**
288
     * Sets the values for an enumeration
289
     * @param StructValueContainer $structValueContainer
290
     * @return Struct
291
     */
292
    private function setValues(StructValueContainer $structValueContainer)
293
    {
294 304
        $this->values = $structValueContainer;
295
        return $this;
296 304
    }
297 300
    /**
298 300
     * Adds value to values array
299 300
     * @uses Struct::getValue()
300 225
     * @uses Struct::getValues()
301 304
     * @param mixed $value the original value
302
     * @return Struct
303
     */
304
    public function addValue($value)
305
    {
306
        if ($this->getValue($value) === null) {
307
            $this->values->add(new StructValue($this->getGenerator(), $value, $this->getValues()->count(), $this));
308
            $this->setIsRestriction(true);
309
            $this->setIsStruct(true);
310 304
        }
311
        return $this;
312 304
    }
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 132
     */
320
    public function getValue($value)
321 132
    {
322 132
        return $this->values->getStructValueByName($value);
323 28
    }
324 127
    /**
325 84
     * Allows to define from which class the curent model extends
326 63
     * @param bool $short
327 132
     * @return string
328
     */
329
    public function getExtends($short = false)
330
    {
331
        $extends = '';
332 168
        if ($this->isArray()) {
333
            $extends = $this->getGenerator()->getOptionStructArrayClass();
334 168
        } elseif (!$this->getIsRestriction()) {
335
            $extends = $this->getGenerator()->getOptionStructClass();
336
        }
337
        return $short ? Utils::removeNamespace($extends) : $extends;
338
    }
339 96
    /**
340
     * @return Struct|null
341 96
     */
342 96
    public function getInheritanceStruct()
343 44
    {
344 44
        return $this->getGenerator()->getStruct(str_replace('[]', '', $this->getInheritance()));
345 36
    }
346 36
    /**
347 16
     * @return string
348 12
     */
349 36
    public function getTopInheritance()
350 27
    {
351 33
        $inheritance = $this->getInheritance();
352 96
        if (!empty($inheritance)) {
353
            $struct = $this->getInheritanceStruct();
354
            while ($struct instanceof Struct) {
355
                $structInheritance = $struct->getInheritance();
356
                if (!empty($structInheritance)) {
357
                    $inheritance = $structInheritance;
358 160
                }
359
                $struct = $struct->getInheritanceStruct();
360 160
            }
361 160
        }
362
        return $inheritance;
363
    }
364
    /**
365
     * @see \WsdlToPhp\PackageGenerator\Model\AbstractModel::getMeta()
366
     * @return string[]
367 116
     */
368
    public function getMeta()
369 116
    {
370 116
        $inheritanceStruct = $this->getInheritanceStruct();
371 32
        return array_merge_recursive(parent::getMeta(), ($inheritanceStruct && !$inheritanceStruct->getIsStruct()) ? $inheritanceStruct->getMeta() : array());
372 24
    }
373 116
    /**
374
     * @param $filename
375
     * @return StructReservedMethod|StructArrayReservedMethod
376
     */
377
    public function getReservedMethodsInstance($filename = null)
378
    {
379
        $instance = StructReservedMethod::instance($filename);
380
        if ($this->isArray()) {
381
            $instance = StructArrayReservedMethod::instance($filename);
382
        }
383
        return $instance;
384
    }
385
}
386