FormUrlEncoded::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace PlugHttp\Body;
4
5
use PlugHttp\Utils\ContentHelper;
6
7
class FormUrlEncoded implements Handler, Advancer
8
{
9
	private Handler $handler;
10
11
	private array $data;
12
13
	private const CONTENT_TYPE = 'x-www-form-urlencoded';
14
15
	public function __construct()
16
    {
17
        $this->data = [];
18
    }
19
20
    public function getBody($content): array
21
	{
22
	    if (is_array($content)) {
23
	        return $content;
24
        }
25
26
	    $this->handleSentData($content);
27
28
	    return $this->data;
29
	}
30
31
    private function handleSentData(string $content): void
32
    {
33
        $body = explode('&', $content);
34
35
        foreach ($body as $parameter) {
36
            $parameterParsed = explode('=', $parameter);
37
38
            $this->data[$parameterParsed[0]] = str_replace("%0A", "\n", $parameterParsed[1]);
39
        }
40
	}
41
42
	public function next(Handler $handler)
43
	{
44
		$this->handler = $handler;
45
	}
46
47
	public function handle($server): array
48
	{
49
		if (ContentHelper::contentIs($server, self::CONTENT_TYPE)) {
50
			return $this->getBody($server->getContent());
51
		}
52
53
		return $this->handler->handle($server);
54
	}
55
}