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

includes/Request.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
	 * @param string $index index of the array
53
	 *
54
	 * @return string|array|null
55
	 */
56
57 9
	public function get(string $key = null, string $index = null)
58
	{
59
		/* handle index */
60
61 9
		if (is_array(self::$_requestArray) && array_key_exists($index, self::$_requestArray))
62
		{
63 6
			$requestArray = self::$_requestArray[$index];
64
		}
65
		else
66
		{
67 3
			$requestArray = self::$_requestArray;
68
		}
69
70
		/* values as needed */
71
72 9
		if (is_array($requestArray) && array_key_exists($key, $requestArray))
73
		{
74 7
			return $requestArray[$key];
75
		}
76 2
		if (!$key)
0 ignored issues
show
Bug Best Practice introduced by redaxmedia
The expression $key of type null|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
77
		{
78 1
			return $requestArray;
79
		}
80 1
		return null;
81
	}
82
83
	/**
84
	 * get the value from server
85
	 *
86
	 * @since 2.2.0
87
	 *
88
	 * @param string $key key of the item
89
	 *
90
	 * @return string|array|null
91
	 */
92
93 1
	public function getServer(string $key = null)
94
	{
95 1
		return $this->get($key, 'server');
96
	}
97
98
	/**
99
	 * get the value from query
100
	 *
101
	 * @since 2.2.0
102
	 *
103
	 * @param string $key key of the item
104
	 *
105
	 * @return string|array|null
106
	 */
107
108 1
	public function getQuery(string $key = null)
109
	{
110 1
		return $this->get($key, 'get');
111
	}
112
113
	/**
114
	 * get the value from post
115
	 *
116
	 * @since 2.2.0
117
	 *
118
	 * @param string $key key of the item
119
	 *
120
	 * @return string|array|null
121
	 */
122
123 1
	public function getPost(string $key = null)
124
	{
125 1
		return $this->get($key, 'post');
126
	}
127
128
	/**
129
	 * get the value from files
130
	 *
131
	 * @since 3.0.0
132
	 *
133
	 * @param string $key key of the item
134
	 *
135
	 * @return string|array|null
136
	 */
137
138 1
	public function getFiles(string $key = null)
139
	{
140 1
		return $this->get($key, 'files');
141
	}
142
143
	/**
144
	 * get the value from session
145
	 *
146
	 * @since 2.2.0
147
	 *
148
	 * @param string $key key of the item
149
	 *
150
	 * @return string|array|null
151
	 */
152
153 1
	public function getSession(string $key = null)
154
	{
155 1
		return $this->get($key, 'session');
156
	}
157
158
	/**
159
	 * get the value from cookie
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 getCookie(string $key = null)
169
	{
170 1
		return $this->get($key, 'cookie');
171
	}
172
173
	/**
174
	 * set the value to globals
175
	 *
176
	 * @since 2.2.0
177
	 *
178
	 * @param string $key key of the item
179
	 * @param string|array|null $value value of the item
180
	 */
181
182 1
	public function set(string $key = null, $value = null) : void
183
	{
184 1
		self::$_requestArray[$key] = $GLOBALS[$key] = $value;
185 1
	}
186
187
	/**
188
	 * set the value to server
189
	 *
190
	 * @since 2.2.0
191
	 *
192
	 * @param string $key key of the item
193
	 * @param string|array|null $value value of the item
194
	 */
195
196 1
	public function setServer(string $key = null, $value = null) : void
197
	{
198 1
		self::$_requestArray['server'][$key] = $value;
199 1
	}
200
201
	/**
202
	 * set the value to query
203
	 *
204
	 * @since 2.2.0
205
	 *
206
	 * @param string $key key of the item
207
	 * @param string|array|null $value value of the item
208
	 */
209
210 1
	public function setQuery(string $key = null, $value = null) : void
211
	{
212 1
		self::$_requestArray['get'][$key] = $value;
213 1
	}
214
215
	/**
216
	 * set the value to post
217
	 *
218
	 * @since 2.2.0
219
	 *
220
	 * @param string $key key of the item
221
	 * @param string|array|null $value value of the item
222
	 */
223
224 1
	public function setPost(string $key = null, $value = null) : void
225
	{
226 1
		self::$_requestArray['post'][$key] = $value;
227 1
	}
228
229
	/**
230
	 * set the value to files
231
	 *
232
	 * @since 3.0.0
233
	 *
234
	 * @param string $key key of the item
235
	 * @param string|array|null $value value of the item
236
	 */
237
238 1
	public function setFiles(string $key = null, $value = null) : void
239
	{
240 1
		self::$_requestArray['files'][$key] = $value;
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
	 * set the value to cookie
259
	 *
260
	 * @since 2.2.0
261
	 *
262
	 * @param string $key key of the item
263
	 * @param string|array|null $value value of the item
264
	 */
265
266 1
	public function setCookie(string $key = null, $value = null) : void
267
	{
268 1
		self::$_requestArray['cookie'][$key] = $_COOKIE[$key] = $value;
269 1
	}
270
271
	/**
272
	 * refresh the session
273
	 *
274
	 * @since 2.6.2
275
	 */
276
277 1
	public function refreshSession() : void
278
	{
279 1
		self::$_requestArray['session'] = $_SESSION ? : [];
280 1
	}
281
282
	/**
283
	 * refresh the cookie
284
	 *
285
	 * @since 2.6.2
286
	 */
287
288 1
	public function refreshCookie() : void
289
	{
290 1
		self::$_requestArray['cookie'] = $_COOKIE ? : [];
291 1
	}
292
}
293