Route::setVariables()   A
last analyzed

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
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 array
32
     */
33
    private $_methods = [];
34
35
    /**
36
     * Route constructor.
37
     *
38
     * @param   array $methods Method of received query
39
     * @param   string $regexp Regular expression
40
     * @param   callable|string $callable Class name or callback
41
     */
42
    public function __construct(array $methods, string $regexp, $callable)
43
    {
44
        $this->setRoute($methods, $regexp, $callable);
45
    }
46
47
    /**
48
     * Set current method of object
49
     *
50
     * @param   array $methods
51
     * @return  RouteInterface
52
     */
53
    public function setMethods(array $methods): RouteInterface
54
    {
55
        $this->_methods = $methods;
56
        return $this;
57
    }
58
59
    /**
60
     * Get method of current object
61
     *
62
     * @return array
63
     */
64
    public function getMethods(): array
65
    {
66
        return $this->_methods;
67
    }
68
69
    /**
70
     * Check if method is in set
71
     *
72
     * @param   string $method
73
     * @return  bool
74
     */
75
    public function checkMethod(string $method): bool
76
    {
77
        return \in_array($method, $this->getMethods(), false);
78
    }
79
80
    /**
81
     * Set variables of current class
82
     *
83
     * @param   array $variables
84
     * @return  RouteInterface
85
     */
86
    public function setVariables(array $variables): RouteInterface
87
    {
88
        $this->_variables = $variables;
89
        return $this;
90
    }
91
92
    /**
93
     * Return array of available variables
94
     *
95
     * @return  array
96
     */
97
    public function getVariables(): array
98
    {
99
        return $this->_variables;
100
    }
101
102
    /**
103
     * Set single route
104
     *
105
     * @param   array $methods Method of received query
106
     * @param   string $regexp Regular expression
107
     * @param   callable|string $callable Class name or callback
108
     * @return  RouteInterface
109
     */
110
    public function setRoute(array $methods, string $regexp, $callable): RouteInterface
111
    {
112
        return $this
113
            ->setMethods($methods)
114
            ->setRegexp($regexp)
115
            ->setCallback($callable);
116
    }
117
118
    /**
119
     * Set callable element or class
120
     *
121
     * @param   mixed $callback
122
     * @return  RouteInterface
123
     */
124
    public function setCallback($callback): RouteInterface
125
    {
126
        $this->_callback = $callback;
127
        return $this;
128
    }
129
130
    /**
131
     * Return callable element
132
     *
133
     * @return  callable|string
134
     */
135
    public function getCallback()
136
    {
137
        return $this->_callback;
138
    }
139
140
    /**
141
     * Set regexp of current route
142
     *
143
     * @param   string $regexp
144
     * @return  RouteInterface
145
     */
146
    public function setRegexp(string $regexp): RouteInterface
147
    {
148
        $pattern = ['/</', '/>/'];
149
        $replace = ['(?P<', '>.+)'];
150
        $regexp = preg_replace($pattern, $replace, $regexp);
151
        $this->_regexp = '#^' . $regexp . '$#u';
152
        return $this;
153
    }
154
155
    /**
156
     * Return regexp of current route
157
     *
158
     * @return  string
159
     */
160
    public function getRegexp(): string
161
    {
162
        return $this->_regexp;
163
    }
164
165
}
166