Passed
Push — master ( b57abd...c4e347 )
by Alex
01:45
created

Utils::getCallableDescription()   A

Complexity

Conditions 6
Paths 5

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 6
eloc 8
nc 5
nop 1
dl 0
loc 13
rs 9.2222
c 3
b 0
f 0
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
    /**
91
     * Method detects if the $string is a parameter or a static component of the route
92
     *
93
     * @param string $string
94
     *            String to be validated
95
     * @return bool Does we have parameter
96
     */
97
    public static function isParameter($string): bool
98
    {
99
        return $string[0] == '[' && $string[strlen($string) - 1] == ']';
100
    }
101
}