Completed
Push — master ( ec1c77...41662e )
by Mr
02:15
created

Route::setMethods()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace DrMVC\Router;
4
5
use Psr\Http\Message\ServerRequestInterface;
6
use Psr\Http\Message\ResponseInterface;
7
8
/**
9
 * Class Route
10
 * @package DrMVC\Router
11
 * @since 3.0.0
12
 */
13
class Route implements RouteInterface
14
{
15
    /**
16
     * @var array
17
     */
18
    private $_variables = [];
19
20
    /**
21
     * @var string
22
     */
23
    private $_regexp;
24
25
    /**
26
     * @var mixed
27
     */
28
    private $_callback;
29
30
    /**
31
     * @var ServerRequestInterface
32
     */
33
    private $_request;
34
35
    /**
36
     * @var ResponseInterface
37
     */
38
    private $_response;
39
40
    /**
41
     * @var array
42
     */
43
    private $_methods = [];
44
45
    /**
46
     * Route constructor.
47
     *
48
     * @param   array $methods Method of received query
49
     * @param   string $regexp Regular expression
50
     * @param   callable|string $callable Class name or callback
51
     * @param   ServerRequestInterface $request PSR-7 request
52
     * @param   ResponseInterface $response RSP-7 response
53
     */
54
    public function __construct(
55
        array $methods,
56
        string $regexp,
57
        $callable,
58
        ServerRequestInterface $request = null,
59
        ResponseInterface $response = null
60
    ) {
61
        $this
62
            ->setRoute($methods, $regexp, $callable)
63
            ->setRequest($request)
64
            ->setResponse($response);
65
    }
66
67
    /**
68
     * Set current method of object
69
     *
70
     * @param   array $methods
71
     * @return  RouteInterface
72
     */
73
    public function setMethods(array $methods): RouteInterface
74
    {
75
        $this->_methods = $methods;
76
        return $this;
77
    }
78
79
    /**
80
     * Get method of current object
81
     *
82
     * @return array
83
     */
84
    public function getMethods(): array
85
    {
86
        return $this->_methods;
87
    }
88
89
    /**
90
     * Check if method is in set
91
     *
92
     * @param   string $method
93
     * @return  bool
94
     */
95
    public function checkMethod(string $method): bool
96
    {
97
        return \in_array($method, $this->getMethods(), false);
98
    }
99
100
    /**
101
     * Set variables of current class
102
     *
103
     * @param   array $variables
104
     * @return  RouteInterface
105
     */
106
    public function setVariables(array $variables): RouteInterface
107
    {
108
        $this->_variables = $variables;
109
        return $this;
110
    }
111
112
    /**
113
     * Return array of available variables
114
     *
115
     * @return  array
116
     */
117
    public function getVariables(): array
118
    {
119
        return $this->_variables;
120
    }
121
122
    /**
123
     * Set single route
124
     *
125
     * @param   array $methods Method of received query
126
     * @param   string $regexp Regular expression
127
     * @param   callable|string $callable Class name or callback
128
     * @return  RouteInterface
129
     */
130
    public function setRoute(
131
        array $methods,
132
        string $regexp,
133
        $callable
134
    ): RouteInterface {
135
        return $this
136
            ->setMethods($methods)
137
            ->setRegexp($regexp)
138
            ->setCallback($callable);
139
    }
140
141
    /**
142
     * Set PSR-7 request
143
     *
144
     * @param   ServerRequestInterface $request - PSR-7 request
145
     * @return  RouteInterface
146
     */
147
    public function setRequest(ServerRequestInterface $request = null): RouteInterface
148
    {
149
        if (null !== $request) {
150
            $this->_request = $request;
151
        }
152
        return $this;
153
    }
154
155
    /**
156
     * Get PSR request
157
     *
158
     * @return  ServerRequestInterface|null
159
     */
160
    public function getRequest()
161
    {
162
        return $this->_request;
163
    }
164
165
    /**
166
     * Set RSR response
167
     *
168
     * @param   mixed $response
169
     * @return  RouteInterface
170
     */
171
    public function setResponse(ResponseInterface $response = null): RouteInterface
172
    {
173
        if (!empty($response)) {
174
            $this->_response = $response;
175
        }
176
        return $this;
177
    }
178
179
    /**
180
     * Get RSR-7 response
181
     *
182
     * @return  ResponseInterface|null
183
     */
184
    public function getResponse()
185
    {
186
        return $this->_response;
187
    }
188
189
    /**
190
     * Set callable element or class
191
     *
192
     * @param   mixed $callback
193
     * @return  RouteInterface
194
     */
195
    public function setCallback($callback): RouteInterface
196
    {
197
        $this->_callback = $callback;
198
        return $this;
199
    }
200
201
    /**
202
     * Return callable element
203
     *
204
     * @return  callable|string
205
     */
206
    public function getCallback()
207
    {
208
        return $this->_callback;
209
    }
210
211
    /**
212
     * Set regexp of current route
213
     *
214
     * @param   string $regexp
215
     * @return  RouteInterface
216
     */
217
    public function setRegexp(string $regexp): RouteInterface
218
    {
219
        $pattern = ['/</', '/>/'];
220
        $replace = ['(?P<', '>.+)'];
221
        $regexp = preg_replace($pattern, $replace, $regexp);
222
        $this->_regexp = '#^' . $regexp . '$#u';
223
        return $this;
224
    }
225
226
    /**
227
     * Return regexp of current route
228
     *
229
     * @return  string
230
     */
231
    public function getRegexp(): string
232
    {
233
        return $this->_regexp;
234
    }
235
236
}
237