Completed
Pull Request — master (#20)
by Woody
02:29
created

Parameters   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 177
Duplicated Lines 0 %

Test Coverage

Coverage 92.31%

Importance

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

20 Methods

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