Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
29 / 29
100.00% covered (success)
100.00%
14 / 14
CRAP
100.00% covered (success)
100.00%
1 / 1
Struct
100.00% covered (success)
100.00%
29 / 29
100.00% covered (success)
100.00%
14 / 14
21
100.00% covered (success)
100.00%
1 / 1
 getStructByName
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getStructByNameAndType
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 addVirtualStruct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 addStruct
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
 addStructWithAttribute
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 addUnionStruct
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 getVirtual
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 getByType
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 getObjectKeyWithType
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getObjectKeyWithVirtual
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getTypeKey
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getVirtualKey
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 add
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 objectClass
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace WsdlToPhp\PackageGenerator\Container\Model;
6
7use WsdlToPhp\PackageGenerator\Model\Struct as Model;
8
9final class Struct extends AbstractModel
10{
11    /**
12     * Only for virtually-considered objects (in order to avoid duplications in objects property).
13     */
14    protected array $virtualObjects = [];
15
16    public function getStructByName(string $name): ?Model
17    {
18        return $this->get($name);
19    }
20
21    public function getStructByNameAndType(string $name, string $type): ?Model
22    {
23        return $this->getByType($name, $type);
24    }
25
26    public function addVirtualStruct(string $structName, string $structType = ''): self
27    {
28        return $this->addStruct($structName, false, $structType);
29    }
30
31    public function addStruct(string $structName, bool $isStruct = true, string $structType = ''): self
32    {
33        if (null === (empty($structType) ? $this->get($structName) : $this->getByType($structName, $structType))) {
34            $model = new Model($this->generator, $structName, $isStruct);
35            $this->add($model->setInheritance($structType));
36        }
37
38        return $this;
39    }
40
41    public function addStructWithAttribute(string $structName, string $attributeName, string $attributeType): self
42    {
43        $this->addStruct($structName);
44        if (($struct = $this->getStructByName($structName)) instanceof Model) {
45            $struct->addAttribute($attributeName, $attributeType);
46        }
47
48        return $this;
49    }
50
51    public function addUnionStruct(string $structName, array $types): self
52    {
53        $this->addVirtualStruct($structName);
54        if (($struct = $this->getStructByName($structName)) instanceof Model) {
55            $struct->setTypes($types);
56        }
57
58        return $this;
59    }
60
61    public function getVirtual(string $value): ?Model
62    {
63        $key = $this->getVirtualKey($value);
64
65        return array_key_exists($key, $this->virtualObjects) ? $this->virtualObjects[$key] : null;
66    }
67
68    public function getByType(string $value, string $type): ?Model
69    {
70        $key = $this->getTypeKey($value, $type);
71
72        return array_key_exists($key, $this->objects) ? $this->objects[$key] : null;
73    }
74
75    public function getObjectKeyWithType(object $object, string $type): string
76    {
77        return $this->getTypeKey($this->getObjectKey($object), $type);
78    }
79
80    public function getObjectKeyWithVirtual(object $object): string
81    {
82        return $this->getVirtualKey($this->getObjectKey($object));
83    }
84
85    /**
86     * The key must not conflict with possible key values.
87     */
88    public function getTypeKey(string $name, string $type): string
89    {
90        return sprintf('struct_name_%s-type_%s', $name, $type);
91    }
92
93    /**
94     * The key must not conflict with possible key values.
95     */
96    public function getVirtualKey(string $name): string
97    {
98        return sprintf('virtual_struct_name_%s', $name);
99    }
100
101    /**
102     * 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.
103     *
104     * @param Model $object
105     */
106    public function add(object $object): self
107    {
108        $inheritance = $object->getInheritance();
109        if (!empty($inheritance)) {
110            $this->virtualObjects[$this->getObjectKeyWithVirtual($object)] = $this->objects[$this->getObjectKeyWithType($object, $object->getInheritance())] = $object;
111        } else {
112            parent::add($object);
113        }
114
115        return $this;
116    }
117
118    protected function objectClass(): string
119    {
120        return Model::class;
121    }
122}