Passed
Push — master ( b9acc3...1dafdc )
by Jean-Christophe
03:25
created

ControllerParserPathTrait::cleanpath()   B

Complexity

Conditions 9
Paths 16

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 9.0468

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 16
ccs 11
cts 12
cp 0.9167
rs 8.0555
c 0
b 0
f 0
cc 9
nc 16
nop 2
crap 9.0468
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 1
	protected static function getPathFromMethod(\ReflectionMethod $method) {
11 1
		$methodName = $method->getName ();
12 1
		if ($methodName === "index") {
13 1
			$pathParts = [ "(index/)?" ];
14
		} else {
15 1
			$pathParts = [ $methodName ];
16
		}
17 1
		$parameters = $method->getParameters ();
18 1
		foreach ( $parameters as $parameter ) {
19 1
			if ($parameter->isVariadic ()) {
20 1
				$pathParts [] = '{...' . $parameter->getName () . '}';
21 1
				return "/" . \implode ( "/", $pathParts );
22
			}
23 1
			if (! $parameter->isOptional ()) {
24 1
				$pathParts [] = '{' . $parameter->getName () . '}';
25
			} else {
26 1
				$pathParts [\sizeof ( $pathParts ) - 1] .= '{~' . $parameter->getName () . '}';
27
			}
28
		}
29 1
		return "/" . \implode ( "/", $pathParts );
30
	}
31
32 1
	protected static function parseMethodPath(\ReflectionMethod $method, $path) {
33 1
		if (! isset ( $path ) || $path === '')
34
			return;
35 1
		$parameters = $method->getParameters ();
36 1
		foreach ( $parameters as $parameter ) {
37 1
			$name = $parameter->getName ();
38 1
			if ($parameter->isVariadic ()) {
39
				$path = str_replace ( '{' . $name . '}', '{...' . $name . '}', $path );
40 1
			} elseif ($parameter->isOptional ()) {
41
				$path = str_replace ( '{' . $name . '}', '{~' . $name . '}', $path );
42
			}
43
		}
44 1
		return $path;
45
	}
46
47 11
	public static function cleanpath($prefix, $path = "") {
48 11
		$path = str_replace ( "//", "/", $path );
49 11
		if ($prefix !== "" && ! UString::startswith ( $prefix, "/" )) {
50
			$prefix = "/" . $prefix;
51
		}
52 11
		if (! UString::endswith ( $prefix, "/" )) {
53 11
			$prefix = $prefix . "/";
54
		}
55 11
		if ($path !== "" && UString::startswith ( $path, "/" )) {
56 1
			$path = \substr ( $path, 1 );
57
		}
58 11
		$path = $prefix . $path;
59 11
		if (! UString::endswith ( $path, "/" ) && ! UString::endswith ( $path, '(.*?)' ) && ! UString::endswith ( $path, "(index/)?" )) {
60 11
			$path = $path . "/";
61
		}
62 11
		return $path;
63
	}
64
65 11
	public static function addParamsPath($path, \ReflectionMethod $method, $requirements) {
66 11
		$parameters = [ ];
67 11
		$hasOptional = false;
68 11
		preg_match_all ( '@\{(\.\.\.|\~)?(.+?)\}@s', $path, $matches );
69 11
		if (isset ( $matches [2] ) && \sizeof ( $matches [2] ) > 0) {
70 1
			$path = \preg_quote ( $path );
71 1
			$params = Reflexion::getMethodParameters ( $method );
72 1
			$index = 0;
73 1
			foreach ( $matches [2] as $paramMatch ) {
74 1
				$find = \array_search ( $paramMatch, $params );
75 1
				if ($find !== false) {
76 1
					$requirement = '.+?';
77 1
					if (isset ( $requirements [$paramMatch] )) {
78 1
						$requirement = $requirements [$paramMatch];
79
					}
80 1
					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 1
				$index ++;
85
			}
86
		}
87 11
		if ($hasOptional)
88 1
			$path .= "/(.*?)";
89 11
		return [ "path" => $path,"parameters" => $parameters ];
90
	}
91
92 1
	public static function scanParam(&$parameters, &$hasOptional, $matches, $index, $paramMatch, $find, &$path, $requirement) {
93 1
		$toReplace = true;
94 1
		if (isset ( $matches [1] [$index] )) {
95 1
			if ($matches [1] [$index] === "...") {
96 1
				$parameters [] = "*";
97 1
				$path = \str_replace ( "\{\.\.\." . $paramMatch . "\}", "(.*?)", $path );
98 1
				$toReplace = false;
99 1
			} elseif ($matches [1] [$index] === "~") {
100 1
				$parameters [] = "~" . $find;
101 1
				$path = \str_replace ( "\{~" . $paramMatch . "\}", "", $path );
102 1
				$hasOptional = true;
103 1
				$toReplace = false;
104
			}
105
		}
106 1
		if ($toReplace) {
107 1
			$parameters [] = $find;
108 1
			$path = \str_replace ( "\{" . $paramMatch . "\}", "({$requirement})", $path );
109
		}
110 1
	}
111
}
112
113