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

Parameters   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 177
Duplicated Lines 0 %

Test Coverage

Coverage 29.23%

Importance

Changes 0
Metric Value
wmc 28
eloc 44
dl 0
loc 177
ccs 19
cts 65
cp 0.2923
rs 10
c 0
b 0
f 0

20 Methods

Rating   Name   Duplication   Size   Complexity  
A serialize() 0 3 1
A hasPathSchema() 0 3 1
A addParameter() 0 3 1
A unserialize() 0 4 1
A __construct() 0 4 2
A getIterator() 0 4 2
A getHeadersSchema() 0 3 1
A getQueryParametersSchema() 0 3 1
A hasBodySchema() 0 5 2
A getQuery() 0 3 1
A hasQueryParametersSchema() 0 3 1
A getPathSchema() 0 3 1
A getBodySchema() 0 3 1
A findByLocation() 0 6 1
A getPath() 0 3 1
A getHeaders() 0 3 1
A getSchema() 0 18 4
A getBody() 0 8 2
A getByName() 0 7 2
A hasHeadersSchema() 0 3 1
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 8
    public function __construct(array $parameters)
15
    {
16 8
        foreach ($parameters as $parameter) {
17 2
            $this->addParameter($parameter);
18
        }
19 8
    }
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
    public function hasBodySchema(): bool
30
    {
31
        $body = $this->getBody();
32
33
        return ($body !== null && $body->hasSchema());
34
    }
35
36
    /**
37
     * JSON Schema for the body.
38
     */
39
    public function getBodySchema(): ?stdClass
40
    {
41
        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
    public function hasQueryParametersSchema(): bool
58
    {
59
        return $this->getQueryParametersSchema() !== null;
60
    }
61
62
    /**
63
     * JSON Schema for the query parameters.
64
     */
65
    public function getQueryParametersSchema(): ?stdClass
66
    {
67
        return $this->getSchema($this->getQuery());
68
    }
69
70
    public function hasHeadersSchema(): bool
71
    {
72
        return $this->getHeadersSchema() !== null;
73
    }
74
75
    /**
76
     * JSON Schema for the headers.
77
     */
78
    public function getHeadersSchema(): ?stdClass
79
    {
80
        return $this->getSchema($this->getHeaders());
81
    }
82
83
    /**
84
     * @return Parameter[]
85
     */
86
    public function getPath(): array
87
    {
88
        return $this->findByLocation('path');
89
    }
90
91
    /**
92
     * @return Parameter[]
93
     */
94
    public function getQuery(): array
95
    {
96
        return $this->findByLocation('query');
97
    }
98
99
    /**
100
     * @return Parameter[]
101
     */
102
    public function getHeaders(): array
103
    {
104
        return $this->findByLocation('header');
105
    }
106
107
    public function getBody(): ?Parameter
108
    {
109
        $match = $this->findByLocation('body');
110
        if (empty($match)) {
111
            return null;
112
        }
113
114
        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
    private function getSchema(array $parameters): ?stdClass
136
    {
137
        if (empty($parameters)) {
138
            return null;
139
        }
140
141
        $schema = new \stdClass();
142
        $schema->type = 'object';
143
        $schema->required = [];
144
        $schema->properties = new \stdClass();
145
        foreach ($parameters as $name => $parameter) {
146
            if ($parameter->isRequired()) {
147
                $schema->required[] = $parameter->getName();
148
            }
149
            $schema->properties->{$name} = $parameter->getSchema();
150
        }
151
152
        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
    private function findByLocation($location): array
172
    {
173
        return array_filter(
174
            $this->parameters,
175
            function (Parameter $parameter) use ($location) {
176
                return $parameter->getLocation() === $location;
177
            }
178
        );
179
    }
180
181 2
    private function addParameter(Parameter $parameter)
182
    {
183 2
        $this->parameters[$parameter->getName()] = $parameter;
184 2
    }
185
}
186