Completed
Push — master ( 181f24...8b19ed )
by Alex
01:38
created

RoutesSet::setRouteCallback()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 3
rs 10
1
<?php
2
namespace Mezon\Router;
3
4
trait RoutesSet
5
{
6
7
    /**
8
     * List off routes for all supported request methods
9
     */
10
    private $routes = [
11
        'GET' => [],
12
        'POST' => [],
13
        'PUT' => [],
14
        'DELETE' => [],
15
        'OPTION' => []
16
    ];
17
18
    /**
19
     * Route names
20
     *
21
     * @var array
22
     */
23
    private $routeNames = [];
24
25
    /**
26
     * This flag rises when we add route / * /
27
     *
28
     * @var bool
29
     */
30
    protected $universalRouteWasAdded = false;
31
32
    /**
33
     * Method validates request method
34
     *
35
     * @param string $requestMethod
36
     *            HTTP request method
37
     */
38
    protected function validateRequestMethod(string $requestMethod): void
39
    {
40
        if (isset($this->routes[$requestMethod]) === false) {
41
            throw (new \Exception('Unsupported request method'));
42
        }
43
    }
44
45
    /**
46
     * Method returns list of routes for the HTTP method.
47
     *
48
     * @param string $requestMethod
49
     *            HTTP request method
50
     * @return array Routes
51
     */
52
    protected function &getRoutesForMethod(string $requestMethod): array
53
    {
54
        return $this->routes[$requestMethod];
55
    }
56
57
    /**
58
     * Method sets callback for router
59
     *
60
     * @param string $route
61
     *            route
62
     * @param mixed $callback
63
     *            route callback
64
     * @param string $requestMethod
65
     *            HTTP request method
66
     */
67
    protected function setRouteCallback(string $route, $callback, string $requestMethod): void
68
    {
69
        $this->routes[$requestMethod][$route] = $callback;
70
    }
71
72
    /**
73
     * Method returns a list of supported request methods
74
     *
75
     * @return array list of supported request methods
76
     */
77
    public function getListOfSupportedRequestMethods(): array
78
    {
79
        return [
80
            'GET',
81
            'POST',
82
            'PUT',
83
            'DELETE',
84
            'OPTION'
85
        ];
86
    }
87
88
    /**
89
     * Method clears router data.
90
     */
91
    public function clear()
92
    {
93
        $this->universalRouteWasAdded = false;
94
95
        $this->routeNames = [];
96
97
        foreach ($this->getListOfSupportedRequestMethods() as $requestMethod) {
98
            $this->routes[$requestMethod] = [];
99
        }
100
    }
101
102
    /**
103
     * Method returns true if the router exists
104
     *
105
     * @param string $route
106
     *            checking route
107
     * @return bool true if the router exists, false otherwise
108
     */
109
    public function routeExists(string $route): bool
110
    {
111
        $route = trim($route, '/');
112
113
        foreach ($this->getListOfSupportedRequestMethods() as $requestMethod) {
114
            if (isset($this->routes[$requestMethod][$route])) {
115
                return true;
116
            }
117
        }
118
119
        return false;
120
    }
121
122
    /**
123
     * Method rturns all available routes
124
     */
125
    public function getAllRoutesTrace()
126
    {
127
        $trace = [];
128
129
        foreach ($this->getListOfSupportedRequestMethods() as $requestMethod) {
130
            if (count($this->routes[$requestMethod]) > 0) {
131
                $trace[] = $requestMethod . ':' . implode(', ', array_keys($this->routes[$requestMethod]));
132
            }
133
        }
134
135
        return implode('; ', $trace);
136
    }
137
138
    /**
139
     * Additing route for GET request
140
     *
141
     * @param string $route
142
     *            route
143
     * @param object $object
144
     *            callback object
145
     * @param string $method
146
     *            callback method
147
     */
148
    public function addGetRoute(string $route, object $object, string $method): void
149
    {
150
        $this->routes['GET'][trim($route, '/')] = [
151
            $object,
152
            $method
153
        ];
154
    }
155
156
    /**
157
     * Additing route for GET request
158
     *
159
     * @param string $route
160
     *            route
161
     * @param object $object
162
     *            callback object
163
     * @param string $method
164
     *            callback method
165
     */
166
    public function addPostRoute(string $route, object $object, string $method): void
167
    {
168
        $this->routes['POST'][trim($route, '/')] = [
169
            $object,
170
            $method
171
        ];
172
    }
173
174
    /**
175
     * Method registers name of the route
176
     *
177
     * @param string $routeName
178
     *            route's name
179
     * @param string $route
180
     *            route
181
     */
182
    protected function registerRouteName(string $routeName, string $route): void
183
    {
184
        if ($routeName != '') {
185
            $this->routeNames[$routeName] = $route;
186
        }
187
    }
188
189
    /**
190
     * Validating that route name exists
191
     *
192
     * @param string $routeName
193
     * @return bool
194
     */
195
    protected function routeNameExists(string $routeName): bool
196
    {
197
        return isset($this->routeNames[$routeName]);
198
    }
199
200
    /**
201
     * Getting route by name
202
     *
203
     * @param string $routeName
204
     *            route's name
205
     * @return string route
206
     */
207
    public function getRouteByName(string $routeName): string
208
    {
209
        if ($this->routeNameExists($routeName) === false) {
210
            throw (new \Exception('Route with name ' . $routeName . ' does not exist'));
211
        }
212
213
        return $this->routeNames[$routeName];
214
    }
215
}
216