FormUrlencodedSerializer::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 2
rs 10
1
<?php
2
3
namespace Ipag\Sdk\IO;
4
5
class FormUrlencodedSerializer implements SerializerInterface
6
{
7
    public function __construct()
8
    {
9
    }
10
11
    public function serialize(array $data): string
12
    {
13
        $output = '';
14
15
        foreach ($data as $key => $value) {
16
            if (is_array($value)) {
17
                $key = $key . '[]';
18
19
                foreach ($value as $v) {
20
                    $output .= $key . '=' . urlencode($v) . '&';
21
                }
22
            } else {
23
                $output .= $key . '=' . urlencode($value) . '&';
24
            }
25
        }
26
27
        return rtrim($output, '&');
28
    }
29
30
    public function unserialize(string $data): array
31
    {
32
        $pieces = explode('&', $data);
33
        $output = [];
34
35
        foreach ($pieces as $piece) {
36
            [$key, $value] = explode('=', $piece);
37
            $output[$key] = urldecode($value);
38
        }
39
40
        return $output;
41
    }
42
43
    public function getContentType(): string
44
    {
45
        return 'application/x-www-form-urlencoded';
46
    }
47
}
48