Completed
Push — develop ( 4170b6...85aaf1 )
by Mikaël
22:49
created

StructAttribute::isAChoice()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
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 1458
    public function __construct(Generator $generator, $name, $type = '', Struct $struct = null)
44
    {
45 1458
        parent::__construct($generator, $name);
46 1458
        $this->setType($type)->setOwner($struct);
47 1458
    }
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 222
    public function getUniqueString($string, $additionalContext = '')
59
    {
60 222
        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 222
    public function getUniqueName($additionalContext = '')
72
    {
73 222
        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 216
    public function getGetterName()
81
    {
82 216
        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 216
    public function getSetterName()
90
    {
91 216
        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 390
    public function getType($useTypeStruct = false)
98
    {
99 390
        if ($useTypeStruct) {
100 174
            $typeStruct = $this->getTypeStruct();
101 174
            if ($typeStruct instanceof Struct) {
102 156
                $type = $typeStruct->getTopInheritance();
103 156
                return $type ? $type : $this->type;
104
            }
105 69
        }
106 390
        return $this->type;
107
    }
108
    /**
109
     * Sets the type value
110
     * @param string $type
111
     * @return StructAttribute
112
     */
113 1458
    public function setType($type)
114
    {
115 1458
        $this->type = $type;
116 1458
        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 576
    public function setContainsElements($containsElements)
132
    {
133 576
        $this->containsElements = $containsElements;
134 576
        return $this;
135
    }
136
    /**
137
     * @return bool
138
     */
139 228
    public function getRemovableFromRequest()
140
    {
141 228
        return $this->removableFromRequest;
142
    }
143
    /**
144
     * @return bool
145
     */
146 210
    public function isAChoice()
147
    {
148 210
        return is_array($this->getMetaValue('choice'));
149
    }
150
    /**
151
     * @param bool $removableFromRequest
152
     * @return StructAttribute
153
     */
154 570
    public function setRemovableFromRequest($removableFromRequest)
155
    {
156 570
        $this->removableFromRequest = $removableFromRequest;
157 570
        return $this;
158
    }
159
    /**
160
     * 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 252
    public function isArray()
166
    {
167 252
        return $this->containsElements || $this->isTypeStructArray();
168
    }
169
    /**
170
     * 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
     * @return bool
173
     */
174 246
    public function isList()
175
    {
176 246
        $typeStruct = $this->getTypeStruct();
177 246
        return $typeStruct && $typeStruct->isList();
178
    }
179
    /**
180
     * Returns potential default value
181
     * @uses AbstractModel::getMetaValueFirstSet()
182
     * @uses Utils::getValueWithinItsType()
183
     * @uses StructAttribute::getType()
184
     * @uses StructAttribute::getContainsElements()
185
     * @return mixed
186
     */
187 216
    public function getDefaultValue()
188
    {
189 216
        if ($this->isArray()) {
190 114
            return [];
191
        }
192 174
        return Utils::getValueWithinItsType($this->getMetaValueFirstSet([
193 174
            'default',
194 87
            'Default',
195 87
            'DefaultValue',
196 87
            'defaultValue',
197 87
            'defaultvalue',
198 174
        ]), $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
     * @return bool true|false
205
     */
206 210
    public function isRequired()
207
    {
208 210
        return ($this->getMetaValue('use', '') === 'required' || $this->getMetaValueFirstSet([
209 204
            'minOccurs',
210 102
            'minoccurs',
211 102
            'MinOccurs',
212 102
            'Minoccurs',
213 210
        ], false));
214
    }
215
    /**
216
     * Returns the owner model object, meaning a Struct object
217
     * @see AbstractModel::getOwner()
218
     * @uses AbstractModel::getOwner()
219
     * @return Struct
220
     */
221 240
    public function getOwner()
222
    {
223 240
        return parent::getOwner();
224
    }
225
    /**
226
     * @uses StructAttribute::getType()
227
     * @return bool
228
     */
229 210
    public function isXml()
230
    {
231 210
        return stripos($this->getType(), '\DOM') === 0;
232
    }
233
    /**
234
     * @return Struct|null
235
     */
236 360
    public function getTypeStruct()
237
    {
238 360
        $struct = $this->getGenerator()->getStructByNameAndType($this->getType(), $this->getInheritance());
239 360
        return $struct ? $struct : $this->getGenerator()->getStructByName($this->getType());
240
    }
241
    /**
242
     * @return string[]
243
     */
244 312
    public function getTypeStructMeta()
245
    {
246 312
        $typeStruct = $this->getTypeStruct();
247 312
        return ($typeStruct && !$typeStruct->isStruct()) ? $typeStruct->getMeta() : [];
248
    }
249
    /**
250
     * @return bool
251
     */
252 210
    public function isTypeStructArray()
253
    {
254 210
        $typeStruct = $this->getTypeStruct();
255 210
        return $typeStruct && $typeStruct->isArray() && !$typeStruct->isStruct();
256
    }
257
    /**
258
     * @return Struct|null
259
     */
260 312
    public function getInheritanceStruct()
261
    {
262 312
        return $this->getGenerator()->getStructByName($this->getInheritance());
263
    }
264
    /**
265
     * @return string[]
266
     */
267 312
    public function getInheritanceStructMeta()
268
    {
269 312
        $inheritanceStruct = $this->getInheritanceStruct();
270 312
        return ($inheritanceStruct && !$inheritanceStruct->isStruct()) ? $inheritanceStruct->getMeta() : [];
271
    }
272
    /**
273
     * @see \WsdlToPhp\PackageGenerator\Model\AbstractModel::getMeta()
274
     * @return string[]
275
     */
276 312
    public function getMeta()
277
    {
278 312
        return array_merge_recursive(parent::getMeta(), $this->getTypeStructMeta(), $this->getInheritanceStructMeta());
279
    }
280
    /**
281
     * @param $filename
282
     * @return StructReservedMethod|StructArrayReservedMethod
283
     */
284 222
    public function getReservedMethodsInstance($filename = null)
285
    {
286 222
        return $this->getOwner()->getReservedMethodsInstance($filename);
287
    }
288
    /**
289
     * {@inheritDoc}
290
     * @see \WsdlToPhp\PackageGenerator\Model\AbstractModel::toJsonSerialize()
291
     */
292 6
    protected function toJsonSerialize()
293
    {
294
        return [
295 6
            'containsElements' => $this->containsElements,
296 6
            'removableFromRequest' => $this->removableFromRequest,
297 6
            'type' => $this->type,
298 3
        ];
299
    }
300
}
301