Completed
Push — feature/issue-48 ( 6a0d6d...c28322 )
by Mikaël
28:17
created

StructAttribute::getContainsElements()   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 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
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
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 432
    public function __construct(Generator $generator, $name, $type, Struct $struct)
42
    {
43 432
        parent::__construct($generator, $name);
44 432
        $this->setType($type);
45 432
        $this->setOwner($struct);
46 432
    }
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 88
    public function getUniqueName()
56
    {
57 88
        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 84
    public function getGetterName()
65
    {
66 84
        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 84
    public function getSetterName()
74
    {
75 84
        return sprintf('set%s', ucfirst(self::getUniqueName()));
76
    }
77
    /**
78
     * Returns the type value
79
     * @return string
80
     */
81 280
    public function getType()
82
    {
83 280
        return $this->type;
84
    }
85
    /**
86
     * Sets the type value
87
     * @param string $type
88
     * @return StructAttribute
89
     */
90 432
    public function setType($type)
91
    {
92 432
        $this->type = $type;
93 432
        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 272
    public function setContainsElements($containsElements)
109
    {
110 272
        $this->containsElements = $containsElements;
111 272
        return $this;
112
    }
113
    /**
114
     * @return bool
115
     */
116 12
    public function getRemovableFromRequest()
117
    {
118 12
        return $this->removableFromRequest;
119
    }
120
    /**
121
     * @param bool $removableFromRequest
122
     * @return StructAttribute
123
     */
124 268
    public function setRemovableFromRequest($removableFromRequest)
125
    {
126 268
        $this->removableFromRequest = $removableFromRequest;
127 268
        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 84
    public function isArray()
136
    {
137 84
        return $this->containsElements;
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 84
    public function getDefaultValue()
148
    {
149 84
        if ($this->isArray()) {
150 44
            return array();
151
        }
152 56
        return Utils::getValueWithinItsType($this->getMetaValueFirstSet(array(
153 56
            'default',
154 42
            'Default',
155 42
            'DefaultValue',
156 42
            'defaultValue',
157 42
            'defaultvalue',
158 56
        )), $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 84
    public function isRequired()
167
    {
168 84
        return ($this->getMetaValue('use', '') === 'required' || $this->getMetaValueFirstSet(array(
169 80
            'minOccurs',
170 60
            'minoccurs',
171 60
            'MinOccurs',
172 60
            'Minoccurs',
173 84
        ), 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 88
    public function getOwner()
182
    {
183 88
        return parent::getOwner();
184
    }
185
    /**
186
     * @uses StructAttribute::getType()
187
     * @return bool
188
     */
189 84
    public function isXml()
190
    {
191 84
        return stripos($this->getType(), '\DOM') === 0;
192
    }
193
    /**
194
     * @return Struct|null
195
     */
196 104
    public function getTypeStruct()
197
    {
198 104
        return $this->getGenerator()->getStruct($this->getType());
199
    }
200
    /**
201
     * @return string[]
202
     */
203 104
    public function getTypeStructMeta()
204
    {
205 104
        $typeStruct = $this->getTypeStruct();
206 104
        return ($typeStruct && !$typeStruct->getIsStruct()) ? $typeStruct->getMeta() : array();
207
    }
208
    /**
209
     * @return Struct|null
210
     */
211 104
    public function getInheritanceStruct()
212
    {
213 104
        return $this->getGenerator()->getStruct($this->getInheritance());
214
    }
215
    /**
216
     * @return string[]
217
     */
218 104
    public function getInheritanceStructMeta()
219
    {
220 104
        $inheritanceStruct = $this->getInheritanceStruct();
221 104
        return ($inheritanceStruct && !$inheritanceStruct->getIsStruct()) ? $inheritanceStruct->getMeta() : array();
222
    }
223
    /**
224
     * @see \WsdlToPhp\PackageGenerator\Model\AbstractModel::getMeta()
225
     * @return string[]
226
     */
227 104
    public function getMeta()
228
    {
229 104
        return array_merge_recursive(parent::getMeta(), $this->getTypeStructMeta(), $this->getInheritanceStructMeta());
230
    }
231
}
232