QueryMapper::stringify()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2.1481

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 6
ccs 2
cts 3
cp 0.6667
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2.1481
1
<?php
2
3
namespace kalanis\Restful\Mapping;
4
5
6
use kalanis\Restful\Mapping\Exceptions\MappingException;
7
use Traversable;
8
9
10
/**
11
 * Query string mapper
12
 * @package kalanis\Restful\Mapping
13
 */
14 1
class QueryMapper implements IMapper
15
{
16
17
    /**
18
     * Convert array or Traversable input to string output response
19
     * @param string|object|iterable<string|int, mixed> $data
20
     * @param bool $prettyPrint
21
     */
22
    public function stringify(iterable|string|object $data, bool $prettyPrint = true): string
23
    {
24 1
        if ($data instanceof Traversable) {
25
            $data = iterator_to_array($data);
26
        }
27 1
        return http_build_query((array) $data, '', '&');
28
    }
29
30
    /**
31
     * Convert client request data to array or traversable
32
     * @param mixed $data
33
     * @throws MappingException
34
     * @return iterable<string|int, mixed>
35
     *
36
     */
37
    public function parse(mixed $data): iterable
38
    {
39 1
        $result = [];
40 1
        parse_str(strval($data), $result);
41 1
        return $result;
42
    }
43
}
44