Completed
Push — feature/issue-158 ( 8b9ab6...25acf9 )
by Mikaël
23:05 queued 36s
created

StructAttribute::getUniqueString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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