Completed
Push — master ( 773920...14fe81 )
by Guillem
02:29
created

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