Passed
Push — develop ( 5a78c9...82a2ac )
by Mikaël
49:04 queued 14s
created

StructAttribute::getRemovableFromRequest()   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 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace WsdlToPhp\PackageGenerator\Model;
6
7
use DOMDocument;
8
use WsdlToPhp\PackageGenerator\ConfigurationReader\AbstractReservedWord;
9
use WsdlToPhp\PackageGenerator\Generator\Generator;
10
use WsdlToPhp\PackageGenerator\Generator\Utils;
11
12
/**
13
 * Class StructAttribute stands for an available struct attribute described in the WSDL.
14
 */
15
final class StructAttribute extends AbstractModel
16
{
17
    protected string $type = '';
18
19
    /**
20
     * Defines that this property is not a simple value but an array of values
21
     * Infos at {@link https://www.w3.org/TR/xmlschema-0/#OccurrenceConstraints}.
22
     */
23
    protected bool $containsElements = false;
24
25
    /**
26
     * Defines that this property can be removed from request or not.
27
     * The property can be removed from the request (meaning from the Struct) as soon as the nillable=true && minOccurs=0
28
     * Infos at {@link http://www.w3schools.com/xml/el_element.asp}.
29
     */
30
    protected bool $removableFromRequest = false;
31
32 382
    public function __construct(Generator $generator, string $name, string $type = '', ?Struct $struct = null)
33
    {
34 382
        parent::__construct($generator, $name);
35
        $this
36 382
            ->setType($type)
37 382
            ->setOwner($struct)
38
        ;
39 382
    }
40
41 108
    public function getUniqueString(string $string, string $additionalContext = ''): string
42
    {
43 108
        return self::uniqueName($string, spl_object_hash($this->getOwner()).$this->getOwner()->getName().$additionalContext);
44
    }
45
46 108
    public function getUniqueName(string $additionalContext = ''): string
47
    {
48 108
        return $this->getUniqueString($this->getCleanName(), $additionalContext);
49
    }
50
51 104
    public function getGetterName(): string
52
    {
53 104
        return $this->replaceReservedMethod(sprintf('get%s', ucfirst($this->getUniqueName('get'))), $this->getOwner()->getPackagedName());
54
    }
55
56 104
    public function getSetterName(): string
57
    {
58 104
        return $this->replaceReservedMethod(sprintf('set%s', ucfirst($this->getUniqueName('set'))), $this->getOwner()->getPackagedName());
59
    }
60
61 146
    public function getType(bool $useTypeStruct = false): string
62
    {
63 146
        if ($useTypeStruct) {
64 80
            $typeStruct = $this->getTypeStruct();
65 80
            if ($typeStruct instanceof Struct) {
66 44
                $type = $typeStruct->getTopInheritance();
67
68 44
                return $type ? $type : $this->type;
69
            }
70
        }
71
72 146
        return $this->type;
73
    }
74
75 382
    public function setType(string $type): StructAttribute
76
    {
77 382
        $this->type = $type;
78
79 382
        return $this;
80
    }
81
82 6
    public function getContainsElements(): bool
83
    {
84 6
        return $this->containsElements;
85
    }
86
87
    /**
88
     * If already able to contain several occurrences, it must stay as it is, the wider behaviour wins.
89
     */
90 224
    public function setContainsElements(bool $containsElements = true): StructAttribute
91
    {
92 224
        $this->containsElements = $this->containsElements || $containsElements;
93
94 224
        return $this;
95
    }
96
97 108
    public function getRemovableFromRequest(): bool
98
    {
99 108
        return $this->removableFromRequest;
100
    }
101
102 102
    public function isAChoice(): bool
103
    {
104 102
        return is_array($this->getMetaValue('choice'));
105
    }
106
107
    /**
108
     * If already able to be removed from request, it must stay as it is, the wider behaviour wins.
109
     */
110 222
    public function setRemovableFromRequest(bool $removableFromRequest = true): StructAttribute
111
    {
112 222
        $this->removableFromRequest = $this->removableFromRequest || $removableFromRequest;
113
114 222
        return $this;
115
    }
116
117
    /**
118
     * If this attribute contains elements then it's an array
119
     * only if its parent, the Struct, is not itself an array,
120
     * if the parent is an array, then it is certainly not an array too.
121
     */
122 116
    public function isArray(): bool
123
    {
124 116
        return $this->containsElements || $this->isTypeStructArray();
125
    }
126
127
    /**
128
     * If this attribute is based on a struct that is a list,
129
     * then it is a list of basic scalar values that are sent space-separated.
130
     */
131 116
    public function isList(): bool
132
    {
133 116
        $typeStruct = $this->getTypeStruct();
134
135 116
        return $typeStruct && $typeStruct->isList();
136
    }
137
138
    /**
139
     * @return array|bool|float|int|string
140
     */
141 94
    public function getDefaultValue()
142
    {
143 94
        if ($this->isArray() || $this->isList()) {
144 48
            return [];
145
        }
146
147 82
        if (($struct = $this->getTypeStruct()) && $struct->isStruct()) {
148 60
            return null;
149
        }
150
151 80
        return Utils::getValueWithinItsType($this->getMetaValueFirstSet([
152 80
            'default',
153
            'Default',
154
            'DefaultValue',
155
            'defaultValue',
156
            'defaultvalue',
157 80
        ]), $this->getType(true));
158
    }
159
160 104
    public function isRequired(): bool
161
    {
162 104
        return 'required' === $this->getMetaValue('use', '') || 0 < $this->getMetaValueFirstSet([
163 102
            'minOccurs',
164
            'minoccurs',
165
            'MinOccurs',
166
            'Minoccurs',
167 104
        ], 0);
168
    }
169
170 156
    public function isNullable(): bool
171
    {
172 156
        return 'true' === $this->getMetaValue('nillable', 'false');
173
    }
174
175 114
    public function getOwner(): Struct
176
    {
177 114
        return parent::getOwner();
0 ignored issues
show
Bug Best Practice introduced by
The expression return parent::getOwner() returns the type null which is incompatible with the type-hinted return WsdlToPhp\PackageGenerator\Model\Struct.
Loading history...
178
    }
179
180 138
    public function isXml(): bool
181
    {
182 138
        return DOMDocument::class === $this->getType();
183
    }
184 138
185
    public function getTypeStruct(): ?Struct
186
    {
187 138
        $struct = $this->getGenerator()->getStructByNameAndType($this->getType(), $this->getInheritance());
188
189 138
        return $struct ? $struct : $this->getGenerator()->getStructByName($this->getType());
190
    }
191 138
192
    public function getTypeStructMeta(): array
193
    {
194 100
        $typeStruct = $this->getTypeStruct();
195
196 100
        return ($typeStruct && !$typeStruct->isStruct()) ? $typeStruct->getMeta() : [];
197
    }
198 100
199
    public function isTypeStructArray(): bool
200
    {
201 138
        $typeStruct = $this->getTypeStruct();
202
203 138
        return $typeStruct && $typeStruct->isArray() && !$typeStruct->isStruct();
204
    }
205
206 138
    public function getInheritanceStruct(): ?Struct
207
    {
208 138
        return $this->getGenerator()->getStructByName($this->getInheritance());
209
    }
210 138
211
    public function getInheritanceStructMeta(): array
212
    {
213 138
        $inheritanceStruct = $this->getInheritanceStruct();
214
215 138
        return ($inheritanceStruct && !$inheritanceStruct->isStruct()) ? $inheritanceStruct->getMeta() : [];
216
    }
217
218 106
    public function getMeta(): array
219
    {
220 106
        return $this->mergeMeta($this->getInheritanceStructMeta(), $this->getTypeStructMeta(), parent::getMeta());
221
    }
222
223 2
    public function getReservedMethodsInstance(?string $filename = null): AbstractReservedWord
224
    {
225
        return $this->getOwner()->getReservedMethodsInstance($filename);
226 2
    }
227 2
228 2
    protected function toJsonSerialize(): array
229
    {
230
        return [
231
            'containsElements' => $this->containsElements,
232
            'removableFromRequest' => $this->removableFromRequest,
233
            'type' => $this->type,
234
        ];
235
    }
236
}
237