Passed
Push — master ( f4a0a2...9fb418 )
by Dominik
02:30
created

UrlEncodedTypeEncoder::getValueAsString()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 25

Duplication

Lines 25
Ratio 100 %

Code Coverage

Tests 13
CRAP Score 7

Importance

Changes 0
Metric Value
dl 25
loc 25
ccs 13
cts 13
cp 1
rs 8.5866
c 0
b 0
f 0
cc 7
nc 7
nop 1
crap 7
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\Serialization\Encoder;
6
7
final class UrlEncodedTypeEncoder implements TypeEncoderInterface
8
{
9
    /**
10
     * @return string
11
     */
12 1
    public function getContentType(): string
13
    {
14 1
        return 'application/x-www-form-urlencoded';
15
    }
16
17
    /**
18
     * @param array $data
19
     *
20
     * @return string
21
     */
22 2
    public function encode(array $data): string
23
    {
24 2
        return $this->buildQuery($data);
25
    }
26
27
    /**
28
     * @param array  $data
29
     * @param string $path
30
     *
31
     * @return string
32
     */
33 2
    private function buildQuery(array $data, string $path = ''): string
34
    {
35 2
        if ([] === $data) {
36 1
            return '';
37
        }
38
39 2
        $query = '';
40 2
        foreach ($data as $key => $value) {
41 2
            if (null === $value) {
42 1
                continue;
43
            }
44
45 2
            $subPath = '' !== $path ? $path.'['.$key.']' : (string) $key;
46 2
            if (is_array($value)) {
47 1
                $query .= $this->buildQuery($value, $subPath);
48
            } else {
49 2
                $query .= $subPath.'='.urlencode($this->getValueAsString($value));
50
            }
51 1
            $query .= '&';
52
        }
53
54 1
        $query = substr($query, 0, -strlen('&'));
55
56 1
        return $query;
57
    }
58
59
    /**
60
     * @param bool|int|float|string $value
61
     *
62
     * @return string
63
     *
64
     * @throws \InvalidArgumentException
65
     */
66 2 View Code Duplication
    private function getValueAsString($value): string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
67
    {
68 2
        if (is_string($value)) {
69 1
            return $value;
70
        }
71
72 2
        if (is_bool($value)) {
73 1
            return $value ? 'true' : 'false';
74
        }
75
76 2
        if (is_float($value)) {
77 1
            $value = (string) $value;
78 1
            if (false === strpos($value, '.')) {
79 1
                $value .= '.0';
80
            }
81
82 1
            return $value;
83
        }
84
85 2
        if (is_int($value)) {
86 1
            return (string) $value;
87
        }
88
89 1
        throw new \InvalidArgumentException(sprintf('Unsupported data type: %s', gettype($value)));
90
    }
91
}
92