Test Failed
Push — master ( 02520f...f0ea68 )
by Jean-Christophe
06:44
created

RouterTestTrait::addTestRoute()   A

Complexity

Conditions 6
Paths 5

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 13
rs 9.2222
c 0
b 0
f 0
cc 6
nc 5
nop 3
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.0
13
 *
14
 * @property array $routes
15
 *
16
 */
17
trait RouterTestTrait {
18
19
	public static function testRoutes($path, $method = null) {
20
		$response = [ ];
21
		if(isset($path)){
22
				$path = self::slashPath ( $path );
23
			if (isset ( self::$routes [$path] )) {
24
				self::addTestRoute ( $response, $path, $method );
25
			}
26
		}
27
		foreach ( self::$routes as $routePath => $routeDetails ) {
28
			if (preg_match ( "@^" . $routePath . "$@s", $path ) || $path == null) {
29
				self::addTestRoute ( $response, $routePath, $method );
30
			}
31
		}
32
		return $response;
33
	}
34
35
	private static function addTestRoute(&$response, $path, $method = null) {
36
		if (isset ( $method )) {
37
			$restrict = false;
38
			if (UString::startswith ( $method, '-' )) {
39
				$restrict = true;
40
				$method = ltrim ( $method, '-' );
41
			}
42
			$routeMethod = self::getMethod ( self::$routes [$path] );
43
			if ((sizeof($routeMethod)==0 && ! $restrict) || array_search( strtolower ( $method ),$routeMethod)!==false) {
44
				$response [$path] = self::$routes [$path];
45
			}
46
		} else {
47
			$response [$path] = self::$routes [$path];
48
		}
49
	}
50
51
	private static function getMethod($routeDetails) {
52
		if (! isset ( $routeDetails ["controller"] )) {
53
			return array_keys($routeDetails);
54
		}
55
		return [];
56
	}
57
}
58
59