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

ParamsFormatter   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 25
ccs 13
cts 13
cp 1
rs 10
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A format() 0 23 4
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