Completed
Push — feature/issue-123 ( 501a8f...3c961f )
by Mikaël
26:43
created

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