Passed
Branch main (bad861)
by Moises
01:31 queued 20s
created

Route::setUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace DevPontes\Route;
4
5
/**
6
 * Description of Route
7
 *
8
 * @author Moises Pontes
9
 * @package DevPontes\Route
10
 */
11
class Route
12
{
13
    /** @var array */
14
    private $error;
15
16
    /** @var string */
17
    private $param;
18
19
    /** @var array */
20
    private $routes;
21
22
    /** @var string */
23
    private $method;
24
25
    /** @var string */
26
    private $controller;
27
28
    /** @var string */
29
    private $controlPath;
30
31
    /** @var array */
32
    private $url;
33
34
    /**
35
     * Route constructor.
36
     *
37
     * @param string $controlPath
38
     * @param array $routes
39
     * @param array $error
40
     */
41
    public function __construct(string $controlPath, array $routes, array $error)
42
    {
43
        $url = filter_input(INPUT_GET, 'url', FILTER_SANITIZE_SPECIAL_CHARS);
44
45
        $this->error = $error;
46
        $this->controlPath = $controlPath;
47
48
        $this->setUrl($url);
49
        $this->setRoutes($routes);
50
    }
51
52
    /**
53
     * Separa a url em partes
54
     *
55
     * @param string|null $url
56
     * @return void
57
     */
58
    private function setUrl(?string $url): void
59
    {
60
        $url = "/" . $url;
61
        $this->url = explode('/', $url);
62
    }
63
64
    /**
65
     * Configura as rotas
66
     *
67
     * @param array $routes
68
     * @return void
69
     */
70
    private function setRoutes(array $routes): void
71
    {
72
        $this->routes = [];
73
74
        foreach ($routes as $route) {
75
            $dispatch = explode('@', $route[1]);
76
77
            $set = [
78
                "url"        => $route[0],
79
                "controller" => $dispatch[0],
80
                "method"     => $dispatch[1],
81
            ];
82
83
            array_push($this->routes, $set);
84
        }
85
    }
86
87
    /**
88
     * @param array $routeArr
89
     * @param string $route
90
     * @return void
91
     */
92
    private function setParam(array $routeArr, string &$route): void
93
    {
94
        foreach ($routeArr as $k => $v) {
95
            if (preg_match('/^\{.*\}$/', $v) && (count($this->url) == count($routeArr))) {
96
                $routeArr[$k] = $this->url[$k];
97
                $this->param  = $this->url[$k];
98
            }
99
100
            $route = implode('/', $routeArr);
101
        }
102
    }
103
104
    /**
105
     * Execulta a controler e o método referente a url
106
     *
107
     * @return void
108
     */
109
    public function run(): void
110
    {
111
        $found = false;
112
        $url = implode('/', $this->url);
113
114
        foreach ($this->routes as $route) {
115
            $routeArray = explode('/', $route['url']);
116
            $this->setParam($routeArray, $route['url']);
117
118
            if ($route['url'] == $url) {
119
                $found = true;
120
                $this->method = $route['method'];
121
                $this->controller = $route['controller'];
122
                break;
123
            }
124
        }
125
126
        if ($found) {
127
            $controller = "{$this->controlPath}\\{$this->controller}";
128
129
            call_user_func([new $controller(), $this->method], $this->param);
130
        } else {
131
            $controller = "{$this->controlPath}\\{$this->error[0]}";
132
            call_user_func([new $controller(), $this->error[1]]);
133
        }
134
    }
135
}
136