Test Failed
Push — master ( 7df265...08d2f1 )
by Jean-Christophe
06:27
created

RouterAdminTrait::getRoutesPathByController()   A

Complexity

Conditions 6
Paths 4

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 10.5

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 16
ccs 5
cts 10
cp 0.5
rs 9.2222
c 0
b 0
f 0
cc 6
nc 4
nop 1
crap 10.5
1
<?php
2
3
namespace Ubiquity\controllers\traits;
4
5
/**
6
 * Trait for admin part of Router class.
7
 * Ubiquity\controllers\traits$RouterAdminTrait
8
 * This class is part of Ubiquity
9
 *
10
 * @author jcheron <[email protected]>
11
 * @version 1.0.2
12
 * @property array $routes
13
 */
14
trait RouterAdminTrait {
15 1
16 1
	abstract public static function slashPath($path);
17 1
18 1
	public static function getRouteInfoByControllerAction($controller, $action) {
19
		foreach ( self::$routes as $routePath => $routeDetails ) {
20 1
			if (! isset ( $routeDetails ["controller"] )) {
21
				$routeDetails = \current ( $routeDetails );
22
			}
23
			if ($controller === $routeDetails ["controller"] && $action === $routeDetails ["action"]) {
24
				$routeDetails ["path"] = $routePath;
25 1
				return $routeDetails;
26
			}
27
		}
28
		return false;
29
	}
30
31
	public static function getRoutesPathByController($controller) {
32
		$result = [ ];
33
		foreach ( self::$routes as $routePath => $routeDetails ) {
34
			if (! isset ( $routeDetails ['controller'] )) {
35
				foreach ( $routeDetails as $method => $routeInfo ) {
36
					if ($routeInfo ['controller'] === $controller) {
37
						$result [] = [ 'method' => $method,self::getRoutePathInfos ( $controller, $routePath, $routeInfo ) ];
38
					}
39
				}
40
			} else {
41 1
				if ($routeDetails ['controller'] === $controller) {
42 1
					$result [] = [ 'method' => '*',self::getRoutePathInfos ( $controller, $routePath, $routeDetails ) ];
43 1
				}
44 1
			}
45 1
		}
46
		return $result;
47 1
	}
48 1
49
	public static function getRoutePathInfos($controller, $routePath, $routeInfo) {
50 1
		$method = new \ReflectionMethod (  $controller,$routeInfo ['action'] );
51
		$parameters = $method->getParameters ();
52
		$routeParams = $routeInfo ['parameters'];
53
		$pattern = "@\(.*?\)@";
54
		$params = [ ];
55
		foreach ( $routeParams as $param ) {
56
			if ($param === '*') {
57
				$params [] = $parameters [sizeof ( $params )]->getName();
58
			} else {
59
				$index = ( int ) filter_var ( $param, FILTER_SANITIZE_NUMBER_INT );
60
				$params [] = $parameters [$index]->getName();
61
			}
62
		}
63
		$path = $routePath;
64
		foreach ( $params as $param ) {
65
			$path = preg_replace ( $pattern, '{'.$param.'}', $path, 1 );
66
		}
67
		return $path;
68
	}
69
70
	public static function getRouteInfo($path) {
71
		$path = self::slashPath ( $path );
72
		foreach ( self::$routes as $routePath => $routeDetails ) {
73
			if (preg_match ( "@^" . $routePath . "$@s", $path, $matches ) || \stripslashes ( $routePath ) == $path) {
74
				if (! isset ( $routeDetails ["controller"] )) {
75
					return \current ( $routeDetails );
76
				} else
77
					return $routeDetails;
78
			}
79
		}
80
		return false;
81
	}
82
83
	public static function getAnnotations($controllerName, $actionName) {
84
		$result = [ ];
85
		foreach ( self::$routes as $routePath => $routeDetails ) {
86
			if (! isset ( $routeDetails ["controller"] )) {
87
				$routeDetails = \current ( $routeDetails );
88
			}
89
			if ($routeDetails ["controller"] === $controllerName && $routeDetails ["action"] === $actionName)
90
				$result [$routePath] = $routeDetails;
91
		}
92
		return $result;
93
	}
94
95
	public static function filterRoutes($path) {
96
		$path = self::slashPath ( $path );
97
		$result = [ ];
98
		foreach ( self::$routes as $routePath => $routeDetails ) {
99
			if (preg_match ( "@^" . $routePath . ".*?$@s", $path, $matches )) {
100
				$result [$routePath] = $routeDetails;
101
			}
102
		}
103
		return $result;
104
	}
105
}
106
107