Passed
Push — 2.x ( bef6c0...34f135 )
by Mikaël
03:54
created

StructAttribute::isAChoice()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 1
cts 1
cp 1
crap 1
rs 10
c 0
b 0
f 0
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 can 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 1410
    public function __construct(Generator $generator, $name, $type = '', Struct $struct = null)
44
    {
45 1410
        parent::__construct($generator, $name);
46 1410
        $this->setType($type)->setOwner($struct);
47 1410
    }
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 $string
55
     * @param string $additionalContext
56
     * @return string
57
     */
58 210
    public function getUniqueString($string, $additionalContext = '')
59
    {
60 210
        return self::uniqueName($string, $this->getOwner()->getName() . $additionalContext);
61
    }
62
    /**
63
     * Returns the unique name in the current struct (for setters/getters and struct contrusctor array)
64
     * @uses AbstractModel::getCleanName()
65
     * @uses AbstractModel::getName()
66
     * @uses AbstractModel::uniqueName()
67
     * @uses StructAttribute::getOwner()
68
     * @param string $additionalContext
69
     * @return string
70
     */
71 210
    public function getUniqueName($additionalContext = '')
72
    {
73 210
        return $this->getUniqueString($this->getCleanName(), $additionalContext);
74
    }
75
    /**
76
     * Returns the getter name for this attribute
77
     * @uses StructAttribute::getUniqueName()
78
     * @return string
79
     */
80 204
    public function getGetterName()
81
    {
82 204
        return $this->replaceReservedMethod(sprintf('get%s', ucfirst($this->getUniqueName('get'))), $this->getOwner()->getPackagedName());
83
    }
84
    /**
85
     * Returns the getter name for this attribute
86
     * @uses StructAttribute::getUniqueName()
87
     * @return string
88
     */
89 204
    public function getSetterName()
90
    {
91 204
        return $this->replaceReservedMethod(sprintf('set%s', ucfirst($this->getUniqueName('set'))), $this->getOwner()->getPackagedName());
92
    }
93
    /**
94
     * Returns the type value
95
     * @return string
96
     */
97 360
    public function getType($useTypeStruct = false)
98
    {
99 360
        if ($useTypeStruct) {
100 162
            $typeStruct = $this->getTypeStruct();
101 162
            if ($typeStruct instanceof Struct) {
102 144
                $type = $typeStruct->getTopInheritance();
103 144
                return $type ? $type : $this->type;
104
            }
105 66
        }
106 360
        return $this->type;
107
    }
108
    /**
109
     * Sets the type value
110
     * @param string $type
111
     * @return StructAttribute
112
     */
113 1410
    public function setType($type)
114
    {
115 1410
        $this->type = $type;
116 1410
        return $this;
117
    }
118
    /**
119
     * Returns the type value
120
     * @return bool
121
     */
122 18
    public function getContainsElements()
123
    {
124 18
        return $this->containsElements;
125
    }
126
    /**
127
     * Sets the type value
128
     * @param bool $containsElements
129
     * @return StructAttribute
130
     */
131 564
    public function setContainsElements($containsElements)
132
    {
133 564
        $this->containsElements = $containsElements;
134 564
        return $this;
135
    }
136
    /**
137
     * @return bool
138
     */
139 216
    public function getRemovableFromRequest()
140
    {
141 216
        return $this->removableFromRequest;
142
    }
143
    /**
144
     * @return bool
145
     */
146
    public function isAChoice()
147 558
    {
148
        return is_array($this->getMetaValue('choice'));
149 558
    }
150 558
    /**
151
     * @param bool $removableFromRequest
152
     * @return StructAttribute
153
     */
154
    public function setRemovableFromRequest($removableFromRequest)
155
    {
156
        $this->removableFromRequest = $removableFromRequest;
157
        return $this;
158 240
    }
159
    /**
160 240
     * If this attribute contains elements then it's an array
161
     * only if its parent, the Struct, is not itself an array,
162
     * if the parent is an array, then it is certainly not an array too
163
     * @return bool
164
     */
165
    public function isArray()
166
    {
167
        return $this->containsElements || $this->isTypeStructArray();
168
    }
169
    /**
170 204
     * If this attribute is based on a struct that is a list,
171
     * then it is list of basic sclar values that are sent space-separated
172 204
     * @return bool
173 114
     */
174
    public function isList()
