Completed
Push — master ( da58d4...61a0f7 )
by Henry
06:34
created

Request::get()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 3
nc 2
nop 1
crap 3
1
<?php
2
namespace Redaxscript;
3
4
use function array_key_exists;
5
use function is_array;
6
7
/**
8
 * children class to request globals
9
 *
10
 * @since 2.2.0
11
 *
12
 * @package Redaxscript
13
 * @category Request
14
 * @author Henry Ruhs
15
 */
16
17
class Request extends Singleton
18
{
19
	/**
20
	 * array of the request
21
	 *
22
	 * @var array
23
	 */
24
25
	protected static $_requestArray = [];
26
27
	/**
28
	 * init the class
29
	 *
30
	 * @since 2.4.0
31
	 */
32
33 1
	public function init() : void
34
	{
35
		self::$_requestArray =
36
		[
37 1
			'server' => $_SERVER ? : [],
38
			'get' => $_GET ? : [],
39
			'post' => $_POST ? : [],
40
			'files' => $_FILES ? : [],
41
			'session' => $_SESSION ? : [],
42
			'cookie' => $_COOKIE ? : []
43
		];
44 1
	}
45
46
	/**
47
	 * get the value from globals
48
	 *
49
	 * @since 3.0.0
50
	 *
51
	 * @param string $key key of the item
52
	 *
53
	 * @return string|array|null
54
	 */
55
56
	public function get(string $key = null)
57 9
	{
58
		if (is_array(self::$_requestArray) && array_key_exists($key, self::$_requestArray))
59
		{
60
			return self::$_requestArray[$key];
61 9
		}
62
		return null;
63 6
	}
64
65
	/**
66
	 * get the array from globals
67 3
	 *
68
	 * @since 4.0.0
69
	 *
70
	 * @return array
71
	 */
72 9
73
	public function getArray() : array
74 7
	{
75
		return self::$_requestArray;
76 2
	}
77
78 1
	/**
79
	 * set the value to globals
80 1
	 *
81
	 * @since 2.2.0
82
	 *
83
	 * @param string $key key of the item
84
	 * @param string|array|null $value value of the item
85
	 */
86
87
	public function set(string $key = null, $value = null) : void
88
	{
89
		self::$_requestArray[$key] = $GLOBALS[$key] = $value;
90
	}
91
92
	/**
93 1
	 * get the value from server
94
	 *
95 1
	 * @since 2.2.0
96
	 *
97
	 * @param string $key key of the item
98
	 *
99
	 * @return string|array|null
100
	 */
101
102
	public function getServer(string $key = null)
103
	{
104
		if (is_array(self::$_requestArray['server']) && array_key_exists($key, self::$_requestArray['server']))
105
		{
106
			return self::$_requestArray['server'][$key];
107
		}
108 1
		return null;
109
	}
110 1
111
	/**
112
	 * set the value to server
113
	 *
114
	 * @since 2.2.0
115
	 *
116
	 * @param string $key key of the item
117
	 * @param string|array|null $value value of the item
118
	 */
119
120
	public function setServer(string $key = null, $value = null) : void
121
	{
122
		self::$_requestArray['server'][$key] = $value;
123 1
	}
124
125 1
	/**
126
	 * get the value from query
127
	 *
128
	 * @since 2.2.0
129
	 *
130
	 * @param string $key key of the item
131
	 *
132
	 * @return string|array|null
133
	 */
134
135
	public function getQuery(string $key = null)
136
	{
137
		if (is_array(self::$_requestArray['get']) && array_key_exists($key, self::$_requestArray['get']))
138 1
		{
139
			return self::$_requestArray['get'][$key];
140 1
		}
141
		return null;
142
	}
143
144
	/**
145
	 * set the value to query
146
	 *
147
	 * @since 2.2.0
148
	 *
149
	 * @param string $key key of the item
150
	 * @param string|array|null $value value of the item
151
	 */
152
153 1
	public function setQuery(string $key = null, $value = null) : void
154
	{
155 1
		self::$_requestArray['get'][$key] = $value;
156
	}
157
158
	/**
159
	 * get the value from post
160
	 *
161
	 * @since 2.2.0
162
	 *
163
	 * @param string $key key of the item
164
	 *
165
	 * @return string|array|null
166
	 */
167
168 1
	public function getPost(string $key = null)
169
	{
170 1
		if (is_array(self::$_requestArray['post']) && array_key_exists($key, self::$_requestArray['post']))
171
		{
172
			return self::$_requestArray['post'][$key];
173
		}
174
		return null;
175
	}
176
177
	/**
178
	 * set the value to post
179
	 *
180
	 * @since 2.2.0
181
	 *
182 1
	 * @param string $key key of the item
183
	 * @param string|array|null $value value of the item
184 1
	 */
185 1
186
	public function setPost(string $key = null, $value = null) : void
187
	{
188
		self::$_requestArray['post'][$key] = $value;
189
	}
190
191
	/**
192
	 * get the value from files
193
	 *
194
	 * @since 3.0.0
195
	 *
196 1
	 * @param string $key key of the item
197
	 *
198 1
	 * @return string|array|null
199 1
	 */
200
201
	public function getFiles(string $key = null)
202
	{
203
		if (is_array(self::$_requestArray['files']) && array_key_exists($key, self::$_requestArray['files']))
204
		{
205
			return self::$_requestArray['files'][$key];
206
		}
207
		return null;
208
	}
209
210 1
	/**
211
	 * set the value to files
212 1
	 *
213 1
	 * @since 3.0.0
214
	 *
215
	 * @param string $key key of the item
216
	 * @param string|array|null $value value of the item
217
	 */
218
219
	public function setFiles(string $key = null, $value = null) : void
220
	{
221
		self::$_requestArray['files'][$key] = $value;
222
	}
223
224 1
	/**
225
	 * get the value from session
226 1
	 *
227 1
	 * @since 2.2.0
228
	 *
229
	 * @param string $key key of the item
230
	 *
231
	 * @return string|array|null
232
	 */
233
234
	public function getSession(string $key = null)
235
	{
236
		if (is_array(self::$_requestArray['session']) && array_key_exists($key, self::$_requestArray['session']))
237
		{
238 1
			return self::$_requestArray['session'][$key];
239
		}
240 1
		return null;
241 1
	}
242
243
	/**
244
	 * set the value to session
245
	 *
246
	 * @since 2.2.0
247
	 *
248
	 * @param string $key key of the item
249
	 * @param string|array|null $value value of the item
250
	 */
251
252 1
	public function setSession(string $key = null, $value = null) : void
253
	{
254 1
		self::$_requestArray['session'][$key] = $_SESSION[$key] = $value;
255 1
	}
256
257
	/**
258
	 * refresh the session
259
	 *
260
	 * @since 2.6.2
261
	 */
262
263
	public function refreshSession() : void
264
	{
265
		self::$_requestArray['session'] = $_SESSION ? : [];
266 1
	}
267
268 1
	/**
269 1
	 * get the value from cookie
270
	 *
271
	 * @since 2.2.0
272
	 *
273
	 * @param string $key key of the item
274
	 *
275
	 * @return string|array|null
276
	 */
277 1
278
	public function getCookie(string $key = null)
279 1
	{
280 1
		if (is_array(self::$_requestArray['cookie']) && array_key_exists($key, self::$_requestArray['cookie']))
281
		{
282
			return self::$_requestArray['cookie'][$key];
283
		}
284
		return null;
285
	}
286
287
	/**
288 1
	 * set the value to cookie
289
	 *
290 1
	 * @since 2.2.0
291 1
	 *
292
	 * @param string $key key of the item
293
	 * @param string|array|null $value value of the item
294
	 */
295
296
	public function setCookie(string $key = null, $value = null) : void
297
	{
298
		self::$_requestArray['cookie'][$key] = $_COOKIE[$key] = $value;
299
	}
300
301
	/**
302
	 * refresh the cookie
303
	 *
304
	 * @since 2.6.2
305
	 */
306
307
	public function refreshCookie() : void
308
	{
309
		self::$_requestArray['cookie'] = $_COOKIE ? : [];
310
	}
311
}
312