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