Completed
Push — feature/issue-157 ( 8c1a44 )
by Mikaël
20:07
created

Struct::addVirtualStruct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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
     * @see \WsdlToPhp\PackageGenerator\Container\Model\Model::objectClass()
11
     * @return string
12
     */
13 660
    protected function objectClass()
14
    {
15 660
        return 'WsdlToPhp\PackageGenerator\Model\Struct';
16
    }
17
    /**
18
     * @param string $name
19
     * @return Model|null
20
     */
21 665
    public function getStructByName($name)
22
    {
23 665
        return $this->get($name);
24
    }
25
    /**
26
     * @param string $name
27
     * @param string $type
28
     * @return Model|null
29
     */
30 10
    public function getStructByNameAndType($name, $type)
31
    {
32 10
        return $this->getByType($name, $type);
33
    }
34
    /**
35
     * Adds a virtual struct
36
     * @param string $structName the original struct name
37
     * @return Struct
38
     */
39 245
    public function addVirtualStruct($structName)
40
    {
41 245
        return $this->addStruct($structName, false);
42
    }
43
    /**
44
     * Adds type to structs
45
     * @param string $structName the original struct name
46
     * @param bool $isStruct whether the Struct has to be generated or not
47
     * @return Struct
48
     */
49 255
    public function addStruct($structName, $isStruct = true)
50
    {
51 255
        if ($this->get($structName) === null) {
52 245
            $this->add(new Model($this->generator, $structName, $isStruct));
53 147
        }
54 255
        return $this;
55
    }
56
    /**
57
     * Adds type to structs and its attribute
58
     * @param string $structName the original struct name
59
     * @param string $attributeName the attribute name
60
     * @param string $attributeType the attribute type
61
     * @return Struct
62
     */
63 255
    public function addStructWithAttribute($structName, $attributeName, $attributeType)
64
    {
65 255
        $this->addStruct($structName);
66 255
        if (($struct = $this->getStructByName($structName)) instanceof Model) {
67 255
            $struct->addAttribute($attributeName, $attributeType);
68 153
        }
69 255
        return $this;
70 3
    }
71
    /**
72
     * Adds a union struct
73
     * @param string $structName
74
     * @param string[] $types
75
     * @return Struct
76
     */
77 20
    public function addUnionStruct($structName, $types)
78
    {
79 20
        $this->addVirtualStruct($structName);
80 20
        if (($struct = $this->getStructByName($structName)) instanceof Model) {
81 20
            $struct->setTypes($types);
82 12
        }
83 20
        return $this;
84
    }
85
    /**
86
     * @see \WsdlToPhp\PackageGenerator\Model\AbstractModel::get()
87
     * @param string $value
88
     * @return Model|null
89
     */
90 665
    public function get($value)
91
    {
92 665
        return parent::get($value);
93
    }
94
    /**
95
     * @see parent::get()
96
     * @param string $value
97
     * @param string $type
98
     * @return mixed
99
     */
100 10
    public function getByType($value, $type)
101
    {
102 10
        if (!is_string($value) && !is_int($value)) {
103
            throw new \InvalidArgumentException(sprintf('Value "%s" can\'t be used to get an object from "%s"', var_export($value, true), get_class($this)), __LINE__);
104
        }
105 10
        if (!is_scalar($type)) {
106
            throw new \InvalidArgumentException(sprintf('Value "%s" can\'t be used to get an object', var_export($type, true)), __LINE__);
107
        }
108 10
        $key = $this->getTypeKey($value, $type);
109 10
        return array_key_exists($key, $this->objects) ? $this->objects[$key] : null;
110
    }
111
    /**
112
     * @param $object
113
     * @param $type
114
     * @return string
115
     */
116 215
    public function getObjectWithTypeKey($object, $type)
117
    {
118 215
        return $this->getTypeKey($this->getObjectKey($object), $type);
119
    }
120
    /**
121
     * The key must not conflict with possible key values
122
     * @param $name
123
     * @param $type
124
     * @return string
125
     */
126 220
    public function getTypeKey($name, $type)
127
    {
128 220
        return sprintf('struct_name_%s-type_%s', $name, $type);
129
    }
130
    /**
131
     * By overriding this method, we ensure that each time a new object is stored as usual, it is also stored with our new key.
132
     * Nonetheless, it is stored only if its type is known.
133
     * @param Model $object
134
     * @return Struct
135
     */
136 660
    public function add($object)
137
    {
138 660
        parent::add($object);
139 660
        $inheritance = $object->getInheritance();
140 660
        if (!empty($inheritance)) {
141 215
            $this->objects[$this->getObjectWithTypeKey($object, $object->getInheritance())] = $object;
142 129
        }
143 660
        return $this;
144
    }
145
}
146