Passed
Push — master ( 516163...07b934 )
by 世昌
02:15
created

RouteMatcher::setAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
namespace nebula\component\route;
3
4
use Serializable;
5
use nebula\component\route\uri\UriMatcher;
6
7
/**
8
 * 路由匹配器
9
 *
10
 */
11
class RouteMatcher
12
{
13
    
14
    /**
15
     * 匹配的方法
16
     *
17
     * @var array
18
     */
19
    protected $methods;
20
21
    /**
22
     * 匹配的URI
23
     *
24
     * @var string
25
     */
26
    protected $uri;
27
28
    /**
29
     * 属性
30
     *
31
     * @var array
32
     */
33
    protected $attribute;
34
35
    /**
36
     * Uri匹配器
37
     *
38
     * @var UriMatcher
39
     */
40
    protected $matcher;
41
    
42
43
    public function __construct(array $methods, string $uri, array $attribute=[])
44
    {
45
        array_walk($methods, function ($value) {
46
            return strtoupper($value);
47
        });
48
        $this->methods = $methods;
49
        $this->uri = $uri;
50
        $this->matcher = UriMatcher::build($uri);
51
        $this->attribute = $attribute;
52
    }
53
    
54
    /**
55
     * Get 属性
56
     *
57
     * @return  array
58
     */
59
    public function getAttribute()
60
    {
61
        return $this->attribute;
62
    }
63
64
    /**
65
     * Set 属性
66
     *
67
     * @param  array  $attribute  属性
68
     *
69
     * @return  self
70
     */
71
    public function setAttribute(array $attribute)
72
    {
73
        $this->attribute = $attribute;
74
75
        return $this;
76
    }
77
78
    /**
79
     * Get 匹配的方法
80
     *
81
     * @return  array
82
     */
83
    public function getMethods()
84
    {
85
        return $this->methods;
86
    }
87
88
    /**
89
     * Set 匹配的方法
90
     *
91
     * @param  array  $methods  匹配的方法
92
     *
93
     * @return  self
94
     */
95
    public function setMethods(array $methods)
96
    {
97
        $this->methods = $methods;
98
99
        return $this;
100
    }
101
102
    /**
103
     * Get 匹配的URI
104
     *
105
     * @return  string
106
     */
107
    public function getUri()
108
    {
109
        return $this->uri;
110
    }
111
112
    /**
113
     * Set 匹配的URI
114
     *
115
     * @param  string  $uri  匹配的URI
116
     *
117
     * @return  self
118
     */
119
    public function setUri(string $uri)
120
    {
121
        $this->uri = $uri;
122
123
        return $this;
124
    }
125
126
    /**
127
     * Get uri匹配器
128
     *
129
     * @return  UriMatcher
130
     */
131
    public function getMatcher()
132
    {
133
        return $this->matcher;
134
    }
135
136
    /**
137
     * 匹配请求
138
     *
139
     * @param RequestInterface $request
140
     * @return array|null
141
     */
142
    public function match(RequestInterface $request):?array {
143
        if (\in_array($request->getMethod(), $this->methods)) {
144
            if ($parameter = $this->matcher->match($request->getUrl())) {
145
                return $this->matcher->buildParamter($parameter);
146
            }
147
        }
148
        return null;
149
    }
150
}
151