Parameter::getFirst()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
namespace Redaxscript\Router;
3
4
use Redaxscript\Filter;
5
use Redaxscript\Request;
6
use Redaxscript\Server;
7
use function array_filter;
8
use function array_key_exists;
9
use function array_map;
10
use function array_reverse;
11
use function explode;
12
use function is_array;
13
use function is_numeric;
14
15
/**
16
 * parent class to get the parameter
17
 *
18
 * @since 2.4.0
19
 *
20
 * @package Redaxscript
21
 * @category Router
22
 * @author Henry Ruhs
23
 */
24
25
class Parameter
26
{
27
	/**
28
	 * instance of the request class
29
	 *
30
	 * @var Request
31
	 */
32
33
	protected $_request;
34
35
	/**
36
	 * array of the parameter
37
	 *
38
	 * @var array
39
	 */
40
41
	protected $_parameterArray = [];
42
43
	/**
44
	 * constructor of the class
45
	 *
46
	 * @since 2.4.0
47
	 *
48
	 * @param Request $request instance of the request class
49
	 */
50
51 160
	public function __construct(Request $request)
52
	{
53 160
		$this->_request = $request;
54 160
	}
55
56
	/**
57
	 * init the class
58
	 *
59
	 * @since 2.4.0
60
	 */
61
62 160
	public function init() : void
63
	{
64 160
		$parameter = $this->_request->getQuery('p');
65 160
		$this->_parameterArray = array_filter(explode('/', $parameter), 'strlen');
66 160
		if (is_array($this->_parameterArray))
67
		{
68 160
			$aliasFilter = new Filter\Alias;
69 160
			$this->_parameterArray = array_map(
70
			[
71 160
				$aliasFilter,
72 160
				'sanitize'
73 160
			], $this->_parameterArray);
74
		}
75 160
	}
76
77
	/**
78
	 * get the first parameter
79
	 *
80
	 * @since 3.1.0
81
	 *
82
	 * @return string|null
83
	 */
84
85 80
	public function getFirst() : ?string
86
	{
87 80
		return $this->_getParameter(0);
88
	}
89
90
	/**
91
	 * get the first sub parameter
92
	 *
93
	 * @since 3.1.0
94
	 *
95
	 * @return int|null
96
	 */
97
98 16
	public function getFirstSub() : ?int
99
	{
100 16
		return $this->_getParameterSub(1);
101
	}
102
103
	/**
104
	 * get the second parameter
105
	 *
106
	 * @since 3.1.0
107
	 *
108
	 * @return string|null
109
	 */
110
111 40
	public function getSecond() : ?string
112
	{
113 40
		return $this->_getParameter(1);
114
	}
115
116
	/**
117
	 * get the second sub parameter
118
	 *
119
	 * @since 3.1.0
120
	 *
121
	 * @return int|null
122
	 */
123
124 16
	public function getSecondSub() : ?int
125
	{
126 16
		return $this->_getParameterSub(2);
127
	}
128
129
	/**
130
	 * get the third parameter
131
	 *
132
	 * @since 3.1.0
133
	 *
134
	 * @return string|null
135
	 */
136
137 26
	public function getThird() : ?string
138
	{
139 26
		return $this->_getParameter(2);
140
	}
141
142
	/**
143
	 * get the third sub parameter
144
	 *
145
	 * @since 3.1.0
146
	 *
147
	 * @return int|null
148
	 */
149
150 21
	public function getThirdSub() : ?int
151
	{
152 21
		return $this->_getParameterSub(3);
153
	}
154
155
	/**
156
	 * get the fourth parameter
157
	 *
158
	 * @since 3.1.0
159
	 *
160
	 * @return string|null
161
	 */
162
163 21
	public function getFourth() : ?string
164
	{
165 21
		return $this->_getParameter(3);
166
	}
167
168
	/**
169
	 * get the fourth sub parameter
170
	 *
171
	 * @since 3.1.0
172
	 *
173
	 * @return int|null
174
	 */
175
176 16
	public function getFourthSub() : ?int
177
	{
178 16
		return $this->_getParameterSub(4);
179
	}
180
181
	/**
182
	 * get the last parameter
183
	 *
184
	 * @since 2.4.0
185
	 *
186
	 * @return string|null
187
	 */
188
189 32
	public function getLast() : ?string
190
	{
191 32
		foreach (array_reverse($this->_parameterArray) as $value)
192
		{
193 30
			if (!is_numeric($value))
194
			{
195 28
				return $value;
196
			}
197
		}
198 4
		return null;
199
	}
200
201
	/**
202
	 * get the last sub parameter
203
	 *
204
	 * @since 3.1.0
205
	 *
206
	 * @return int|null
207
	 */
208
209 16
	public function getLastSub() : ?int
210
	{
211 16
		foreach (array_reverse($this->_parameterArray) as $value)
212
		{
213 15
			if (is_numeric($value))
214
			{
215 7
				return $value;
216
			}
217
		}
218 9
		return null;
219
	}
220
221
	/**
222
	 * get the admin parameter
223
	 *
224
	 * @since 2.4.0
225
	 *
226
	 * @return string|null
227
	 */
228
229 64
	public function getAdmin() : ?string
230
	{
231 64
		if ($this->getFirst() === 'admin' && $this->getSecond())
0 ignored issues
show
Bug Best Practice introduced by redaxmedia
The expression $this->getSecond() of type string|null is loosely compared to true; 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...
232
		{
233 20
			return $this->getSecond();
234
		}
235 44
		return null;
236
	}
237
238
	/**
239
	 * get the table parameter
240
	 *
241
	 * @since 2.4.0
242
	 *
243
	 * @return string|null
244
	 */
245
246 32
	public function getTable() : ?string
247
	{
248 32
		if ($this->getAdmin() && $this->getThird())
0 ignored issues
show
Bug Best Practice introduced by redaxmedia
The expression $this->getAdmin() of type string|null is loosely compared to true; 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...
Bug Best Practice introduced by redaxmedia
The expression $this->getThird() of type string|null is loosely compared to true; 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...
249
		{
250 10
			return $this->getThird();
251
		}
252 22
		return null;
253
	}
254
255
	/**
256
	 * get the alias parameter
257
	 *
258
	 * @since 2.4.0
259
	 *
260
	 * @return string|null
261
	 */
262
263 16
	public function getAlias() : ?string
264
	{
265 16
		if ($this->getAdmin() && $this->getFourth())
0 ignored issues
show
Bug Best Practice introduced by redaxmedia
The expression $this->getAdmin() of type string|null is loosely compared to true; 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...
Bug Best Practice introduced by redaxmedia
The expression $this->getFourth() of type string|null is loosely compared to true; 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...
266
		{
267 1
			return $this->getFourth();
268
		}
269 15
		return null;
270
	}
271
272
	/**
273
	 * get the id parameter
274
	 *
275
	 * @since 2.4.0
276
	 *
277
	 * @return int|null
278
	 */
279
280 16
	public function getId() : ?int
281
	{
282 16
		if ($this->getTable() && $this->getThirdSub())
0 ignored issues
show
Bug Best Practice introduced by redaxmedia
The expression $this->getTable() of type string|null is loosely compared to true; 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...
Bug Best Practice introduced by redaxmedia
The expression $this->getThirdSub() of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. 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 integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
283
		{
284 2
			return $this->getThirdSub();
285
		}
286 14
		return null;
287
	}
288
289
	/**
290
	 * get the token parameter
291
	 *
292
	 * @since 2.4.0
293
	 *
294
	 * @return string|null
295
	 */
296
297 16
	public function getToken() : ?string
298
	{
299 16
		$token = new Server\Token($this->_request);
300 16
		if ($this->getLast() === $token->getOutput())
301
		{
302 2
			return $this->getLast();
303
		}
304 14
		return null;
305
	}
306
307
	/**
308
	 * get the parameter by key
309
	 *
310
	 * @since 3.1.0
311
	 *
312
	 * @param int $key
313
	 *
314
	 * @return string|null
315
	 */
316
317 128
	protected function _getParameter(int $key = null) : ?string
318
	{
319 128
		if (is_array($this->_parameterArray) && array_key_exists($key, $this->_parameterArray) && !is_numeric($this->_parameterArray[$key]))
320
		{
321 93
			return $this->_parameterArray[$key];
322
		}
323 43
		return null;
324
	}
325
326
	/**
327
	 * get the parameter sub by key
328
	 *
329
	 * @since 3.1.0
330
	 *
331
	 * @param int $key
332
	 *
333
	 * @return int|null
334
	 */
335
336 69
	protected function _getParameterSub(int $key = null) : ?int
337
	{
338 69
		if (is_array($this->_parameterArray) && array_key_exists($key, $this->_parameterArray) && is_numeric($this->_parameterArray[$key]))
339
		{
340 8
			return $this->_parameterArray[$key];
341
		}
342 61
		return null;
343
	}
344
}
345