175 162
    {
176 162
        $typeStruct = $this->getTypeStruct();
177 81
        return $typeStruct && $typeStruct->isList();
178 81
    }
179 81
    /**
180 81
     * Returns potential default value
181 162
     * @uses AbstractModel::getMetaValueFirstSet()
182
     * @uses Utils::getValueWithinItsType()
183
     * @uses StructAttribute::getType()
184
     * @uses StructAttribute::getContainsElements()
185
     * @return mixed
186
     */
187
    public function getDefaultValue()
188
    {
189 198
        if ($this->isArray()) {
190
            return [];
191 198
        }
192 192
        return Utils::getValueWithinItsType($this->getMetaValueFirstSet([
193 96
            'default',
194 96
            'Default',
195 96
            'DefaultValue',
196 198
            'defaultValue',
197
            'defaultvalue',
198
        ]), $this->getType(true));
199
    }
200
    /**
201
     * Returns true or false depending on minOccurs information associated to the attribute
202
     * @uses AbstractModel::getMetaValueFirstSet()
203
     * @uses AbstractModel::getMetaValue()
204 216
     * @return bool true|false
205
     */
206 216
    public function isRequired()
207
    {
208
        return ($this->getMetaValue('use', '') === 'required' || $this->getMetaValueFirstSet([
209
            'minOccurs',
210
            'minoccurs',
211
            'MinOccurs',
212 198
            'Minoccurs',
213
        ], false));
214 198
    }
215
    /**
216
     * Returns the owner model object, meaning a Struct object
217
     * @see AbstractModel::getOwner()
218
     * @uses AbstractModel::getOwner()
219 330
     * @return Struct
220
     */
221 330
    public function getOwner()
222 330
    {
223
        return parent::getOwner();
224
    }
225
    /**
226
     * @uses StructAttribute::getType()
227 282
     * @return bool
228
     */
229 282
    public function isXml()
230 282
    {
231
        return stripos($this->getType(), '\DOM') === 0;
232
    }
233
    /**
234
     * @return Struct|null
235 198
     */
236
    public function getTypeStruct()
237 198
    {
238 198
        $struct = $this->getGenerator()->getStructByNameAndType($this->getType(), $this->getInheritance());
239
        return $struct ? $struct : $this->getGenerator()->getStructByName($this->getType());
240
    }
241
    /**
242
     * @return string[]
243 282
     */
244
    public function getTypeStructMeta()
245 282
    {
246
        $typeStruct = $this->getTypeStruct();
247
        return ($typeStruct && !$typeStruct->isStruct()) ? $typeStruct->getMeta() : [];
248
    }
249
    /**
250 282
     * @return bool
251
     */
252 282
    public function isTypeStructArray()
253 282
    {
254
        $typeStruct = $this->getTypeStruct();
255
        return $typeStruct && $typeStruct->isArray() && !$typeStruct->isStruct();
256
    }
257
    /**
258
     * @return Struct|null
259 282
     */
260
    public function getInheritanceStruct()
261 282
    {
262
        return $this->getGenerator()->getStructByName($this->getInheritance());
263
    }
264
    /**
265
     * @return string[]
266
     */
267 210
    public function getInheritanceStructMeta()
268
    {
269 210
        $inheritanceStruct = $this->getInheritanceStruct();
270
        return ($inheritanceStruct && !$inheritanceStruct->isStruct()) ? $inheritanceStruct->getMeta() : [];
271
    }
272
    /**
273
     * @see \WsdlToPhp\PackageGenerator\Model\AbstractModel::getMeta()
274
     * @return string[]
275 6
     */
276
    public function getMeta()
277
    {
278 6
        return array_merge_recursive(parent::getMeta(), $this->getTypeStructMeta(), $this->getInheritanceStructMeta());
279 6
    }
280 6
    /**
281 3
     * @param $filename
282
     * @return StructReservedMethod|StructArrayReservedMethod
283
     */
284
    public function getReservedMethodsInstance($filename = null)
285
    {
286
        return $this->getOwner()->getReservedMethodsInstance($filename);
287
    }
288
    /**
289
     * {@inheritDoc}
290
     * @see \WsdlToPhp\PackageGenerator\Model\AbstractModel::toJsonSerialize()
291
     */
292
    protected function toJsonSerialize()
293
    {
294
        return [
295
            'containsElements' => $this->containsElements,
296
            'removableFromRequest' => $this->removableFromRequest,
297
            'type' => $this->type,
298
        ];
299
    }
300
}
301