Passed
Push — master ( e78b35...181f24 )
by Alex
01:47
created

RoutesSet::validateSupportedRequestMethod()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 4
rs 10
c 1
b 0
f 1
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 returns a list of supported request methods
59
     *
60
     * @return array list of supported request methods
61
     */
62
    public function getListOfSupportedRequestMethods(): array
63
    {
64
        return [
65
            'GET',
66
            'POST',
67
            'PUT',
68
            'DELETE',
69
            'OPTION'
70
        ];
71
    }
72
73
    /**
74
     * Method clears router data.
75
     */
76
    public function clear()
77
    {
78
        $this->universalRouteWasAdded = false;
79
80
        $this->routeNames = [];
81
82
        foreach ($this->getListOfSupportedRequestMethods() as $requestMethod) {
83
            $this->routes[$requestMethod] = [];
84
        }
85
    }
86
87
    /**
88
     * Method returns true if the router exists
89
     *
90
     * @param string $route
91
     *            checking route
92
     * @return bool true if the router exists, false otherwise
93
     */
94
    public function routeExists(string $route): bool
95
    {
96
        $route = trim($route, '/');
97
98
        foreach ($this->getListOfSupportedRequestMethods() as $requestMethod) {
99
            if (isset($this->routes[$requestMethod][$route])) {
100
                return true;
101
            }
102
        }
103
104
        return false;
105
    }
106
107
    /**
108
     * Method rturns all available routes
109
     */
110
    public function getAllRoutesTrace()
111
    {
112
        $trace = [];
113
114
        foreach ($this->getListOfSupportedRequestMethods() as $requestMethod) {
115
            if (count($this->routes[$requestMethod]) > 0) {
116
                $trace[] = $requestMethod . ':' . implode(', ', array_keys($this->routes[$requestMethod]));
117
            }
118
        }
119
120
        return implode('; ', $trace);
121
    }
122
123
    /**
124
     * Additing route for GET request
125
     *
126
     * @param string $route
127
     *            route
128
     * @param object $object
129
     *            callback object
130
     * @param string $method
131
     *            callback method
132
     */
133
    public function addGetRoute(string $route, object $object, string $method): void
134
    {
135
        $this->routes['GET'][trim($route, '/')] = [
136
            $object,
137
            $method
138
        ];
139
    }
140
141
    /**
142
     * Additing route for GET request
143
     *
144
     * @param string $route
145
     *            route
146
     * @param object $object
147
     *            callback object
148
     * @param string $method
149
     *            callback method
150
     */
151
    public function addPostRoute(string $route, object $object, string $method): void
152
    {
153
        $this->routes['POST'][trim($route, '/')] = [
154
            $object,
155
            $method
156
        ];
157
    }
158
159
    /**
160
     * Method registers name of the route
161
     *
162
     * @param string $routeName
163
     *            route's name
164
     * @param string $route
165
     *            route
166
     */
167
    protected function registerRouteName(string $routeName, string $route): void
168
    {
169
        if ($routeName != '') {
170
            $this->routeNames[$routeName] = $route;
171
        }
172
    }
173
174
    /**
175
     * Validating that route name exists
176
     *
177
     * @param string $routeName
178
     * @return bool
179
     */
180
    protected function routeNameExists(string $routeName): bool
181
    {
182
        return isset($this->routeNames[$routeName]);
183
    }
184
185
    /**
186
     * Getting route by name
187
     *
188
     * @param string $routeName
189
     *            route's name
190
     * @return string route
191
     */
192
    public function getRouteByName(string $routeName): string
193
    {
194
        if ($this->routeNameExists($routeName) === false) {
195
            throw (new \Exception('Route with name ' . $routeName . ' does not exist'));
196
        }
197
198
        return $this->routeNames[$routeName];
199
    }
200
}
201