|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PlugHttp\Body; |
|
4
|
|
|
|
|
5
|
|
|
use PlugHttp\Globals\Post; |
|
6
|
|
|
use PlugHttp\Globals\Server; |
|
7
|
|
|
use PlugHttp\Utils\ArrayUtil; |
|
8
|
|
|
|
|
9
|
|
|
class Content |
|
10
|
|
|
{ |
|
11
|
|
|
private Server $server; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @var string|array |
|
15
|
|
|
*/ |
|
16
|
|
|
private $requestBody; |
|
17
|
|
|
|
|
18
|
|
|
public function __construct(Server $server) |
|
19
|
|
|
{ |
|
20
|
|
|
$this->server = $server; |
|
21
|
|
|
$this->getBodyRequest(); |
|
22
|
|
|
$this->createProperty(); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
private function getBodyRequest() |
|
26
|
|
|
{ |
|
27
|
|
|
$xml = new XML(); |
|
28
|
|
|
$json = new Json(); |
|
29
|
|
|
$post = new Post(); |
|
30
|
|
|
$formData = new FormData(); |
|
31
|
|
|
$urlEncode = new FormUrlEncoded(); |
|
32
|
|
|
$textPlain = new TextPlain(); |
|
33
|
|
|
|
|
34
|
|
|
$json->next($formData); |
|
35
|
|
|
$formData->next($urlEncode); |
|
36
|
|
|
$urlEncode->next($textPlain); |
|
37
|
|
|
$textPlain->next($xml); |
|
38
|
|
|
$xml->next($post); |
|
39
|
|
|
|
|
40
|
|
|
$this->requestBody = $json->handle($this->server); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
private function createProperty() |
|
44
|
|
|
{ |
|
45
|
|
|
if (is_array($this->requestBody)) { |
|
46
|
|
|
foreach ($this->requestBody as $key => $value) { |
|
47
|
|
|
$this->$key = $value; |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function add(string $key, $value): void |
|
53
|
|
|
{ |
|
54
|
|
|
$this->requestBody[$key] = $value; |
|
55
|
|
|
|
|
56
|
|
|
$this->__set($key, $value); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function remove(string $key): void |
|
60
|
|
|
{ |
|
61
|
|
|
unset($this->requestBody[$key]); |
|
62
|
|
|
|
|
63
|
|
|
unset($this->$key); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function get(string $key) |
|
67
|
|
|
{ |
|
68
|
|
|
return $this->requestBody[$key]; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @return array|string |
|
73
|
|
|
*/ |
|
74
|
|
|
public function all() |
|
75
|
|
|
{ |
|
76
|
|
|
return $this->requestBody; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function __set($name, $value) |
|
80
|
|
|
{ |
|
81
|
|
|
$this->$name = $value; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function __get($name) |
|
85
|
|
|
{ |
|
86
|
|
|
$this->$name; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function except(array $keys): array |
|
90
|
|
|
{ |
|
91
|
|
|
return ArrayUtil::except($this->requestBody, $keys); |
|
|
|
|
|
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public function only(array $keys): array |
|
95
|
|
|
{ |
|
96
|
|
|
return ArrayUtil::only($this->requestBody, $keys); |
|
|
|
|
|
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
public function has(string $key): bool |
|
100
|
|
|
{ |
|
101
|
|
|
return !empty($this->requestBody[$key]); |
|
102
|
|
|
} |
|
103
|
|
|
} |