1
|
|
|
<?php |
2
|
|
|
namespace Mezon\Router; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Class Utils |
6
|
|
|
* |
7
|
|
|
* @package Router |
8
|
|
|
* @subpackage Utils |
9
|
|
|
* @author Dodonov A.A. |
10
|
|
|
* @version v.1.0 (2020/01/17) |
11
|
|
|
* @copyright Copyright (c) 2020, aeon.org |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Router utilities class |
16
|
|
|
*/ |
17
|
|
|
class Utils |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Converting method name to route |
22
|
|
|
* |
23
|
|
|
* @param string $methodName |
24
|
|
|
* method name |
25
|
|
|
* @return string route |
26
|
|
|
*/ |
27
|
|
|
public static function convertMethodNameToRoute(string $methodName): string |
28
|
|
|
{ |
29
|
|
|
$methodName = str_replace('action', '', $methodName); |
30
|
|
|
|
31
|
|
|
if (ctype_upper($methodName[0])) { |
32
|
|
|
$methodName[0] = strtolower($methodName[0]); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
for ($i = 1; $i < strlen($methodName); $i ++) { |
36
|
|
|
if (ctype_upper($methodName[$i])) { |
37
|
|
|
$methodName = substr_replace($methodName, '-' . strtolower($methodName[$i]), $i, 1); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
return $methodName; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Method prepares route for the next processing |
46
|
|
|
* |
47
|
|
|
* @param mixed $route |
48
|
|
|
* Route |
49
|
|
|
* @return string Trimmed route |
50
|
|
|
*/ |
51
|
|
|
public static function prepareRoute($route): string |
52
|
|
|
{ |
53
|
|
|
if (is_array($route) && $route[0] === '') { |
54
|
|
|
$route = $_SERVER['REQUEST_URI']; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
if ($route == '/') { |
58
|
|
|
$route = 'index'; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if (is_array($route)) { |
62
|
|
|
$route = implode('/', $route); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return trim($route, '/'); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Method compiles callable description |
70
|
|
|
* |
71
|
|
|
* @param mixed $processor |
72
|
|
|
* Object to be descripted |
73
|
|
|
* @return string Description |
74
|
|
|
*/ |
75
|
|
|
public static function getCallableDescription($processor): string |
76
|
|
|
{ |
77
|
|
|
if (is_string($processor)) { |
78
|
|
|
return $processor; |
79
|
|
|
} elseif (isset($processor[0]) && isset($processor[1])) { |
80
|
|
|
if (is_object($processor[0])) { |
81
|
|
|
return get_class($processor[0]) . '::' . $processor[1]; |
82
|
|
|
} elseif (is_string($processor[0])) { |
83
|
|
|
return $processor[0] . '::' . $processor[1]; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return serialize($processor); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|