FormUrlEncoded   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 8
eloc 17
c 3
b 0
f 0
dl 0
loc 47
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A next() 0 3 1
A handle() 0 7 2
A __construct() 0 3 1
A handleSentData() 0 8 2
A getBody() 0 9 2
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
}