Completed
Push — master ( ac8a1e...7d894b )
by Mikaël
56:43 queued 30:10
created

StructAttribute::isXml()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
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 1155
    public function __construct(Generator $generator, $name, $type = '', Struct $struct = null)
44
    {
45 1155
        parent::__construct($generator, $name);
46 1155
        $this->setType($type)->setOwner($struct);
47 1155
    }
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 175
    public function getUniqueString($string, $additionalContext= '')
60
    {
61 175
        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 175
    public function getUniqueName($additionalContext = '')
73
    {
74 175
        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 170
    public function getGetterName()
82
    {
83 170
        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 170
    public function getSetterName()
91
    {
92 170
        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 295
    public function getType($useTypeStruct = false)
99
    {
100 295
        if ($useTypeStruct) {
101 135
            $typeStruct = $this->getTypeStruct();
102 135
            if ($typeStruct instanceof Struct) {
103 120
                $type = $typeStruct->getTopInheritance();
104 120
                return $type ? $type : $this->type;
105
            }
106 44
        }
107 295
        return $this->type;
108
    }
109
    /**
110
     * Sets the type value
111
     * @param string $type
112
     * @return StructAttribute
113
     */
114 1155
    public function setType($type)
115
    {
116 1155
        $this->type = $type;
117 1155
        return $this;
118
    }
119
    /**
120
     * Returns the type value
121
     * @return bool
122
     */
123 15
    public function getContainsElements()
124
    {
125 15
        return $this->containsElements;
126
    }
127
    /**
128
     * Sets the type value
129
     * @param bool $containsElements
130
     * @return StructAttribute
131
     */
132 455
    public function setContainsElements($containsElements)
133
    {
134 455
        $this->containsElements = $containsElements;
135 455
        return $this;
136
    }
137
    /**
138
     * @return bool
139
     */
140 180
    public function getRemovableFromRequest()
141
    {
142 180
        return $this->removableFromRequest;
143
    }
144
    /**
145
     * @param bool $removableFromRequest
146
     * @return StructAttribute
147
     */
148 450
    public function setRemovableFromRequest($removableFromRequest)
149
    {
150 450
        $this->removableFromRequest = $removableFromRequest;
151 450
        return $this;
152
    }
153
    /**
154
     * If this attribute contains elements then it's an array
155
     * only if its parent, the Struct, is not itself an array,
156
     * if the parent is an array, then it is certainly not an array too
157
     * @return bool
158
     */
159 200
    public function isArray()
160
    {
161 200
        return $this->containsElements || $this->isTypeStructArray();
162
    }
163
    /**
164
     * Returns potential default value
165
     * @uses AbstractModel::getMetaValueFirstSet()
166
     * @uses Utils::getValueWithinItsType()
167
     * @uses StructAttribute::getType()
168
     * @uses StructAttribute::getContainsElements()
169
     * @return mixed
170
     */
171 170
    public function getDefaultValue()
172
    {
173 170
        if ($this->isArray()) {
174 95
            return [];
175
        }
176 135
        return Utils::getValueWithinItsType($this->getMetaValueFirstSet([
177 135
            'default',
178 54
            'Default',
179 54
            'DefaultValue',
180 54
            'defaultValue',
181 54
            'defaultvalue',
182 135
        ]), $this->getType(true));
183
    }
184
    /**
185
     * Returns true or false depending on minOccurs information associated to the attribute
186
     * @uses AbstractModel::getMetaValueFirstSet()
187
     * @uses AbstractModel::getMetaValue()
188
     * @return bool true|false
189
     */
190 165
    public function isRequired()
191
    {
192 165
        return ($this->getMetaValue('use', '') === 'required' || $this->getMetaValueFirstSet([
193 160
            'minOccurs',
194 64
            'minoccurs',
195 64
            'MinOccurs',
196 64
            'Minoccurs',
197 165
        ], false));
198
    }
199
    /**
200
     * Returns the owner model object, meaning a Struct object
201
     * @see AbstractModel::getOwner()
202
     * @uses AbstractModel::getOwner()
203
     * @return Struct
204
     */
205 180
    public function getOwner()
206
    {
207 180
        return parent::getOwner();
208
    }
209
    /**
210
     * @uses StructAttribute::getType()
211
     * @return bool
212
     */
213 165
    public function isXml()
214
    {
215 165
        return stripos($this->getType(), '\DOM') === 0;
216
    }
217
    /**
218
     * @return Struct|null
219
     */
220 270
    public function getTypeStruct()
221
    {
222 270
        return $this->getGenerator()->getStruct($this->getType());
223
    }
224
    /**
225
     * @return string[]
226
     */
227 230
    public function getTypeStructMeta()
228
    {
229 230
        $typeStruct = $this->getTypeStruct();
230 230
        return ($typeStruct && !$typeStruct->isStruct()) ? $typeStruct->getMeta() : [];
231
    }
232
    /**
233
     * @return bool
234
     */
235 165
    public function isTypeStructArray()
236
    {
237 165
        $typeStruct = $this->getTypeStruct();
238 165
        return $typeStruct && $typeStruct->isArray() && !$typeStruct->isStruct();
239
    }
240
    /**
241
     * @return Struct|null
242
     */
243 230
    public function getInheritanceStruct()
244
    {
245 230
        return $this->getGenerator()->getStruct($this->getInheritance());
246
    }
247
    /**
248
     * @return string[]
249
     */
250 230
    public function getInheritanceStructMeta()
251
    {
252 230
        $inheritanceStruct = $this->getInheritanceStruct();
253 230
        return ($inheritanceStruct && !$inheritanceStruct->isStruct()) ? $inheritanceStruct->getMeta() : [];
254
    }
255
    /**
256
     * @see \WsdlToPhp\PackageGenerator\Model\AbstractModel::getMeta()
257
     * @return string[]
258
     */
259 230
    public function getMeta()
260
    {
261 230
        return array_merge_recursive(parent::getMeta(), $this->getTypeStructMeta(), $this->getInheritanceStructMeta());
262
    }
263
    /**
264
     * @param $filename
265
     * @return StructReservedMethod|StructArrayReservedMethod
266
     */
267 175
    public function getReservedMethodsInstance($filename = null)
268
    {
269 175
        return $this->getOwner()->getReservedMethodsInstance($filename);
270
    }
271
    /**
272
     * {@inheritDoc}
273
     * @see \WsdlToPhp\PackageGenerator\Model\AbstractModel::toJsonSerialize()
274
     */
275 5
    protected function toJsonSerialize()
276
    {
277
        return [
278 5
            'containsElements' => $this->containsElements,
279 5
            'removableFromRequest' => $this->removableFromRequest,
280 5
            'type' => $this->type,
281 2
        ];
282
    }
283
}
284