Passed
Push — master ( 55710c...55bc39 )
by Jean-Christophe
06:21
created

RouterAdminTrait::getRoutePathInfos()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 19
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 19
ccs 15
cts 15
cp 1
rs 9.7666
c 0
b 0
f 0
cc 4
nc 6
nop 3
crap 4
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
16
	abstract public static function slashPath($path);
17
18 1
	public static function getRouteInfoByControllerAction($controller, $action) {
19 1
		foreach ( self::$routes as $routePath => $routeDetails ) {
20 1
			if (! isset ( $routeDetails ["controller"] )) {
21 1
				$routeDetails = \current ( $routeDetails );
22
			}
23 1
			if ($controller === $routeDetails ["controller"] && $action === $routeDetails ["action"]) {
24
				$routeDetails ["path"] = $routePath;
25
				return $routeDetails;
26
			}
27
		}
28 1
		return false;
29
	}
30
31 1
	public static function getRoutesPathByController($controller) {
32 1
		$result = [ ];
33 1
		foreach ( self::$routes as $routePath => $routeDetails ) {
34 1
			if (! isset ( $routeDetails ['controller'] )) {
35 1
				foreach ( $routeDetails as $method => $routeInfo ) {
36 1
					if ($routeInfo ['controller'] === $controller) {
37 1
						$result [] = [ 'method' => $method,'url'=>self::getRoutePathInfos ( $controller, $routePath, $routeInfo ) ];
38
					}
39
				}
40
			} else {
41 1
				if ($routeDetails ['controller'] === $controller) {
42 1
					$result [] = [ 'method' => '*','url'=>self::getRoutePathInfos ( $controller, $routePath, $routeDetails ) ];
43
				}
44
			}
45
		}
46 1
		return $result;
47
	}
48
49 1
	public static function getRoutePathInfos($controller, $routePath, $routeInfo) {
50 1
		$method = new \ReflectionMethod (  $controller,$routeInfo ['action'] );
51 1
		$parameters = $method->getParameters ();
52 1
		$routeParams = $routeInfo ['parameters'];
53 1
		$pattern = "@\(.*?\)@";
54 1
		$params = [ ];
55 1
		foreach ( $routeParams as $param ) {
56 1
			if ($param === '*') {
57 1
				$params [] = $parameters [sizeof ( $params )]->getName();
58
			} else {
59 1
				$index = ( int ) filter_var ( $param, FILTER_SANITIZE_NUMBER_INT );
60 1
				$params [] = $parameters [$index]->getName();
61
			}
62
		}
63 1
		$path = $routePath;
64 1
		foreach ( $params as $param ) {
65 1
			$path = preg_replace ( $pattern, '{'.$param.'}', $path, 1 );
66
		}
67 1
		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 1
	public static function getAnnotations($controllerName, $actionName) {
84 1
		$result = [ ];
85 1
		foreach ( self::$routes as $routePath => $routeDetails ) {
86 1
			if (! isset ( $routeDetails ["controller"] )) {
87 1
				$routeDetails = \current ( $routeDetails );
88
			}
89 1
			if ($routeDetails ["controller"] === $controllerName && $routeDetails ["action"] === $actionName)
90 1
				$result [$routePath] = $routeDetails;
91
		}
92 1
		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