Completed
Push — feature/issue-99 ( def87e )
by Mikaël
02:17
created

StructAttribute::getUniqueName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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