Completed
Push — feature/issue-47 ( cdce8d...8413b2 )
by Mikaël
24:42
created

Struct::getMeta()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
ccs 3
cts 3
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
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 464
    public function __construct(Generator $generator, $name, $isStruct = true, $isRestriction = false)
57
    {
58 464
        parent::__construct($generator, $name);
59 348
        $this
60 464
            ->setIsStruct($isStruct)
61 464
            ->setIsRestriction($isRestriction)
62 464
            ->setAttributes(new StructAttributeContainer($generator))
63 464
            ->setValues(new StructValueContainer($generator));
64 464
    }
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 208
    public function getContextualPart()
72
    {
73 208
        $part = $this->getGenerator()->getOptionStructsFolder();
74 208
        if ($this->getIsRestriction()) {
75 76
            $part = $this->getGenerator()->getOptionEnumsFolder();
76 199
        } elseif ($this->isArray()) {
77 44
            $part = $this->getGenerator()->getOptionArraysFolder();
78 33
        }
79 208
        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 112
    public function getDocSubPackages()
89
    {
90 112
        $package = self::DOC_SUB_PACKAGE_STRUCTS;
91 112
        if ($this->getIsRestriction()) {
92 52
            $package = self::DOC_SUB_PACKAGE_ENUMERATIONS;
93 104
        } elseif ($this->isArray()) {
94 28
            $package = self::DOC_SUB_PACKAGE_ARRAYS;
95 21
        }
96
        return array(
97 112
            $package,
98 84
        );
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 224
    public function isArray()
107
    {
108 224
        return ($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 236
    public function getAttributes($includeInheritanceAttributes = false, $requiredFirst = false)
120
    {
121 236
        if ($includeInheritanceAttributes === false && $requiredFirst === false) {
122 236
            $attributes = $this->attributes;
123 177
        } else {
124 76
            $attributes = $this->getAllAttributes($includeInheritanceAttributes, $requiredFirst);
125
        }
126 236
        return $attributes;
127
    }
128
    /**
129
     * @param bool $includeInheritanceAttributes
130
     * @param bool $requiredFirst
131
     * @return StructAttributeContainer
132
     */
133 76
    protected function getAllAttributes($includeInheritanceAttributes, $requiredFirst)
134
    {
135 76
        $allAttributes = new StructAttributeContainer($this->getGenerator());
136 76
        if ($includeInheritanceAttributes === true) {
137 4
            $this->addInheritanceAttributes($allAttributes);
138 3
        }
139 76
        foreach ($this->attributes as $attribute) {
140 76
            $allAttributes->add($attribute);
141 57
        }
142 76
        if ($requiredFirst === true) {
143 76
            $attributes = $this->putRequiredFirst($allAttributes);
144 57
        } else {
145
            $attributes = $allAttributes;
146
        }
147 76
        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 76
    protected function putRequiredFirst(StructAttributeContainer $allAttributes)
168
    {
169 76
        $attributes = new StructAttributeContainer($this->getGenerator());
170 76
        $requiredAttributes = new StructAttributeContainer($this->getGenerator());
171 76
        $notRequiredAttributes = new StructAttributeContainer($this->getGenerator());
172 76
        foreach ($allAttributes as $attribute) {
173 76
            if ($attribute->isRequired()) {
174 20
                $requiredAttributes->add($attribute);
175 15
            } else {
176 73
                $notRequiredAttributes->add($attribute);
177
            }
178 57
        }
179 76
        foreach ($requiredAttributes as $attribute) {
180 20
            $attributes->add($attribute);
181 57
        }
182 76
        foreach ($notRequiredAttributes as $attribute) {
183 72
            $attributes->add($attribute);
184 57
        }
185 76
        unset($requiredAttributes, $notRequiredAttributes);
186 76
        return $attributes;
187
    }
188
    /**
189
     * Returns the number of own attributes
190
     * @uses Struct::getAttributes()
191
     * @return int
192
     */
193 224
    public function countOwnAttributes()
194
    {
195 224
        return $this->getAttributes(false, false)->count();
196
    }
197
    /**
198
     * Sets the attributes of the struct
199
     * @param StructAttributeContainer $structAttributeContainer
200
     * @return Struct
201
     */
202 464
    public function setAttributes(StructAttributeContainer $structAttributeContainer)
203
    {
204 464
        $this->attributes = $structAttributeContainer;
205 464
        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 416
    public function addAttribute($attributeName, $attributeType)
215
    {
216 416
        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 408
        if ($this->attributes->getStructAttributeByName($attributeName) === null) {
220 404
            $structAttribute = new StructAttribute($this->getGenerator(), $attributeName, $attributeType, $this);
221 404
            $this->attributes->add($structAttribute);
222 303
        }
223 408
        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 276
    public function getAttribute($attributeName)
232
    {
233 276
        return $this->attributes->getStructAttributeByName($attributeName);
234
    }
235
    /**
236
     * Returns the isRestriction value
237
     * @return bool
238
     */
239 240
    public function getIsRestriction()
240
    {
241 240
        return $this->isRestriction;
242
    }
243
    /**
244
     * Sets the isRestriction value
245
     * @param bool $isRestriction
246
     * @return Struct
247
     */
248 464
    public function setIsRestriction($isRestriction = true)
249
    {
250 464
        $this->isRestriction = $isRestriction;
251 464
        return $this;
252
    }
253
    /**
254
     * Returns the isStruct value
255
     * @return bool
256
     */
257 168
    public function getIsStruct()
258
    {
259 168
        return $this->isStruct;
260
    }
261
    /**
262
     * Sets the isStruct value
263
     * @param bool $isStruct
264
     * @return Struct
265
     */
266 464
    public function setIsStruct($isStruct = true)
267
    {
268 464
        $this->isStruct = $isStruct;
269 464
        return $this;
270
    }
271
    /**
272
     * Returns the values for an enumeration
273
     * @return StructValueContainer
274
     */
275 256
    public function getValues()
276
    {
277 256
        return $this->values;
278
    }
279
    /**
280
     * Sets the values for an enumeration
281
     * @param StructValueContainer $structValueContainer
282
     * @return Struct
283
     */
284 464
    private function setValues(StructValueContainer $structValueContainer)
285
    {
286 464
        $this->values = $structValueContainer;
287 464
        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 260
    public function addValue($value)
297
    {
298 260
        if ($this->getValue($value) === null) {
299 256
            $this->values->add(new StructValue($this->getGenerator(), $value, $this->getValues()->count(), $this));
300 256
            $this->setIsRestriction(true);
301 256
            $this->setIsStruct(true);
302 192
        }
303 260
        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 260
    public function getValue($value)
313
    {
314 260
        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 104
    public function getExtends($short = false)
322
    {
323 104
        $extends = '';
324 104
        if ($this->isArray()) {
325 28
            $extends = $this->getGenerator()->getOptionStructArrayClass();
326 99
        } elseif (!$this->getIsRestriction()) {
327 56
            $extends = $this->getGenerator()->getOptionStructClass();
328 42
        }
329 104
        return $short ? Utils::removeNamespace($extends) : $extends;
330
    }
331
    /**
332
     * @return Struct|null
333
     */
334 132
    public function getInheritanceStruct()
335
    {
336 132
        return $this->getGenerator()->getStruct($this->getInheritance());
337
    }
338
    /**
339
     * @see \WsdlToPhp\PackageGenerator\Model\AbstractModel::getMeta()
340
     * @return string[]
341
     */
342 132
    public function getMeta()
343
    {
344 132
        $inheritanceStruct = $this->getInheritanceStruct();
345 132
        return array_merge_recursive(parent::getMeta(), ($inheritanceStruct && !$inheritanceStruct->getIsStruct()) ? $inheritanceStruct->getMeta() : array());
346
    }
347
}
348