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

src/Encoder/UrlEncodedTypeEncoder.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
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