QueryParamsNormalizer   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 17
eloc 44
dl 0
loc 71
ccs 42
cts 42
cp 1
rs 10
c 1
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
D normalize() 0 61 17
1
<?php
2
namespace ElevenLabs\Api\Normalizer;
3
4
class QueryParamsNormalizer
5
{
6
    /**
7
     * Normalize parameters
8
     *
9
     * @param array $queryParams An array of query parameters
10
     * @param \stdClass $queryParamsSchema A JSON Schema of query params
11
     *
12
     * @return array An array of query parameters with the proper types
13
     */
14
    public static function normalize(array $queryParams, \stdClass $queryParamsSchema)
15 13
    {
16
        foreach ($queryParamsSchema->properties as $name => $queryParamSchema) {
17 13
            $type = isset($queryParamSchema->type)
18 13
                ? $queryParamSchema->type
19 13
                : 'string';
20 13
21
            if (array_key_exists($name, $queryParams)) {
22 13
                switch ($type) {
23 13
                    case 'boolean':
24 13
                        if ($queryParams[$name] === 'false') {
25 4
                            $queryParams[$name] = false;
26 1
                        }
27
                        if ($queryParams[$name] === 'true') {
28 4
                            $queryParams[$name] = true;
29 1
                        }
30
                        if (in_array($queryParams[$name], ['0', '1'])) {
31 4
                            $queryParams[$name] = (bool) $queryParams[$name];
32 4
                        }
33
                        break;
34 4
                    case 'integer':
35 9
                        if (is_numeric($queryParams[$name])) {
36 3
                            $queryParams[$name] = (int) $queryParams[$name];
37 1
                        }
38
                        break;
39 3
                    case 'number':
40 6
                        if (is_numeric($queryParams[$name])) {
41 1
                            $queryParams[$name] = (float) $queryParams[$name];
42 1
                        }
43
                        break;
44 1
                }
45
46
                if (isset($queryParamSchema->collectionFormat)) {
47 13
                    switch ($queryParamSchema->collectionFormat) {
48 5
                        case 'csv':
49 5
                            $separator = ',';
50 1
                            break;
51 1
                        case 'ssv':
52 4
                            $separator = ' ';
53 1
                            break;
54 1
                        case 'pipes':
55 3
                            $separator = '|';
56 1
                            break;
57 1
                        case 'tsv':
58 2
                            $separator = "\t";
59 1
                            break;
60 1
                        default:
61
                            throw new \InvalidArgumentException(
62 1
                                sprintf(
63 1
                                    '%s is not a supported query collection format',
64 1
                                    $queryParamSchema->collectionFormat
65 1
                                )
66
                            );
67
                    }
68
69
                    $queryParams[$name] = explode($separator, $queryParams[$name]);
70 12
                }
71
            }
72
        }
73
74
        return $queryParams;
75 12
    }
76
}
77