Passed
Push — develop ( 2d5fab...bcbf5d )
by Mikaël
08:03
created

StructAttribute::getGetterName()   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 386
    public function __construct(Generator $generator, string $name, string $type = '', ?Struct $struct = null)
33
    {
34 386
        parent::__construct($generator, $name);
35
        $this
36 386
            ->setType($type)
37 386
            ->setOwner($struct)
38
        ;
39 386
    }
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 148
    public function getType(bool $useTypeStruct = false): string
62
    {
63 148
        if ($useTypeStruct) {
64 84
            $typeStruct = $this->getTypeStruct();
65 84
            if ($typeStruct instanceof Struct) {
66 46
                $type = $typeStruct->getTopInheritance();
67
68 46
                return $type ? $type : $this->type;
69
            }
70
        }
71
72 148
        return $this->type;
73
    }
74
75 386
    public function setType(string $type): StructAttribute
76
    {
77 386
        $this->type = $type;
78
79 386
        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 232
    public function setContainsElements(bool $containsElements = true): StructAttribute
91
    {
92 232
        $this->containsElements = $this->containsElements || $containsElements;
93
94 232
        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 224
    public function setRemovableFromRequest(bool $removableFromRequest = true): StructAttribute
111
    {
112 224
        $this->removableFromRequest = $this->removableFromRequest || $removableFromRequest;
113
114 224
        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 an array too.
121
     */
122 114
    public function isArray(): bool
123
    {
124 114
        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 114
    public function isList(): bool
132
    {
133 114
        $typeStruct = $this->getTypeStruct();
134
135 114
        return $typeStruct && $typeStruct->isList();
136
    }
137
138
    /**
139
     * @return array|bool|float|int|string
140
     */
141 94
    public function getDefaultValue()
142
    {
143 94
        if (($struct = $this->getTypeStruct()) && $struct->isStruct()) {
144 70
            return null;
145
        }
146
147 84
        return Utils::getValueWithinItsType($this->getMetaValueFirstSet([
148 84
            'default',
149
            'Default',
150
            'DefaultValue',
151
            'defaultValue',
152
            'defaultvalue',
153 84
        ]), $this->getType(true));
154
    }
155
156 104
    public function isRequired(): bool
157
    {
158 104
        return 'required' === $this->getMetaValue('use', '') || 0 < $this->getMetaValueFirstSet([
159 102
            'minOccurs',
160
            'minoccurs',
161
            'MinOccurs',
162
            'Minoccurs',
163 104
        ], 0);
164
    }
165
166 36
    public function isNullable(): bool
167
    {
168 36
        return 'true' === $this->getMetaValue('nillable', 'false');
169
    }
170
171 156
    public function getOwner(): Struct
172
    {
173 156
        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...
174
    }
175
176 114
    public function isXml(): bool
177
    {
178 114
        return DOMDocument::class === $this->getType();
179
    }
180
181 142
    public function getTypeStruct(): ?Struct
182
    {
183 142
        $struct = $this->getGenerator()->getStructByNameAndType($this->getType(), $this->getInheritance());
184
185 142
        return $struct ? $struct : $this->getGenerator()->getStructByName($this->getType());
186
    }
187
188 142
    public function getTypeStructMeta(): array
189
    {
190 142
        $typeStruct = $this->getTypeStruct();
191
192 142
        return ($typeStruct && !$typeStruct->isStruct()) ? $typeStruct->getMeta() : [];
193
    }
194
195 96
    public function isTypeStructArray(): bool
196
    {
197 96
        $typeStruct = $this->getTypeStruct();
198
199 96
        return $typeStruct && $typeStruct->isArray() && !$typeStruct->isStruct();
200
    }
201
202 142
    public function getInheritanceStruct(): ?Struct
203
    {
204 142
        return $this->getGenerator()->getStructByName($this->getInheritance());
205
    }
206
207 142
    public function getInheritanceStructMeta(): array
208
    {
209 142
        $inheritanceStruct = $this->getInheritanceStruct();
210
211 142
        return ($inheritanceStruct && !$inheritanceStruct->isStruct()) ? $inheritanceStruct->getMeta() : [];
212
    }
213
214 142
    public function getMeta(): array
215
    {
216 142
        return $this->mergeMeta($this->getInheritanceStructMeta(), $this->getTypeStructMeta(), parent::getMeta());
217
    }
218
219 106
    public function getReservedMethodsInstance(?string $filename = null): AbstractReservedWord
220
    {
221 106
        return $this->getOwner()->getReservedMethodsInstance($filename);
222
    }
223
224 2
    protected function toJsonSerialize(): array
225
    {
226
        return [
227 2
            'containsElements' => $this->containsElements,
228 2
            'removableFromRequest' => $this->removableFromRequest,
229 2
            'type' => $this->type,
230
        ];
231
    }
232
}
233