Completed
Push — develop ( c4ba27...23e0a3 )
by Mikaël
61:45 queued 33:42
created

StructAttribute   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 229
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 5
Bugs 0 Features 1
Metric Value
wmc 30
c 5
b 0
f 1
lcom 2
cbo 4
dl 0
loc 229
ccs 64
cts 64
cp 1
rs 10

21 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getUniqueName() 0 4 1
A getGetterName() 0 4 1
A getSetterName() 0 4 1
A getRemovableFromRequest() 0 4 1
A setRemovableFromRequest() 0 5 1
A getDefaultValue() 0 13 2
A isRequired() 0 9 2
A getOwner() 0 4 1
A isXml() 0 4 1
A getTypeStruct() 0 4 1
A getTypeStructMeta() 0 5 3
A getType() 0 4 1
A setType() 0 5 1
A getContainsElements() 0 4 1
A setContainsElements() 0 5 1
A isArray() 0 4 2
A getInheritanceStruct() 0 4 1
A isTypeStructArray() 0 5 3
A getInheritanceStructMeta() 0 5 3
A getMeta() 0 4 1
1
<?php
2
3
namespace WsdlToPhp\PackageGenerator\Model;
4
5
use WsdlToPhp\PackageGenerator\Generator\Utils;
6
use WsdlToPhp\PackageGenerator\Generator\Generator;
7
8
/**
9
 * Class StructAttribute stands for an available struct attribute described in the WSDL
10
 */
