Passed
Push — develop ( 27a9c2...644332 )
by Mikaël
100:03 queued 50:03
created

StructAttribute::getReservedMethodsInstance()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
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 1098
    public function __construct(Generator $generator, $name, $type = '', Struct $struct = null)
44
    {
45 1098
        parent::__construct($generator, $name);
46 1098
        $this->setType($type)->setOwner($struct);
47 1098
    }
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 312
    public function getUniqueString($string, $additionalContext = '')
59
    {
60 312
        return self::uniqueName($string, spl_object_hash($this->getOwner()) . $this->getOwner()->getName() . $additionalContext);
61
    }
62
    /**
63
     * Returns the unique name in the current struct (for setters/getters and struct constructor 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 312
    public function getUniqueName($additionalContext = '')
72
    {
73 312
        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 300
    public function getGetterName()
81
    {
82 300
        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 300
    public function getSetterName()
90
    {
91 300
        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 426
    public function getType($useTypeStruct = false)
98
    {
99 426
        if ($useTypeStruct) {
100 258
            $typeStruct = $this->getTypeStruct();
101 258
            if ($typeStruct instanceof Struct) {
102 222
                $type = $typeStruct->getTopInheritance();
103 222
                return $type ? $type : $this->type;
104
            }
105 105
        }
106 426
        return $this->type;
107
    }
108
    /**
109
     * Sets the type value
110
     * @param string $type
111
     * @return StructAttribute
112
     */
113 1098
    public function setType($type)
114
    {
115 1098
        $this->type = $type;
116 1098
        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 624
    public function setContainsElements($containsElements)
132
    {
133 624
        $this->containsElements = $containsElements;
134 624
        return $this;
135
    }
136
    /**
137
     * @return bool
138
     */
139 312
    public function getRemovableFromRequest()
140
    {
141 312
        return $this->removableFromRequest;
142
    }
143
    /**
144
     * @return bool
145
     */
146 294
    public function isAChoice()
147
    {
148 294
        return is_array($this->getMetaValue('choice'));
149
    }
150
    /**
151
     * @param bool $removableFromRequest
152
     * @return StructAttribute
153
     */
154 618
    public function setRemovableFromRequest($removableFromRequest)
155
    {
156 618
        $this->removableFromRequest = $removableFromRequest;
157 618
        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 336
    public function isArray()
166
    {
167 336
        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 scalar values that are sent space-separated
172
     * @return bool
173
     */
174 336
    public function isList()
175
    {
176 336
        $typeStruct = $this->getTypeStruct();
177 336
        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 300
    public function getDefaultValue()
188
    {
189 300
        if ($this->isArray() || $this->isList()) {
190 162
            return [];
191
        }
192 258
193 258
        if (($struct = $this->getTypeStruct()) && $struct->isStruct()) {
194 129
            return null;
195 129
        }
196 129
197 129
        return Utils::getValueWithinItsType($this->getMetaValueFirstSet([
198 258
            'default',
199
            'Default',
200
            'DefaultValue',
201
            'defaultValue',
202
            'defaultvalue',
203
        ]), $this->getType(true));
204
    }
205
    /**
206 300
     * Returns true or false depending on minOccurs information associated to the attribute
207
     * @uses AbstractModel::getMetaValueFirstSet()
208 300
     * @uses AbstractModel::getMetaValue()
209 294
     * @return bool true|false
210 147
     */
211 147
    public function isRequired()
212 147
    {
213 300
        return ($this->getMetaValue('use', '') === 'required' || $this->getMetaValueFirstSet([
214
            'minOccurs',
215
            'minoccurs',
216
            'MinOccurs',
217
            'Minoccurs',
218
        ], false));
219
    }
220
    /**
221 456
     * Returns the owner model object, meaning a Struct object
222
     * @see AbstractModel::getOwner()
223 456
     * @uses AbstractModel::getOwner()
224
     * @return Struct
225
     */
226
    public function getOwner()
227
    {
228
        return parent::getOwner();
229 294
    }
230
    /**
231 294
     * @uses StructAttribute::getType()
232
     * @return bool
233
     */
234
    public function isXml()
235
    {
236 402
        return mb_stripos($this->getType(), '\DOM') === 0;
237
    }
238 402
    /**
239 402
     * @return Struct|null
240
     */
241
    public function getTypeStruct()
242
    {
243
        $struct = $this->getGenerator()->getStructByNameAndType($this->getType(), $this->getInheritance());
244 402
        return $struct ? $struct : $this->getGenerator()->getStructByName($this->getType());
245
    }
246 402
    /**
247 402
     * @return string[]
248
     */
249
    public function getTypeStructMeta()
250
    {
251
        $typeStruct = $this->getTypeStruct();
252 294
        return ($typeStruct && !$typeStruct->isStruct()) ? $typeStruct->getMeta() : [];
253
    }
254 294
    /**
255 294
     * @return bool
256
     */
257
    public function isTypeStructArray()
258
    {
259
        $typeStruct = $this->getTypeStruct();
260 402
        return $typeStruct && $typeStruct->isArray() && !$typeStruct->isStruct();
261
    }
262 402
    /**
263
     * @return Struct|null
264
     */
265
    public function getInheritanceStruct()
266
    {
267 402
        return $this->getGenerator()->getStructByName($this->getInheritance());
268
    }
269 402
    /**
270 402
     * @return string[]
271
     */
272
    public function getInheritanceStructMeta()
273
    {
274
        $inheritanceStruct = $this->getInheritanceStruct();
275
        return ($inheritanceStruct && !$inheritanceStruct->isStruct()) ? $inheritanceStruct->getMeta() : [];
276 402
    }
277
    /**
278 402
     * @see \WsdlToPhp\PackageGenerator\Model\AbstractModel::getMeta()
279
     * @return string[]
280
     */
281
    public function getMeta()
282
    {
283
        return $this->mergeMeta($this->getInheritanceStructMeta(), $this->getTypeStructMeta(), parent::getMeta());
284 306
    }
285
    /**
286 306
     * @param $filename
287
     * @return StructReservedMethod|StructArrayReservedMethod
288
     */
289
    public function getReservedMethodsInstance($filename = null)
290
    {
291
        return $this->getOwner()->getReservedMethodsInstance($filename);
292 6
    }
293
    /**
294
     * {@inheritDoc}
295 6
     * @see \WsdlToPhp\PackageGenerator\Model\AbstractModel::toJsonSerialize()
296 6
     */
297 6
    protected function toJsonSerialize()
298 3
    {
299
        return [
300
            'containsElements' => $this->containsElements,
301
            'removableFromRequest' => $this->removableFromRequest,
302
            'type' => $this->type,
303
        ];
304
    }
305
}
306