Completed
Push — master ( cdc419...060bfa )
by Mikaël
49:47
created

StructAttribute::toJsonSerialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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