Completed
Push — master ( 1bff5d...2a2446 )
by Mikaël
75:38 queued 45:53
created

StructAttribute::setRemovableFromRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
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
    private $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
    private $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
    private $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 464
    public function __construct(Generator $generator, $name, $type, Struct $struct)
44
    {
45 464
        parent::__construct($generator, $name);
46 348
        $this
47 464
            ->setType($type)
48 464
            ->setOwner($struct);
49 464
    }
50
    /**
51
     * Returns the unique name in the current struct (for setters/getters and struct contrusctor array)
52
     * @uses AbstractModel::getCleanName()
53
     * @uses AbstractModel::getName()
54
     * @uses AbstractModel::uniqueName()
55
     * @uses StructAttribute::getOwner()
56
     * @return string
57
     */
58 108
    public function getUniqueName()
59
    {
60 108
        return self::uniqueName($this->getCleanName(), $this->getOwner()->getName());
61
    }
62
    /**
63
     * Returns the getter name for this attribute
64
     * @uses StructAttribute::getUniqueName()
65
     * @return string
66
     */
67 104
    public function getGetterName()
68
    {
69 104
        return $this->replaceReservedMethod(sprintf('get%s', ucfirst(self::getUniqueName())), $this->getOwner()->getPackagedName());
70
    }
71
    /**
72
     * Returns the getter name for this attribute
73
     * @uses StructAttribute::getUniqueName()
74
     * @return string
75
     */
76 104
    public function getSetterName()
77
    {
78 104
        return $this->replaceReservedMethod(sprintf('set%s', ucfirst(self::getUniqueName())), $this->getOwner()->getPackagedName());
79
    }
80
    /**
81
     * Returns the type value
82
     * @return string
83
     */
84 304
    public function getType()
85
    {
86 304
        return $this->type;
87
    }
88
    /**
89
     * Sets the type value
90
     * @param string $type
91
     * @return StructAttribute
92
     */
93 464
    public function setType($type)
94
    {
95 464
        $this->type = $type;
96 464
        return $this;
97
    }
98
    /**
99
     * Returns the type value
100
     * @return bool
101
     */
102 12
    public function getContainsElements()
103
    {
104 12
        return $this->containsElements;
105
    }
106
    /**
107
     * Sets the type value
108
     * @param bool $containsElements
109
     * @return StructAttribute
110
     */
111 296
    public function setContainsElements($containsElements)
112
    {
113 296
        $this->containsElements = $containsElements;
114 296
        return $this;
115
    }
116
    /**
117
     * @return bool
118
     */
119 116
    public function getRemovableFromRequest()
120
    {
121 116
        return $this->removableFromRequest;
122
    }
123
    /**
124
     * @param bool $removableFromRequest
125
     * @return StructAttribute
126
     */
127 292
    public function setRemovableFromRequest($removableFromRequest)
128
    {
129 292
        $this->removableFromRequest = $removableFromRequest;
130 292
        return $this;
131
    }
132
    /**
133
     * If this attribute contains elements then it's an array
134
     * only if its parent, the Struct, is not itself an array,
135
     * if the parent is an array, then it is certainly not an array too
136
     * @return bool
137
     */
138 104
    public function isArray()
139
    {
140 104
        return $this->containsElements || $this->isTypeStructArray();
141
    }
142
    /**
143
     * Returns potential default value
144
     * @uses AbstractModel::getMetaValueFirstSet()
145
     * @uses Utils::getValueWithinItsType()
146
     * @uses StructAttribute::getType()
147
     * @uses StructAttribute::getContainsElements()
148
     * @return mixed
149
     */
150 104
    public function getDefaultValue()
151
    {
152 104
        if ($this->isArray()) {
153 60
            return array();
154
        }
155 76
        return Utils::getValueWithinItsType($this->getMetaValueFirstSet(array(
156 76
            'default',
157 57
            'Default',
158 57
            'DefaultValue',
159 57
            'defaultValue',
160 57
            'defaultvalue',
161 76
        )), $this->getType());
162
    }
163
    /**
164
     * Returns true or false depending on minOccurs information associated to the attribute
165
     * @uses AbstractModel::getMetaValueFirstSet()
166
     * @uses AbstractModel::getMetaValue()
167
     * @return bool true|false
168
     */
169 104
    public function isRequired()
170
    {
171 104
        return ($this->getMetaValue('use', '') === 'required' || $this->getMetaValueFirstSet(array(
172 100
            'minOccurs',
173 75
            'minoccurs',
174 75
            'MinOccurs',
175 75
            'Minoccurs',
176 104
        ), false));
177
    }
178
    /**
179
     * Returns the owner model object, meaning a Struct object
180
     * @see AbstractModel::getOwner()
181
     * @uses AbstractModel::getOwner()
182
     * @return Struct
183
     */
184 112
    public function getOwner()
185
    {
186 112
        return parent::getOwner();
187
    }
188
    /**
189
     * @uses StructAttribute::getType()
190
     * @return bool
191
     */
192 104
    public function isXml()
193
    {
194 104
        return stripos($this->getType(), '\DOM') === 0;
195
    }
196
    /**
197
     * @return Struct|null
198
     */
199 124
    public function getTypeStruct()
200
    {
201 124
        return $this->getGenerator()->getStruct($this->getType());
202
    }
203
    /**
204
     * @return string[]
205
     */
206 124
    public function getTypeStructMeta()
207
    {
208 124
        $typeStruct = $this->getTypeStruct();
209 124
        return ($typeStruct && !$typeStruct->getIsStruct()) ? $typeStruct->getMeta() : array();
210
    }
211
    /**
212
     * @return bool
213
     */
214 76
    public function isTypeStructArray()
215
    {
216 76
        $typeStruct = $this->getTypeStruct();
217 76
        return $typeStruct && $typeStruct->isArray() && !$typeStruct->getIsStruct();
218
    }
219
    /**
220
     * @return Struct|null
221
     */
222 124
    public function getInheritanceStruct()
223
    {
224 124
        return $this->getGenerator()->getStruct($this->getInheritance());
225
    }
226
    /**
227
     * @return string[]
228
     */
229 124
    public function getInheritanceStructMeta()
230
    {
231 124
        $inheritanceStruct = $this->getInheritanceStruct();
232 124
        return ($inheritanceStruct && !$inheritanceStruct->getIsStruct()) ? $inheritanceStruct->getMeta() : array();
233
    }
234
    /**
235
     * @see \WsdlToPhp\PackageGenerator\Model\AbstractModel::getMeta()
236
     * @return string[]
237
     */
238 124
    public function getMeta()
239
    {
240 124
        return array_merge_recursive(parent::getMeta(), $this->getTypeStructMeta(), $this->getInheritanceStructMeta());
241
    }
242
    /**
243
     * @param $filename
244
     * @return StructReservedMethod|StructArrayReservedMethod
245
     */
246 108
    public function getReservedMethodsInstance($filename = null)
247
    {
248 108
        return $this->getOwner()->getReservedMethodsInstance($filename);
249
    }
250
}
251