Passed
Push — master ( ed813f...432706 )
by Jean-Christophe
02:43
created

ControllerParserPathTrait   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 71
dl 0
loc 101
ccs 0
cts 95
cp 0
rs 9.84
c 0
b 0
f 0
wmc 32

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getPathFromMethod() 0 20 5
B cleanpath() 0 16 9
A scanParam() 0 17 5
B addParamsPath() 0 25 7
A parseMethodPath() 0 13 6
1
<?php
2
3
namespace Ubiquity\cache\parser;
4
5
use Ubiquity\utils\base\UString;
6
use Ubiquity\orm\parser\Reflexion;
7
8
trait ControllerParserPathTrait {
9
10
	protected static function getPathFromMethod(\ReflectionMethod $method) {
11
		$methodName = $method->getName ();
12
		if ($methodName === "index") {
13
			$pathParts = [ "(index/)?" ];
14
		} else {
15
			$pathParts = [ $methodName ];
16
		}
17
		$parameters = $method->getParameters ();
18
		foreach ( $parameters as $parameter ) {
19
			if ($parameter->isVariadic ()) {
20
				$pathParts [] = '{...' . $parameter->getName () . '}';
21
				return "/" . \implode ( "/", $pathParts );
22
			}
23
			if (! $parameter->isOptional ()) {
24
				$pathParts [] = '{' . $parameter->getName () . '}';
25
			} else {
26
				$pathParts [\sizeof ( $pathParts ) - 1] .= '{~' . $parameter->getName () . '}';
27
			}
28
		}
29
		return "/" . \implode ( "/", $pathParts );
30
	}
31
32
	protected static function parseMethodPath(\ReflectionMethod $method, $path) {
33
		if (! isset ( $path ) || $path === '')
34
			return;
35
		$parameters = $method->getParameters ();
36
		foreach ( $parameters as $parameter ) {
37
			$name = $parameter->getName ();
38
			if ($parameter->isVariadic ()) {
39
				$path = str_replace ( '{' . $name . '}', '{...' . $name . '}', $path );
40
			} elseif ($parameter->isOptional ()) {
41
				$path = str_replace ( '{' . $name . '}', '{~' . $name . '}', $path );
42
			}
43
		}
44
		return $path;
45
	}
46
47
	public static function cleanpath($prefix, $path = "") {
48
		$path = str_replace ( "//", "/", $path );
49
		if ($prefix !== "" && ! UString::startswith ( $prefix, "/" )) {
50
			$prefix = "/" . $prefix;
51
		}
52
		if (! UString::endswith ( $prefix, "/" )) {
53
			$prefix = $prefix . "/";
54
		}
55
		if ($path !== "" && UString::startswith ( $path, "/" )) {
56
			$path = \substr ( $path, 1 );
57
		}
58
		$path = $prefix . $path;
59
		if (! UString::endswith ( $path, "/" ) && ! UString::endswith ( $path, '(.*?)' ) && ! UString::endswith ( $path, "(index/)?" )) {
60
			$path = $path . "/";
61
		}
62
		return $path;
63
	}
64
65
	public static function addParamsPath($path, \ReflectionMethod $method, $requirements) {
66
		$parameters = [ ];
67
		$hasOptional = false;
68
		preg_match_all ( '@\{(\.\.\.|\~)?(.+?)\}@s', $path, $matches );
69
		if (isset ( $matches [2] ) && \sizeof ( $matches [2] ) > 0) {
70
			$path = \preg_quote ( $path );
71
			$params = Reflexion::getMethodParameters ( $method );
72
			$index = 0;
73
			foreach ( $matches [2] as $paramMatch ) {
74
				$find = \array_search ( $paramMatch, $params );
75
				if ($find !== false) {
76
					$requirement = '.+?';
77
					if (isset ( $requirements [$paramMatch] )) {
78
						$requirement = $requirements [$paramMatch];
79
					}
80
					self::scanParam ( $parameters, $hasOptional, $matches, $index, $paramMatch, $find, $path, $requirement );
81
				} else {
82
					throw new \Exception ( "{$paramMatch} is not a parameter of the method " . $method->name );
83
				}
84
				$index ++;
85
			}
86
		}
87
		if ($hasOptional)
88
			$path .= "/(.*?)";
89
		return [ "path" => $path,"parameters" => $parameters ];
90
	}
91
92
	public static function scanParam(&$parameters, &$hasOptional, $matches, $index, $paramMatch, $find, &$path, $requirement) {
93
		$toReplace = true;
94
		if (isset ( $matches [1] [$index] )) {
95
			if ($matches [1] [$index] === "...") {
96
				$parameters [] = "*";
97
				$path = \str_replace ( "\{\.\.\." . $paramMatch . "\}", "(.*?)", $path );
98
				$toReplace = false;
99
			} elseif ($matches [1] [$index] === "~") {
100
				$parameters [] = "~" . $find;
101
				$path = \str_replace ( "\{~" . $paramMatch . "\}", "", $path );
102
				$hasOptional = true;
103
				$toReplace = false;
104
			}
105
		}
106
		if ($toReplace) {
107
			$parameters [] = $find;
108
			$path = \str_replace ( "\{" . $paramMatch . "\}", "({$requirement})", $path );
109
		}
110
	}
111
}
112
113