RouteRegex::unsetNumericKeys()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3
1
<?php
2
/**
3
 * RegEx routing processing
4
 *
5
 * @file      RouteRegex.php
6
 *
7
 * PHP version 8.0+
8
 *
9
 * @author    Alexander Yancharuk <alex at itvault dot info>
10
 * @copyright © 2012-2021 Alexander Yancharuk
11
 * @date      Сбт Июн 23 10:47:39 2012
12
 * @license   The BSD 3-Clause License
13
 *            <https://tldrlegal.com/license/bsd-3-clause-license-(revised)>
14
 */
15
16
namespace Veles\Routing;
17
18
/**
19
 * Class RouteRegex
20
 * @author  Alexander Yancharuk <alex at itvault dot info>
21
 */
22
class RouteRegex implements RouteStrategyInterface
23
{
24
	/**
25
	 * Variable contains array of named sub-patterns values
26
	 *
27
	 * @var array|null
28
	 */
29
	protected static $params;
30
31
	/**
32
	 * Check if given url matches route pattern
33
	 *
34
	 * @param string $pattern Pattern from route config
35
	 * @param string $url     current $_SERVER['REQUEST_URI'] without params
36
	 *
37
	 * @return bool
38
	 */
39 16
	public static function check($pattern, $url)
40
	{
41 16
		self::$params = null;
42
43 16
		if (1 === ($result = preg_match($pattern, $url, $matches))) {
44 13
			self::unsetNumericKeys($matches);
45
46 13
			self::$params = $matches;
47
		}
48
49 16
		return (bool) $result;
50
	}
51
52
	/**
53
	 * Get url maps
54
	 *
55
	 * @return mixed
56
	 */
57 11
	public static function getParams()
58
	{
59 11
		return self::$params;
60
	}
61
62 13
	protected static function unsetNumericKeys(&$matches)
63
	{
64 13
		foreach (array_keys($matches) as $key) {
65 13
			if (is_int($key)) {
66 13
				unset($matches[$key]);
67
			}
68
		}
69
	}
70
}
71