Completed
Push — master ( bd1396...8457c9 )
by Viacheslav
9s
created

Wrapper::nested()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Swaggest\JsonSchema;
4
5
use Swaggest\JsonSchema\Meta\Meta;
6
use Swaggest\JsonSchema\Meta\MetaHolder;
7
use Swaggest\JsonSchema\Structure\Nested;
8
9
class Wrapper implements SchemaContract, MetaHolder
10
{
11
    /** @var Schema */
12
    private $schema;
13
14
    /** @var Schema */
15
    private $originalSchema;
16
17
    public $objectItemClass;
18
19
    private $cloned = false;
20
21
    /**
22
     * ImmutableSchema constructor.
23
     * @param Schema $schema
24
     */
25 6
    public function __construct(Schema $schema)
26
    {
27 6
        $this->schema = $schema;
28 6
        $this->originalSchema = $schema;
29 6
        $this->objectItemClass = $schema->objectItemClass;
30 6
    }
31
32
    /**
33
     * @param mixed $data
34
     * @param Context $options
35
     * @param string $path
36
     * @param mixed|null $result
37
     * @return array|mixed|null|object|\stdClass
38
     * @throws InvalidValue
39
     * @throws \Exception
40
     * @throws \Swaggest\JsonDiff\Exception
41
     */
42 1523
    public function process($data, Context $options, $path = '#', $result = null)
43
    {
44 1523
        return $this->schema->process($data, $options, $path, $result);
45
    }
46
47
    /**
48
     * @param mixed $data
49
     * @param Context|null $options
50
     * @return array|mixed|null|object|\stdClass
51
     * @throws Exception
52
     * @throws InvalidValue
53
     * @throws \Exception
54
     */
55 3107
    public function in($data, Context $options = null)
56
    {
57 3107
        return $this->schema->in($data, $options);
58
    }
59
60
    /**
61
     * @param mixed $data
62
     * @param Context|null $options
63
     * @return array|mixed|null|object|\stdClass
64
     * @throws InvalidValue
65
     * @throws \Exception
66
     */
67 29
    public function out($data, Context $options = null)
68
    {
69 29
        return $this->schema->out($data, $options);
70
    }
71
72
    /**
73
     * @return string[]
74
     */
75 31
    public function getPropertyNames()
76
    {
77 31
        return array_keys($this->schema->getProperties()->toArray());
78
    }
79
80
    /**
81
     * @return string[]
82
     */
83 31
    public function getNestedPropertyNames()
84
    {
85 31
        return $this->schema->getProperties()->nestedPropertyNames;
0 ignored issues
show
Bug Best Practice introduced by
The property nestedPropertyNames does not exist on Swaggest\JsonSchema\Schema. Since you implemented __get, consider adding a @property annotation.
Loading history...
86
    }
87
88 4
    public function nested()
89
    {
90 4
        return new Nested($this);
91
    }
92
93
    /**
94
     * @return null|Constraint\Properties|Schema|Schema[]
95
     */
96 6
    public function getProperties()
97
    {
98 6
        return $this->schema->properties;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->schema->properties also could return the type Swaggest\JsonSchema\Schema which is incompatible with the return type mandated by Swaggest\JsonSchema\Sche...ntract::getProperties() of null|Swaggest\JsonSchema...a\Constraint\Properties.
Loading history...
99
    }
100
101
    /**
102
     * @param string $name
103
     * @return null|Schema|SchemaContract
104
     */
105 4
    public function getProperty($name)
106
    {
107 4
        return $this->schema->properties[$name];
108
    }
109
110
    /**
111
     * @param string $name
112
     * @param array $arguments
113
     * @return mixed
114
     * @throws Exception
115
     */
116
    public function __call($name, $arguments)
117
    {
118
        if (substr($name, 0, 3) === 'set') {
119
            if (!$this->cloned) {
120
                $this->schema = clone $this->schema;
121
                $this->cloned = true;
122
            }
123
            $this->schema->$name($arguments[0]); // todo performance check direct set
124
            return $this;
125
        } else {
126
            throw new Exception('Unknown method:' . $name);
127
        }
128
    }
129
130 7
    public function getDefault()
131
    {
132 7
        return $this->schema->default;
133
    }
134
135
    /**
136
     * @param mixed $default
137
     * @return $this
138
     */
139 1
    public function setDefault($default)
140
    {
141 1
        if (!$this->cloned) {
142 1
            $this->schema = clone $this->schema;
143 1
            $this->cloned = true;
144
        }
145
146 1
        $this->schema->default = $default;
147 1
        return $this;
148
    }
149
150
    /**
151
     * @param string $name
152
     * @throws Exception
153
     */
154
    public function __get($name)
155
    {
156
        throw new Exception('Unexpected get: ' . $name);
157
    }
158
159
    /**
160
     * @param string $name
161
     * @param mixed $value
162
     * @return Wrapper
163
     */
164
    public function __set($name, $value)
165
    {
166
        if (!$this->cloned) {
167
            $this->schema = clone $this->schema;
168
            $this->cloned = true;
169
        }
170
        $this->schema->$name = $value;
171
        return $this;
172
    }
173
174
    /**
175
     * @param string $name
176
     * @throws Exception
177
     */
178
    public function __isset($name)
179
    {
180
        throw new Exception('Unexpected isset: ' . $name);
181
    }
182
183
    public function addMeta(Meta $meta)
184
    {
185
        $this->originalSchema->addMeta($meta);
186
        return $this;
187
    }
188
189 1
    public function getMeta($name)
190
    {
191 1
        return $this->originalSchema->getMeta($name);
192
    }
193
194
    /**
195
     * @param Context|null $options
196
     * @return Structure\ObjectItemContract
197
     */
198 5
    public function makeObjectItem(Context $options = null)
199
    {
200 5
        return $this->schema->makeObjectItem($options);
201
    }
202
203 5
    public function getObjectItemClass()
204
    {
205 5
        return $this->objectItemClass;
206
    }
207
}