Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
61 / 61
100.00% covered (success)
100.00%
27 / 27
CRAP
100.00% covered (success)
100.00%
1 / 1
StructAttribute
100.00% covered (success)
100.00%
61 / 61
100.00% covered (success)
100.00%
27 / 27
44
100.00% covered (success)
100.00%
1 / 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
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
4
 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%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
 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            $typeStruct = $this->getTypeStruct();
64            if ($typeStruct instanceof Struct) {
65                $type = $typeStruct->getTopInheritance();
66
67                return $type ?: $this->type;
68            }
69        }
70
71        return $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 'required' === $this->getMetaValue('use', '') || 0 < $this->getMetaValueFirstSet([
158            'minOccurs',
159            'minoccurs',
160            'MinOccurs',
161            'Minoccurs',
162        ], 0);
163    }
164
165    public function isNullable(): bool
166    {
167        return 'true' === $this->getMetaValue('nillable', 'false');
168    }
169
170    public function getOwner(): Struct
171    {
172        return parent::getOwner();
173    }
174
175    public function isXml(): bool
176    {
177        return \DOMDocument::class === $this->getType();
178    }
179
180    public function getTypeStruct(): ?Struct
181    {
182        $struct = $this->getGenerator()->getStructByNameAndType($this->getType(), $this->getInheritance());
183
184        return $struct ?: $this->getGenerator()->getStructByName($this->getType());
185    }
186
187    public function getTypeStructMeta(): array
188    {
189        $typeStruct = $this->getTypeStruct();
190
191        return ($typeStruct && !$typeStruct->isStruct()) ? $typeStruct->getMeta() : [];
192    }
193
194    public function isTypeStructArray(): bool
195    {
196        $typeStruct = $this->getTypeStruct();
197
198        return $typeStruct && $typeStruct->isArray() && !$typeStruct->isStruct();
199    }
200
201    public function getInheritanceStruct(): ?Struct
202    {
203        return $this->getGenerator()->getStructByName($this->getInheritance());
204    }
205
206    public function getInheritanceStructMeta(): array
207    {
208        $inheritanceStruct = $this->getInheritanceStruct();
209
210        return ($inheritanceStruct && !$inheritanceStruct->isStruct()) ? $inheritanceStruct->getMeta() : [];
211    }
212
213    public function getMeta(): array
214    {
215        return $this->mergeMeta($this->getInheritanceStructMeta(), $this->getTypeStructMeta(), parent::getMeta());
216    }
217
218    public function getReservedMethodsInstance(?string $filename = null): AbstractReservedWord
219    {
220        return $this->getOwner()->getReservedMethodsInstance($filename);
221    }
222
223    protected function toJsonSerialize(): array
224    {
225        return [
226            'containsElements' => $this->containsElements,
227            'removableFromRequest' => $this->removableFromRequest,
228            'type' => $this->type,
229        ];
230    }
231}