Test Failed
Push — master ( b2d464...e1b9bb )
by Erandir
02:06
created

FormUrlEncoded::handle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 3
c 2
b 0
f 0
dl 0
loc 7
rs 10
cc 2
nc 2
nop 1
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
	public function __construct()
14
    {
15
        $this->data = [];
16
    }
17
18
    public function getBody($content): array
19
	{
20
	    if (is_array($content)) {
21
	        return $content;
22
        }
23
24
	    $this->handleSentData($content);
25
26
	    return $this->data;
27
	}
28
29
    private function handleSentData(string $content): void
30
    {
31
        $body = explode('&', $content);
32
33
        foreach ($body as $parameter) {
34
            $parameterParsed = explode('=', $parameter);
35
36
            $this->data[$parameterParsed[0]] = str_replace("%0A", "\n", $parameterParsed[1]);
37
        }
38
	}
39
40
	public function next(Handler $handler)
41
	{
42
		$this->handler = $handler;
43
	}
44
45
	public function handle($server): array
46
	{
47
		if (ContentHelper::contentIs($server, 'x-www-form-urlencoded')) {
48
			return $this->getBody($server->getContent());
49
		}
50
51
		return $this->handler->handle($server);
52
	}
53
}