Passed
Pull Request — master (#21)
by Erandir
04:20
created

GlobalRequest::bodyObject()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 11
rs 10
cc 3
nc 3
nop 0
1
<?php
2
3
namespace PlugHttp\Globals;
4
5
use PlugHttp\Utils\ArrayUtil;
6
7
class GlobalRequest
8
{
9
	private $get;
10
11
	private $body;
12
13
	private $file;
14
15
	private $server;
16
17
	public function __construct(
18
		$body,
19
		GlobalGet $get,
20
		GlobalFile $file,
21
		GlobalServer $server
22
	)
23
	{
24
		$this->get 		= $get;
25
		$this->server	= $server;
26
		$this->body 	= $body;
27
		$this->file 	= $file;
28
	}
29
30
	public function all()
31
	{
32
		return $this->body;
33
	}
34
35
	public function bodyObject()
36
	{
37
		if (is_array($this->body)) {
38
            return ArrayUtil::converToObject($this->body);
39
        }
40
41
		if (is_object($this->body)) {
42
		    return $this->body;
43
        }
44
45
		throw new \Exception("It wasn't possible convert to object");
46
	}
47
	
48
	public function input(string $value)
49
	{
50
		return $this->body[$value];
51
	}
52
53
	public function query()
54
	{
55
		return $this->get->all();
56
	}
57
58
	public function queryWith(string $parameter)
59
	{
60
		return $this->get->get($parameter);
61
	}
62
63
	public function queryOnly(array $values)
64
	{
65
		return $this->get->only($values);
66
	}
67
68
	public function queryExcept(array $values)
69
	{
70
		return $this->get->except($values);
71
	}
72
73
	public function redirect(string $path, int $code = 301)
74
	{
75
		header("HTTP/1.0 {$code}");
76
		header("Location: {$path}");
77
78
		return true;
79
	}
80
81
	public function except(array $values)
82
	{
83
		return ArrayUtil::except($this->body, $values);
84
	}
85
86
	public function only(array $values)
87
	{
88
		return ArrayUtil::only($this->body, $values);
89
	}
90
91
	public function has(string $value)
92
	{
93
		return isset($this->body[$value]) ? true : false;
94
	}
95
96
	public function method()
97
	{
98
		return $this->server->method();
99
	}
100
101
	public function getUrl()
102
	{
103
		return $this->server->getUrl();
104
	}
105
106
	public function isMethod(string $method)
107
	{
108
		return $this->method() === strtoupper($method);
109
	}
110
111
	public function remove(string $key)
112
	{
113
		unset($this->body[$key]);
114
115
		return $this;
116
	}
117
118
	public function removeQuery(string $key)
119
	{
120
		$this->get->remove($key);
121
122
		return $this;
123
	}
124
125
	public function add($key, $value)
126
	{
127
		$this->body[$key] = $value;
128
129
		return $this;
130
	}
131
132
	public function addQuery($key, $value)
133
	{
134
		$this->get->add($key, $value);
135
136
		return $this;
137
	}
138
139
	public function files()
140
	{
141
		return $this->file->all();
142
	}
143
144
	public function headers()
145
	{
146
		return $this->server->headers();
147
	}
148
149
	public function header(string $header)
150
	{
151
		return $this->server->header($header);
152
	}
153
}