Completed
Pull Request — master (#20)
by Woody
02:36 queued 41s
created

Parameters::getSchema()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4

Importance

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