Passed
Pull Request — master (#16)
by Woody
02:09
created

Parameters::getPathSchema()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 0
cts 2
cp 0
crap 2
1
<?php
2
namespace ElevenLabs\Api\Definition;
3
4
class Parameters implements \Serializable, \IteratorAggregate
5
{
6
    /**
7
     * @var Parameter[]
8
     */
9
    private $parameters;
10
11 9
    public function __construct(array $parameters)
12
    {
13 9
        $this->parameters = [];
14 9
        foreach ($parameters as $parameter) {
15 3
            $this->addParameter($parameter);
16
        }
17 8
    }
18
19 1
    public function getIterator()
20
    {
21 1
        return new \ArrayIterator($this->parameters);
22
    }
23
24
    public function hasBodySchema()
25
    {
26
        $body = $this->getBody();
27
28
        return ($body !== null && $body->hasSchema());
29
    }
30
31
    /**
32
     * JSON Schema for the body.
33
     *
34
     * @return \stdClass|null
35
     */
36
    public function getBodySchema()
37
    {
38
        return $this->getBody()->getSchema();
39
    }
40
41
    /**
42
     * @return bool
43
     */
44
    public function hasPathSchema()
45
    {
46
        return $this->getPathSchema() !== null;
47
    }
48
49
    /**
50
     * JSON Schema for the path parameters.
51
     *
52
     * @return \stdClass|null
53
     */
54
    public function getPathSchema()
55
    {
56
        return $this->getSchema($this->getPath());
57
    }
58
59
    /**
60
     * @return bool
61
     */
62
    public function hasQueryParametersSchema()
63
    {
64
        return $this->getQueryParametersSchema() !== null;
65
    }
66
67
    /**
68
     * JSON Schema for the query parameters.
69
     *
70
     * @return \stdClass|null
71
     */
72
    public function getQueryParametersSchema()
73
    {
74
        return $this->getSchema($this->getQuery());
75
    }
76
77
    /**
78
     * @return bool
79
     */
80
    public function hasHeadersSchema()
81
    {
82
        return $this->getHeadersSchema() !== null;
83
    }
84
85
    /**
86
     * JSON Schema for the headers.
87
     *
88
     * @return \stdClass|null
89
     */
90
    public function getHeadersSchema()
91
    {
92
        return $this->getSchema($this->getHeaders());
93
    }
94
95
    /**
96
     * @return Parameter[]
97
     */
98
    public function getPath()
99
    {
100
        return $this->findByLocation('path');
101
    }
102
103
    /**
104
     * @return Parameter[]
105
     */
106
    public function getQuery()
107
    {
108
        return $this->findByLocation('query');
109
    }
110
111
    /**
112
     * @return Parameter[]
113
     */
114
    public function getHeaders()
115
    {
116
        return $this->findByLocation('header');
117
    }
118
119
    /**
120
     * @return Parameter|null
121
     */
122
    public function getBody()
123
    {
124
        $match = $this->findByLocation('body');
125
        if (empty($match)) {
126
            return null;
127
        }
128
129
        return current($match);
130
    }
131
132
    /**
133
     * Get one request parameter by name
134
     *
135
     * @param string $name
136
     * @return Parameter|null
137
     */
138 1
    public function getByName($name)
139
    {
140 1
        if (! isset($this->parameters[$name])) {
141
            return null;
142
        }
143
144 1
        return $this->parameters[$name];
145
    }
146
147
    /**
148
     * @param Parameter[] $parameters
149
     *
150
     * @return \stdClass|null
151
     */
152
    private function getSchema(array $parameters)
153
    {
154
        if (empty($parameters)) {
155
            return null;
156
        }
157
158
        $schema = new \stdClass();
159
        $schema->type = 'object';
160
        $schema->required = [];
161
        $schema->properties = new \stdClass();
162
        foreach ($parameters as $name => $parameter) {
163
            if ($parameter->isRequired()) {
164
                $schema->required[] = $parameter->getName();
165
            }
166
            $schema->properties->{$name} = $parameter->getSchema();
167
        }
168
169
        return $schema;
170
    }
171
172 3
    public function serialize()
173
    {
174 3
        return serialize(['parameters' => $this->parameters]);
175
    }
176
177 3
    public function unserialize($serialized)
178
    {
179 3
        $data = unserialize($serialized);
180 3
        $this->parameters = $data['parameters'];
181 3
    }
182
183
    private function findByLocation($location)
184
    {
185
        return array_filter(
186
            $this->parameters,
187
            function (Parameter $parameter) use ($location) {
188
                return $parameter->getLocation() === $location;
189
            }
190
        );
191
    }
192
193 3
    private function addParameter(Parameter $parameter)
194
    {
195 3
        if (!in_array($parameter->getLocation(), Parameter::LOCATIONS, true)) {
196 1
            throw new \InvalidArgumentException(
197 1
                sprintf(
198 1
                    '%s is not a supported parameter location, supported: %s',
199 1
                    $parameter->getLocation(),
200 1
                    implode(', ', Parameter::LOCATIONS)
201
                )
202
            );
203
        }
204
205 2
        $this->parameters[$parameter->getName()] = $parameter;
206 2
    }
207
}
208