Completed
Push — feature/issue-47 ( cdce8d...8413b2 )
by Mikaël
24:42
created

StructAttribute::getInheritanceStruct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace WsdlToPhp\PackageGenerator\Model;
4
5
use WsdlToPhp\PackageGenerator\Generator\Utils;
6
use WsdlToPhp\PackageGenerator\Generator\Generator;
7
8
/**
9
 * Class StructAttribute stands for an available struct attribute described in the WSDL
10
 */
11
class StructAttribute extends AbstractModel
12
{
13
    /**
14
     * Type of the struct attribute
15
     * @var string
16
     */
17
    private $type = '';
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
     * @var bool
22
     */
23
    private $containsElements = false;
24
    /**
25
     * Main constructor
26
     * @see AbstractModel::__construct()
27
     * @uses StructAttribute::setType()
28
     * @uses AbstractModel::setOwner()
29
     * @param Generator $generator
30
     * @param string $name the original name
31
     * @param string $type the type
32
     * @param Struct $struct defines the struct which owns this value
33
     */
34 408
    public function __construct(Generator $generator, $name, $type, Struct $struct)
35
    {
36 408
        parent::__construct($generator, $name);
37 408
        $this->setType($type);
38 408
        $this->setOwner($struct);
39 408
    }
40
    /**
41
     * Returns the unique name in the current struct (for setters/getters and struct contrusctor array)
42
     * @uses AbstractModel::getCleanName()
43
     * @uses AbstractModel::getName()
44
     * @uses AbstractModel::uniqueName()
45
     * @uses StructAttribute::getOwner()
46
     * @return string
47
     */
48 80
    public function getUniqueName()
49
    {
50 80
        return self::uniqueName($this->getCleanName(), $this->getOwner()->getName());
51
    }
52
    /**
53
     * Returns the getter name for this attribute
54
     * @uses StructAttribute::getUniqueName()
55
     * @return string
56
     */
57 76
    public function getGetterName()
58
    {
59 76
        return sprintf('get%s', ucfirst(self::getUniqueName()));
60
    }
61
    /**
62
     * Returns the getter name for this attribute
63
     * @uses StructAttribute::getUniqueName()
64
     * @return string
65
     */
66 76
    public function getSetterName()
67
    {
68 76
        return sprintf('set%s', ucfirst(self::getUniqueName()));
69
    }
70
    /**
71
     * Returns the type value
72
     * @return string
73
     */
74 256
    public function getType()
75
    {
76 256
        return $this->type;
77
    }
78
    /**
79
     * Sets the type value
80
     * @param string $type
81
     * @return StructAttribute
82
     */
83 408
    public function setType($type)
84
    {
85 408
        $this->type = $type;
86 408
        return $this;
87
    }
88
    /**
89
     * Returns the type value
90
     * @return bool
91
     */
92 4
    public function getContainsElements()
93
    {
94 4
        return $this->containsElements;
95
    }
96
    /**
97
     * Sets the type value
98
     * @param bool $containsElements
99
     * @return StructAttribute
100
     */
101 244
    public function setContainsElements($containsElements)
102
    {
103 244
        $this->containsElements = $containsElements;
104 244
        return $this;
105
    }
106
    /**
107
     * If this attribute contains elements then it's an array
108
     * only if its parent, the Struct, is not itself an array,
109
     * if the parent is an array, then it is certainly not an array too
110
     * @return bool
111
     */
112 76
    public function isArray()
113
    {
114 76
        return $this->containsElements;
115
    }
116
    /**
117
     * Returns potential default value
118
     * @uses AbstractModel::getMetaValueFirstSet()
119
     * @uses Utils::getValueWithinItsType()
120
     * @uses StructAttribute::getType()
121
     * @uses StructAttribute::getContainsElements()
122
     * @return mixed
123
     */
124 76
    public function getDefaultValue()
125
    {
126 76
        if ($this->isArray()) {
127 44
            return array();
128
        }
129 48
        return Utils::getValueWithinItsType($this->getMetaValueFirstSet(array(
130 48
            'default',
131 36
            'Default',
132 36
            'DefaultValue',
133 36
            'defaultValue',
134 36
            'defaultvalue',
135 48
        )), $this->getType());
136
    }
137
    /**
138
     * Returns true or false depending on minOccurs information associated to the attribute
139
     * @uses AbstractModel::getMetaValueFirstSet()
140
     * @uses AbstractModel::getMetaValue()
141
     * @return bool true|false
142
     */
143 76
    public function isRequired()
144
    {
145 76
        return ($this->getMetaValue('use', '') === 'required' || $this->getMetaValueFirstSet(array(
146 76
            'minOccurs',
147 57
            'minoccurs',
148 57
            'MinOccurs',
149 57
            'Minoccurs',
150 76
        ), false));
151
    }
152
    /**
153
     * Returns the owner model object, meaning a Struct object
154
     * @see AbstractModel::getOwner()
155
     * @uses AbstractModel::getOwner()
156
     * @return Struct
157
     */
158 80
    public function getOwner()
159
    {
160 80
        return parent::getOwner();
161
    }
162
    /**
163
     * @uses StructAttribute::getType()
164
     * @return bool
165
     */
166 76
    public function isXml()
167
    {
168 76
        return stripos($this->getType(), '\DOM') === 0;
169
    }
170
    /**
171
     * @return Struct|null
172
     */
173 88
    public function getTypeStruct()
174
    {
175 88
        return $this->getGenerator()->getStruct($this->getType());
176
    }
177
    /**
178
     * @return Struct|null
179
     */
180 88
    public function getInheritanceStruct()
181
    {
182 88
        return $this->getGenerator()->getStruct($this->getInheritance());
183
    }
184
    /**
185
     * @see \WsdlToPhp\PackageGenerator\Model\AbstractModel::getMeta()
186
     * @return string[]
187
     */
188 88
    public function getMeta()
189
    {
190 88
        $typeStruct = $this->getTypeStruct();
191 88
        $inheritanceStruct = $this->getInheritanceStruct();
192 88
        return array_merge_recursive(parent::getMeta(), ($typeStruct && !$typeStruct->getIsStruct()) ? $typeStruct->getMeta() : array(), ($inheritanceStruct && !$inheritanceStruct->getIsStruct()) ? $inheritanceStruct->getMeta() : array());
193
    }
194
}
195