Parameters::findByLocation()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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