Completed
Push — master ( ab3100...ab2db9 )
by Mikaël
57:47 queued 20:08
created

Struct::add()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 9
ccs 7
cts 7
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace WsdlToPhp\PackageGenerator\Container\Model;
4
5
use WsdlToPhp\PackageGenerator\Model\Struct as Model;
6
7
class Struct extends AbstractModel
8
{
9
    /**
10
     * Only for virtually-considered objects (in order to avoid duplucations in objects property)
11
     * @var array $virtualObjects
12
     */
13
    protected $virtualObjects = [];
14
    /**
15
     * @see \WsdlToPhp\PackageGenerator\Container\Model\Model::objectClass()
16
     * @return string
17
     */
18 828
    protected function objectClass()
19
    {
20 828
        return 'WsdlToPhp\PackageGenerator\Model\Struct';
21
    }
22
    /**
23
     * @param string $name
24
     * @return Model|null
25
     */
26 804
    public function getStructByName($name)
27
    {
28 804
        return $this->get($name);
29
    }
30
    /**
31
     * @param string $name
32
     * @param string $type
33
     * @return Model|null
34
     */
35 366
    public function getStructByNameAndType($name, $type)
36
    {
37 366
        return $this->getByType($name, $type);
38
    }
39
    /**
40
     * Adds a virtual struct
41
     * @param string $structName the original struct name
42
     * @param string $structType the original struct type
43
     * @return Struct
44
     */
45 300
    public function addVirtualStruct($structName, $structType = '')
46
    {
47 300
        return $this->addStruct($structName, false, $structType);
48
    }
49
    /**
50
     * Adds type to structs
51
     * @param string $structName the original struct name
52
     * @param bool $isStruct whether the Struct has to be generated or not
53
     * @param string $structType the original struct type
54
     * @return Struct
55
     */
56 312
    public function addStruct($structName, $isStruct = true, $structType = '')
57
    {
58 312
        if (null === (empty($structType) ? $this->get($structName) : $this->getByType($structName, $structType))) {
59 300
            $model = new Model($this->generator, $structName, $isStruct);
60 300
            $this->add($model->setInheritance($structType));
61 150
        }
62 312
        return $this;
63
    }
64
    /**
65
     * Adds type to structs and its attribute
66
     * @param string $structName the original struct name
67
     * @param string $attributeName the attribute name
68
     * @param string $attributeType the attribute type
69
     * @return Struct
70
     */
71 312
    public function addStructWithAttribute($structName, $attributeName, $attributeType)
72
    {
73 312
        $this->addStruct($structName);
74 312
        if (($struct = $this->getStructByName($structName)) instanceof Model) {
75 312
            $struct->addAttribute($attributeName, $attributeType);
76 156
        }
77 312
        return $this;
78
    }
79
    /**
80
     * Adds a union struct
81
     * @param string $structName
82
     * @param string[] $types
83
     * @return Struct
84
     */
85 30
    public function addUnionStruct($structName, $types)
86
    {
87 30
        $this->addVirtualStruct($structName);
88 30
        if (($struct = $this->getStructByName($structName)) instanceof Model) {
89 30
            $struct->setTypes($types);
90 15
        }
91 30
        return $this;
92
    }
93
    /**
94
     * @see \WsdlToPhp\PackageGenerator\Model\AbstractModel::get()
95
     * @param string $value
96
     * @return Model|null
97
     */
98 804
    public function get($value)
99
    {
100 804
        return parent::get($value);
101
    }
102
    /**
103
     * @see parent::get()
104
     * @throws \InvalidArgumentException
105
     * @param string $value
106
     * @return mixed
107
     */
108 618
    public function getVirtual($value)
109
    {
110 618
        if (!is_scalar($value)) {
0 ignored issues
show
introduced by
The condition is_scalar($value) is always true.
Loading history...
111 6
            throw new \InvalidArgumentException(sprintf('Value "%s" can\'t be used to get an object from "%s"', var_export($value, true), __CLASS__), __LINE__);
112
        }
113 612
        $key = $this->getVirtualKey($value);
114 612
        return array_key_exists($key, $this->virtualObjects) ? $this->virtualObjects[$key] : null;
115
    }
116
    /**
117
     * @see parent::get()
118
     * @throws \InvalidArgumentException
119
     * @param string $value
120
     * @param string $type
121
     * @return mixed
122
     */
123 582
    public function getByType($value, $type)
124
    {
125 582
        if (!is_scalar($value)) {
0 ignored issues
show
introduced by
The condition is_scalar($value) is always true.
Loading history...
126 6
            throw new \InvalidArgumentException(sprintf('Value "%s" can\'t be used to get an object from "%s"', var_export($value, true), __CLASS__), __LINE__);
127
        }
128 576
        if (!is_scalar($type)) {
0 ignored issues
show
introduced by
The condition is_scalar($type) is always true.
Loading history...
129 6
            throw new \InvalidArgumentException(sprintf('Type "%s" can\'t be used to get an object from "%s"', var_export($type, true), __CLASS__), __LINE__);
130
        }
131 570
        $key = $this->getTypeKey($value, $type);
132 570
        return array_key_exists($key, $this->objects) ? $this->objects[$key] : null;
133
    }
134
    /**
135
     * @param $object
136
     * @param $type
137
     * @return string
138
     */
139 792
    public function getObjectKeyWithType($object, $type)
140
    {
141 792
        return $this->getTypeKey($this->getObjectKey($object), $type);
142
    }
143
    /**
144
     * @param $object
145
     * @return string
146
     */
147 792
    public function getObjectKeyWithVirtual($object)
148
    {
149 792
        return $this->getVirtualKey($this->getObjectKey($object));
150
    }
151
    /**
152
     * The key must not conflict with possible key values
153
     * @param $name
154
     * @param $type
155
     * @return string
156
     */
157 858
    public function getTypeKey($name, $type)
158
    {
159 858
        return sprintf('struct_name_%s-type_%s', $name, $type);
160
    }
161
    /**
162
     * The key must not conflict with possible key values
163
     * @param $type
164
     * @return string
165
     */
166 870
    public function getVirtualKey($name)
167
    {
168 870
        return sprintf('virtual_struct_name_%s', $name);
169
    }
170
    /**
171
     * By overriding this method, we ensure that each time a new object is stored, it is stored with our new key if the inheritance is defined.
172
     * @param Model $object
173
     * @return Struct
174
     */
175 834
    public function add($object)
176
    {
177 834
        $inheritance = $object->getInheritance();
178 834
        if (!empty($inheritance)) {
179 792
            $this->virtualObjects[$this->getObjectKeyWithVirtual($object)] = $this->objects[$this->getObjectKeyWithType($object, $object->getInheritance())] = $object;
180 396
        } else {
181 828
            parent::add($object);
182
        }
183 834
        return $this;
184
    }
185
}
186