1 | <?php |
||
2 | declare(strict_types = 1); |
||
3 | namespace Mezon\Router; |
||
4 | |||
5 | /** |
||
6 | * Class Utils |
||
7 | * |
||
8 | * @package Router |
||
9 | * @author Dodonov A.A. |
||
10 | * @version v.1.0 (2020/01/17) |
||
11 | * @copyright Copyright (c) 2020, http://aeon.su |
||
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; |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
42 | } |
||
43 | |||
44 | /** |
||
45 | * Method prepares route for the next processing |
||
46 | * |
||
47 | * @param string[]|string $route |
||
48 | * route |
||
49 | * @return string trimmed route |
||
50 | * @psalm-suppress MixedArgumentTypeCoercion, MixedAssignment |
||
51 | */ |
||
52 | public static function prepareRoute($route): string |
||
53 | { |
||
54 | if (is_array($route) && $route[0] === '') { |
||
55 | $route = (string)$_SERVER['REQUEST_URI']; |
||
56 | } |
||
57 | |||
58 | if ($route === '/') { |
||
59 | $route = 'index'; |
||
60 | } |
||
61 | |||
62 | if (is_array($route)) { |
||
0 ignored issues
–
show
|
|||
63 | $route = implode('/', $route); |
||
64 | } |
||
65 | |||
66 | $route = urldecode($route); |
||
67 | |||
68 | /** @var string $route */ |
||
69 | return trim($route, '/'); |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * Method compiles callable description |
||
74 | * |
||
75 | * @param |
||
76 | * string|mixed|array{0:string, 1:string} $processor |
||
77 | * object to be descripted |
||
78 | * @return string description |
||
79 | * @psalm-suppress MixedOperand, MixedArrayAccess, MixedArgument, MissingParamType |
||
80 | */ |
||
81 | public static function getCallableDescription($processor): string |
||
82 | { |
||
83 | if (is_string($processor)) { |
||
84 | return $processor; |
||
85 | } elseif (isset($processor[0]) && isset($processor[1])) { |
||
86 | if (is_object($processor[0])) { |
||
87 | return get_class($processor[0]) . '::' . $processor[1]; |
||
88 | } elseif (is_string($processor[0])) { |
||
89 | return $processor[0] . '::' . $processor[1]; |
||
90 | } |
||
91 | } |
||
92 | |||
93 | return serialize($processor); |
||
94 | } |
||
95 | } |
||
96 |