11
class StructAttribute extends AbstractModel
12
{
13
    /**
14
     * Type of the struct attribute
15
     * @var string
16
     */
17
    private $type = '';
18
    /**
19
     * Defines that this property is not a simple value but an array of values
20
     * Infos at {@link https://www.w3.org/TR/xmlschema-0/#OccurrenceConstraints}
21
     * @var bool
22
     */
23
    private $containsElements = false;
24
    /**
25
     * Defines that this property can be removed from request or not.
26
     * The property cna be removed from the request (meaning from the Struct) as soon as the nillable=true && minOccurs=0
27
     * Infos at {@link http://www.w3schools.com/xml/el_element.asp}
28
     * @var bool
29
     */
30
    private $removableFromRequest = false;
31
    /**
32
     * Main constructor
33
     * @see AbstractModel::__construct()
34
     * @uses StructAttribute::setType()
35
     * @uses AbstractModel::setOwner()
36
     * @param Generator $generator
37
     * @param string $name the original name
38
     * @param string $type the type
39
     * @param Struct $struct defines the struct which owns this value
40
     */
41 456
    public function __construct(Generator $generator, $name, $type, Struct $struct)
42
    {
43 456
        parent::__construct($generator, $name);
44 456
        $this->setType($type);
45 456
        $this->setOwner($struct);
46 456
    }
47
    /**
48
     * Returns the unique name in the current struct (for setters/getters and struct contrusctor array)
49
     * @uses AbstractModel::getCleanName()
50
     * @uses AbstractModel::getName()
51
     * @uses AbstractModel::uniqueName()
52
     * @uses StructAttribute::getOwner()
53
     * @return string
54
     */
55 108
    public function getUniqueName()
56
    {
57 108
        return self::uniqueName($this->getCleanName(), $this->getOwner()->getName());
58
    }
59
    /**
60
     * Returns the getter name for this attribute
61
     * @uses StructAttribute::getUniqueName()
62
     * @return string
63
     */
64 104
    public function getGetterName()
65
    {
66 104
        return sprintf('get%s', ucfirst(self::getUniqueName()));
67
    }
68
    /**
69
     * Returns the getter name for this attribute
70
     * @uses StructAttribute::getUniqueName()
71
     * @return string
72
     */
73 104
    public function getSetterName()
74
    {
75 104
        return sprintf('set%s', ucfirst(self::getUniqueName()));
76
    }
77
    /**
78
     * Returns the type value
79
     * @return string
80
     */
81 304
    public function getType()
82
    {
83 304
        return $this->type;
84
    }
85
    /**
86
     * Sets the type value
87
     * @param string $type
88
     * @return StructAttribute
89
     */
90 456
    public function setType($type)
91
    {
92 456
        $this->type = $type;
93 456
        return $this;
94
    }
95
    /**
96
     * Returns the type value
97
     * @return bool
98
     */
99 12
    public function getContainsElements()
100
    {
101 12
        return $this->containsElements;
102
    }
103
    /**
104
     * Sets the type value
105
     * @param bool $containsElements
106
     * @return StructAttribute
107
     */
108 296
    public function setContainsElements($containsElements)
109
    {
110 296
        $this->containsElements = $containsElements;
111 296
        return $this;
112
    }
113
    /**
114
     * @return bool
115
     */
116 116
    public function getRemovableFromRequest()
117
    {
118 116
        return $this->removableFromRequest;
119
    }
120
    /**
121
     * @param bool $removableFromRequest
122
     * @return StructAttribute
123
     */
124 292
    public function setRemovableFromRequest($removableFromRequest)
125
    {
126 292
        $this->removableFromRequest = $removableFromRequest;
127 292
        return $this;
128
    }
129
    /**
130
     * If this attribute contains elements then it's an array
131
     * only if its parent, the Struct, is not itself an array,
132
     * if the parent is an array, then it is certainly not an array too
133
     * @return bool
134
     */
135 104
    public function isArray()
136
    {
137 104
        return $this->containsElements || $this->isTypeStructArray();
138
    }
139
    /**
140
     * Returns potential default value
141
     * @uses AbstractModel::getMetaValueFirstSet()
142
     * @uses Utils::getValueWithinItsType()
143
     * @uses StructAttribute::getType()
144
     * @uses StructAttribute::getContainsElements()
145
     * @return mixed
146
     */
147 104
    public function getDefaultValue()
148
    {
149 104
        if ($this->isArray()) {
150 60
            return array();
151
        }
152 76
        return Utils::getValueWithinItsType($this->getMetaValueFirstSet(array(
153 76
            'default',
154 57
            'Default',
155 57
            'DefaultValue',
156 57
            'defaultValue',
157 57
            'defaultvalue',
158 76
        )), $this->getType());
159
    }
160
    /**
161
     * Returns true or false depending on minOccurs information associated to the attribute
162
     * @uses AbstractModel::getMetaValueFirstSet()
163
     * @uses AbstractModel::getMetaValue()
164
     * @return bool true|false
165
     */
166 104
    public function isRequired()
167
    {
168 104
        return ($this->getMetaValue('use', '') === 'required' || $this->getMetaValueFirstSet(array(
169 100
            'minOccurs',
170 75
            'minoccurs',
171 75
            'MinOccurs',
172 75
            'Minoccurs',
173 104
        ), false));
174
    }
175
    /**
176
     * Returns the owner model object, meaning a Struct object
177
     * @see AbstractModel::getOwner()
178
     * @uses AbstractModel::getOwner()
179
     * @return Struct
180
     */
181 108
    public function getOwner()
182
    {
183 108
        return parent::getOwner();
184
    }
185
    /**
186
     * @uses StructAttribute::getType()
187
     * @return bool
188
     */
189 104
    public function isXml()
190
    {
191 104
        return stripos($this->getType(), '\DOM') === 0;
192
    }
193
    /**
194
     * @return Struct|null
195
     */
196 124
    public function getTypeStruct()
197
    {
198 124
        return $this->getGenerator()->getStruct($this->getType());
199
    }
200
    /**
201
     * @return string[]
202
     */
203 124
    public function getTypeStructMeta()
204
    {
205 124
        $typeStruct = $this->getTypeStruct();
206 124
        return ($typeStruct && !$typeStruct->getIsStruct()) ? $typeStruct->getMeta() : array();
207
    }
208
    /**
209
     * @return bool
210
     */
211 76
    public function isTypeStructArray()
212
    {
213 76
        $typeStruct = $this->getTypeStruct();
214 76
        return $typeStruct && $typeStruct->isArray() && !$typeStruct->getIsStruct();
215
    }
216
    /**
217
     * @return Struct|null
218
     */
219 124
    public function getInheritanceStruct()
220
    {
221 124
        return $this->getGenerator()->getStruct($this->getInheritance());
222
    }
223
    /**
224
     * @return string[]
225
     */
226 124
    public function getInheritanceStructMeta()
227
    {
228 124
        $inheritanceStruct = $this->getInheritanceStruct();
229 124
        return ($inheritanceStruct && !$inheritanceStruct->getIsStruct()) ? $inheritanceStruct->getMeta() : array();
230
    }
231
    /**
232
     * @see \WsdlToPhp\PackageGenerator\Model\AbstractModel::getMeta()
233
     * @return string[]
234
     */
235 124
    public function getMeta()
236
    {
237 124
        return array_merge_recursive(parent::getMeta(), $this->getTypeStructMeta(), $this->getInheritanceStructMeta());
238
    }
239
}
240