Passed
Push — master ( 934f11...8ba0cf )
by Jean-Christophe
10:32
created

RouterTestTrait::addTestRoute()   A

Complexity

Conditions 6
Paths 5

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 6.972

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 13
ccs 7
cts 10
cp 0.7
rs 9.2222
cc 6
nc 5
nop 3
crap 6.972
1
<?php
2
3
namespace Ubiquity\controllers\traits;
4
5
use Ubiquity\utils\base\UString;
6
7
/**
8
 * Ubiquity\controllers\traits$RouterTestTrait
9
 * This class is part of Ubiquity
10
 *
11
 * @author jcheron <[email protected]>
12
 * @version 1.0.1
13
 *
14
 * @property array $routes
15
 *
16
 */
17
trait RouterTestTrait {
18
19 1
	public static function testRoutes($path, $method = null): array {
20 1
		$response = [ ];
21 1
		if (isset ( $path )) {
22 1
			$path = self::slashPath ( $path );
23 1
			if (isset ( self::$routes [$path] )) {
24
				self::addTestRoute ( $response, $path, $method );
25
			}
26
		}
27 1
		foreach ( self::$routes as $routePath => $routeDetails ) {
28 1
			if (\preg_match ( "@^{$routePath}\$@s", $path ) || $path == null) {
29 1
				self::addTestRoute ( $response, $routePath, $method );
30
			}
31
		}
32 1
		return $response;
33
	}
34
35 1
	private static function addTestRoute(&$response, $path, $method = null): void {
36 1
		if (isset ( $method )) {
37 1
			$restrict = false;
38 1
			if (UString::startswith ( $method, '-' )) {
39
				$restrict = true;
40
				$method = \ltrim ( $method, '-' );
41
			}
42 1
			$routeMethod = self::getMethod ( self::$routes [$path] );
43 1
			if ((sizeof ( $routeMethod ) == 0 && ! $restrict) || \array_search ( \strtolower ( $method ), $routeMethod ) !== false) {
44 1
				$response [$path] = self::$routes [$path];
45
			}
46
		} else {
47
			$response [$path] = self::$routes [$path];
48
		}
49 1
	}
50
51 1
	private static function getMethod($routeDetails): array {
52 1
		if (! isset ( $routeDetails ['controller'] )) {
53
			return \array_keys ( $routeDetails );
54
		}
55 1
		return [ ];
56
	}
57
}
58
59