Completed
Push — master ( 06621f...b41c10 )
by Mikaël
30:47
created

StructAttribute::isArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 0
crap 2
1
<?php
2
3
namespace WsdlToPhp\PackageGenerator\Model;
4
5
use WsdlToPhp\PackageGenerator\Generator\Utils;
6
use WsdlToPhp\PackageGenerator\Generator\Generator;
7
use WsdlToPhp\PackageGenerator\ConfigurationReader\StructReservedMethod;
8
use WsdlToPhp\PackageGenerator\ConfigurationReader\StructArrayReservedMethod;
9
10
/**
11
 * Class StructAttribute stands for an available struct attribute described in the WSDL
12
 */
13
class StructAttribute extends AbstractModel
14
{
15
    /**
16
     * Type of the struct attribute
17
     * @var string
18
     */
19
    protected $type = '';
20
    /**
21
     * Defines that this property is not a simple value but an array of values
22
     * Infos at {@link https://www.w3.org/TR/xmlschema-0/#OccurrenceConstraints}
23
     * @var bool
24
     */
25
    protected $containsElements = false;
26
    /**
27
     * Defines that this property can be removed from request or not.
28
     * The property cna be removed from the request (meaning from the Struct) as soon as the nillable=true && minOccurs=0
29
     * Infos at {@link http://www.w3schools.com/xml/el_element.asp}
30
     * @var bool
31
     */
32
    protected $removableFromRequest = false;
33
    /**
34
     * Main constructor
35
     * @see AbstractModel::__construct()
36
     * @uses StructAttribute::setType()
37
     * @uses AbstractModel::setOwner()
38
     * @param Generator $generator
39
     * @param string $name the original name
40
     * @param string $type the type
41
     * @param Struct $struct defines the struct which owns this value
42
     */
43 1145
    public function __construct(Generator $generator, $name, $type = '', Struct $struct = null)
44
    {
45 1145
        parent::__construct($generator, $name);
46 1145
        $this->setType($type)->setOwner($struct);
47 1145
    }
48
    /**
49
     * Returns the unique name in the current struct (for setters/getters and struct constructor array)
50
     * @uses AbstractModel::getCleanName()
51
     * @uses AbstractModel::getName()
52
     * @uses AbstractModel::uniqueName()
53
     * @uses StructAttribute::getOwner()
54
     * @param string $additionalContext
55
     * @param string $string
56
     * @param string $additionalContext
57
     * @return string
58
     */
59 170
    public function getUniqueString($string, $additionalContext= '')
60
    {
61 170
        return self::uniqueName($string, $this->getOwner()->getName() . $additionalContext);
62
    }
63
    /**
64
     * Returns the unique name in the current struct (for setters/getters and struct contrusctor array)
65
     * @uses AbstractModel::getCleanName()
66
     * @uses AbstractModel::getName()
67
     * @uses AbstractModel::uniqueName()
68
     * @uses StructAttribute::getOwner()
69
     * @param string $additionalContext
70
     * @return string
71
     */
72 170
    public function getUniqueName($additionalContext = '')
73
    {
74 170
        return $this->getUniqueString($this->getCleanName(), $additionalContext);
75
    }
76
    /**
77
     * Returns the getter name for this attribute
78
     * @uses StructAttribute::getUniqueName()
79
     * @return string
80
     */
81 165
    public function getGetterName()
82
    {
83 165
        return $this->replaceReservedMethod(sprintf('get%s', ucfirst($this->getUniqueName('get'))), $this->getOwner()->getPackagedName());
84
    }
85
    /**
86
     * Returns the getter name for this attribute
87
     * @uses StructAttribute::getUniqueName()
88
     * @return string
89
     */
90 165
    public function getSetterName()
91
    {
92 165
        return $this->replaceReservedMethod(sprintf('set%s', ucfirst($this->getUniqueName('set'))), $this->getOwner()->getPackagedName());
93
    }
94
    /**
95
     * Returns the type value
96
     * @return string
97
     */
98 280
    public function getType()
99
    {
100 280
        return $this->type;
101
    }
102
    /**
103
     * Sets the type value
104
     * @param string $type
105
     * @return StructAttribute
106
     */
107 1145
    public function setType($type)
108
    {
109 1145
        $this->type = $type;
110 1145
        return $this;
111
    }
112
    /**
113
     * Returns the type value
114
     * @return bool
115
     */
116 15
    public function getContainsElements()
117
    {
118 15
        return $this->containsElements;
119
    }
120
    /**
121
     * Sets the type value
122
     * @param bool $containsElements
123
     * @return StructAttribute
124
     */
125 440
    public function setContainsElements($containsElements)
126
    {
127 440
        $this->containsElements = $containsElements;
128 440
        return $this;
129
    }
130
    /**
131
     * @return bool
132
     */
133 175
    public function getRemovableFromRequest()
134
    {
135 175
        return $this->removableFromRequest;
136
    }
137
    /**
138
     * @param bool $removableFromRequest
139
     * @return StructAttribute
140
     */
141 435
    public function setRemovableFromRequest($removableFromRequest)
142
    {
143 435
        $this->removableFromRequest = $removableFromRequest;
144 435
        return $this;
145
    }
146
    /**
147
     * If this attribute contains elements then it's an array
148
     * only if its parent, the Struct, is not itself an array,
149
     * if the parent is an array, then it is certainly not an array too
150
     * @return bool
151
     */
152 190
    public function isArray()
153
    {
154 190
        return $this->containsElements || $this->isTypeStructArray();
155
    }
156
    /**
157
     * Returns potential default value
158
     * @uses AbstractModel::getMetaValueFirstSet()
159
     * @uses Utils::getValueWithinItsType()
160
     * @uses StructAttribute::getType()
161
     * @uses StructAttribute::getContainsElements()
162
     * @return mixed
163
     */
164 160
    public function getDefaultValue()
165
    {
166 160
        if ($this->isArray()) {
167 95
            return [];
168
        }
169 125
        return Utils::getValueWithinItsType($this->getMetaValueFirstSet([
170 125
            'default',
171 50
            'Default',
172 50
            'DefaultValue',
173 50
            'defaultValue',
174 50
            'defaultvalue',
175 125
        ]), $this->getType());
176
    }
177
    /**
178
     * Returns true or false depending on minOccurs information associated to the attribute
179
     * @uses AbstractModel::getMetaValueFirstSet()
180
     * @uses AbstractModel::getMetaValue()
181
     * @return bool true|false
182
     */
183 160
    public function isRequired()
184
    {
185 160
        return ($this->getMetaValue('use', '') === 'required' || $this->getMetaValueFirstSet([
186 155
            'minOccurs',
187 62
            'minoccurs',
188 62
            'MinOccurs',
189 62
            'Minoccurs',
190 160
        ], false));
191
    }
192
    /**
193
     * Returns the owner model object, meaning a Struct object
194
     * @see AbstractModel::getOwner()
195
     * @uses AbstractModel::getOwner()
196
     * @return Struct
197
     */
198 175
    public function getOwner()
199
    {
200 175
        return parent::getOwner();
201
    }
202
    /**
203
     * @uses StructAttribute::getType()
204
     * @return bool
205
     */
206 160
    public function isXml()
207
    {
208 160
        return stripos($this->getType(), '\DOM') === 0;
209
    }
210
    /**
211
     * @return Struct|null
212
     */
213 260
    public function getTypeStruct()
214
    {
215 260
        return $this->getGenerator()->getStruct($this->getType());
216
    }
217
    /**
218
     * @return string[]
219
     */
220 220
    public function getTypeStructMeta()
221
    {
222 220
        $typeStruct = $this->getTypeStruct();
223 220
        return ($typeStruct && !$typeStruct->isStruct()) ? $typeStruct->getMeta() : [];
224
    }
225
    /**
226
     * @return bool
227
     */
228 155
    public function isTypeStructArray()
229
    {
230 155
        $typeStruct = $this->getTypeStruct();
231 155
        return $typeStruct && $typeStruct->isArray() && !$typeStruct->isStruct();
232
    }
233
    /**
234
     * @return Struct|null
235
     */
236 220
    public function getInheritanceStruct()
237
    {
238 220
        return $this->getGenerator()->getStruct($this->getInheritance());
239
    }
240
    /**
241
     * @return string[]
242
     */
243 220
    public function getInheritanceStructMeta()
244
    {
245 220
        $inheritanceStruct = $this->getInheritanceStruct();
246 220
        return ($inheritanceStruct && !$inheritanceStruct->isStruct()) ? $inheritanceStruct->getMeta() : [];
247
    }
248
    /**
249
     * @see \WsdlToPhp\PackageGenerator\Model\AbstractModel::getMeta()
250
     * @return string[]
251
     */
252 220
    public function getMeta()
253
    {
254 220
        return array_merge_recursive(parent::getMeta(), $this->getTypeStructMeta(), $this->getInheritanceStructMeta());
255
    }
256
    /**
257
     * @param $filename
258
     * @return StructReservedMethod|StructArrayReservedMethod
259
     */
260 170
    public function getReservedMethodsInstance($filename = null)
261
    {
262 170
        return $this->getOwner()->getReservedMethodsInstance($filename);
263
    }
264
    /**
265
     * {@inheritDoc}
266
     * @see \WsdlToPhp\PackageGenerator\Model\AbstractModel::toJsonSerialize()
267
     */
268 5
    protected function toJsonSerialize()
269
    {
270
        return [
271 5
            'containsElements' => $this->containsElements,
272 5
            'removableFromRequest' => $this->removableFromRequest,
273 5
            'type' => $this->type,
274 2
        ];
275
    }
276
}
277