Passed
Push — master ( 2d7948...93347a )
by Viacheslav
03:31
created

Wrapper   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 209
Duplicated Lines 0 %

Test Coverage

Coverage 58.33%

Importance

Changes 0
Metric Value
wmc 25
eloc 44
dl 0
loc 209
ccs 35
cts 60
cp 0.5833
rs 10
c 0
b 0
f 0

21 Methods

Rating   Name   Duplication   Size   Complexity  
A __get() 0 3 1
A __isset() 0 3 1
A nested() 0 3 1
A getDefault() 0 3 1
A makeObjectItem() 0 3 1
A getNestedPropertyNames() 0 3 1
A getProperties() 0 3 1
A out() 0 3 1
A getMeta() 0 3 1
A process() 0 3 1
A exportSchema() 0 3 1
A getProperty() 0 3 1
A jsonSerialize() 0 3 1
A addMeta() 0 4 1
A in() 0 3 1
A getObjectItemClass() 0 3 1
A __call() 0 11 3
A __set() 0 8 2
A setDefault() 0 9 2
A __construct() 0 5 1
A getPropertyNames() 0 3 1
1
<?php
2
3
namespace Swaggest\JsonSchema;
4
5
use Swaggest\JsonSchema\Meta\MetaHolder;
6
use Swaggest\JsonSchema\Structure\Nested;
7
8
class Wrapper implements SchemaContract, MetaHolder, SchemaExporter, \JsonSerializable
9
{
10
    /** @var Schema */
11
    private $schema;
12
13
    /**
14
     * Keeps reference to original instance for centralized Meta storage
15
     * @var Schema
16
     */
17
    private $originalSchema;
18
19
    public $objectItemClass;
20
21
    private $cloned = false;
22
23
    /**
24
     * ImmutableSchema constructor.
25
     * @param Schema $schema
26
     */
27 10
    public function __construct(Schema $schema)
28
    {
29 10
        $this->schema = $schema;
30 10
        $this->originalSchema = $schema;
31 10
        $this->objectItemClass = $schema->objectItemClass;
32 10
    }
33
34
    /**
35
     * @param mixed $data
36
     * @param Context $options
37
     * @param string $path
38
     * @param mixed|null $result
39
     * @return array|mixed|null|object|\stdClass
40
     * @throws InvalidValue
41
     * @throws \Exception
42
     * @throws \Swaggest\JsonDiff\Exception
43
     */
44 1576
    public function process($data, Context $options, $path = '#', $result = null)
45
    {
46 1576
        return $this->schema->process($data, $options, $path, $result);
47
    }
48
49
    /**
50
     * @param mixed $data
51
     * @param Context|null $options
52
     * @return array|mixed|null|object|\stdClass
53
     * @throws Exception
54
     * @throws InvalidValue
55
     */
56 3236
    public function in($data, Context $options = null)
57
    {
58 3236
        return $this->schema->in($data, $options);
59
    }
60
61
    /**
62
     * @param mixed $data
63
     * @param Context|null $options
64
     * @return array|mixed|null|object|\stdClass
65
     * @throws InvalidValue
66
     * @throws \Exception
67
     */
68 37
    public function out($data, Context $options = null)
69
    {
70 37
        return $this->schema->out($data, $options);
71
    }
72
73
    /**
74
     * @return string[]
75
     */
76
    public function getPropertyNames()
77
    {
78
        return $this->schema->getPropertyNames();
79
    }
80
81
    /**
82
     * @return string[]
83
     */
84 97
    public function getNestedPropertyNames()
85
    {
86 97
        return $this->schema->getNestedPropertyNames();
87
    }
88
89 4
    public function nested()
90
    {
91 4
        return new Nested($this);
92
    }
93
94
    /**
95
     * @return null|Constraint\Properties
96
     */
97 101
    public function getProperties()
98
    {
99 101
        return $this->schema->properties;
100
    }
101
102
    /**
103
     * @param string $name
104
     * @return null|Schema|SchemaContract
105
     */
106 5
    public function getProperty($name)
107
    {
108 5
        return $this->schema->properties[$name];
109
    }
110
111
    /**
112
     * @param string $name
113
     * @param array $arguments
114
     * @return mixed
115
     * @throws Exception
116
     */
117
    public function __call($name, $arguments)
118
    {
119
        if (substr($name, 0, 3) === 'set') {
120
            if (!$this->cloned) {
121
                $this->schema = clone $this->schema;
122
                $this->cloned = true;
123
            }
124
            $this->schema->$name($arguments[0]); // todo performance check direct set
125
            return $this;
126
        } else {
127
            throw new Exception('Unknown method:' . $name);
128
        }
129
    }
130
131 7
    public function getDefault()
132
    {
133 7
        return $this->schema->default;
134
    }
135
136
    /**
137
     * @param mixed $default
138
     * @return $this
139
     */
140 1
    public function setDefault($default)
141
    {
142 1
        if (!$this->cloned) {
143 1
            $this->schema = clone $this->schema;
144 1
            $this->cloned = true;
145
        }
146
147 1
        $this->schema->default = $default;
148 1
        return $this;
149
    }
150
151
    /**
152
     * @param string $name
153
     * @throws Exception
154
     */
155
    public function __get($name)
156
    {
157
        throw new Exception('Unexpected get: ' . $name);
158
    }
159
160
    /**
161
     * @param string $name
162
     * @param mixed $value
163
     * @return Wrapper
164
     */
165
    public function __set($name, $value)
166
    {
167
        if (!$this->cloned) {
168
            $this->schema = clone $this->schema;
169
            $this->cloned = true;
170
        }
171
        $this->schema->$name = $value;
172
        return $this;
173
    }
174
175
    /**
176
     * @param string $name
177
     * @throws Exception
178
     */
179
    public function __isset($name)
180
    {
181
        throw new Exception('Unexpected isset: ' . $name);
182
    }
183
184
    public function addMeta($meta, $name = null)
185
    {
186
        $this->originalSchema->addMeta($meta, $name);
187
        return $this;
188
    }
189
190 1
    public function getMeta($name)
191
    {
192 1
        return $this->originalSchema->getMeta($name);
193
    }
194
195
    /**
196
     * @param Context|null $options
197
     * @return Structure\ObjectItemContract
198
     */
199 5
    public function makeObjectItem(Context $options = null)
200
    {
201 5
        return $this->schema->makeObjectItem($options);
202
    }
203
204 5
    public function getObjectItemClass()
205
    {
206 5
        return $this->objectItemClass;
207
    }
208
209 3
    public function exportSchema()
210
    {
211 3
        return clone $this->schema;
212
    }
213
214
    public function jsonSerialize()
215
    {
216
        return $this->schema->jsonSerialize();
217
    }
218
219
}