Passed
Push — master ( 231a7d...688ef3 )
by Alexander
02:46
created

ParamsFormatter::format()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 12
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 23
ccs 13
cts 13
cp 1
crap 4
rs 9.8666
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Swagger\Formatter;
6
7
use Yiisoft\Arrays\ArrayHelper;
8
use Yiisoft\Html\Html;
9
10
final class ParamsFormatter
11
{
12 1
    public function format(array $params): string
13
    {
14 1
        if (ArrayHelper::isAssociative($params)) {
15 1
            $result = [];
16
17 1
            $keys = array_keys($params);
18 1
            foreach ($keys as $key) {
19 1
                $element = Html::encode($key) . ':';
20
21 1
                if (is_array($params[$key])) {
22 1
                    $result[] = $element . $this->format($params[$key]);
23 1
                    continue;
24
                }
25
26 1
                $result[] = $element . '"' . Html::encode((string)$params[$key]) . '"';
27
            }
28
29 1
            return '{' . implode(', ', $result) . '}';
30
        }
31
32 1
        $result = array_map([Html::class, 'encode'], $params);
33
34 1
        return '[' . implode(', ', $result) . ']';
35
    }
36
}
37