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

UriMatcher   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 29
dl 0
loc 111
rs 10
c 0
b 0
f 0
wmc 13

7 Methods

Rating   Name   Duplication   Size   Complexity  
A buildParamter() 0 13 3
A getParameter() 0 8 3
A getUri() 0 3 1
A setMatch() 0 5 1
A match() 0 8 3
A __construct() 0 5 1
A getMatch() 0 3 1
1
<?php
2
namespace nebula\component\route\uri;
3
4
use nebula\component\route\uri\MatcherHelper;
5
use nebula\component\route\uri\parameter\Parameter;
6
7
/**
8
 * 可执行命令表达式
9
 *
10
 */
11
class UriMatcher extends MatcherHelper
12
{
13
14
    /**
15
     * 匹配Uri
16
     *
17
     * @var string
18
     */
19
    protected $uri;
20
    /**
21
     * 匹配的正则
22
     *
23
     * @var string
24
     */
25
    protected $match;
26
27
    /**
28
     * URI中的参数
29
     *
30
     * @var Parameter[]
31
     */
32
    protected $parameter;
33
    
34
    
35
    public function __construct(string $uri, string $match, array $parameter)
36
    {
37
        $this->uri = $uri;
38
        $this->match = $match;
39
        $this->parameter = $parameter;
40
    }
41
42
    /**
43
     * 匹配URL
44
     *
45
     * @param string $url
46
     * @param boolean $ignoreCase
47
     * @return array|null
48
     */
49
    public function match(string $url, bool $ignoreCase = true):?array
50
    {
51
        $match = '#^'. $this->match.'$#'. ($ignoreCase?'i':'');
52
        $parameter = [];
53
        if (preg_match($match, $url, $parameter, PREG_UNMATCHED_AS_NULL) > 0) {
54
            return array_slice($parameter, 1);
55
        }
56
        return null;
57
    }
58
59
    /**
60
     * 构建参数
61
     *
62
     * @param array $matchedParameter
63
     * @return array
64
     */
65
    public function buildParamter(array $matchedParameter):array
66
    {
67
        $parameters = [];
68
        foreach ($this->parameter as $index => $parameter) {
69
            $match = $matchedParameter[$index] ?? null;
70
            if (is_null($match)) {
71
                $value = $parameter->getDefaultValue();
72
            } else {
73
                $value = $parameter->unpackValue($match);
74
            }
75
            $parameters[$parameter->getIndexName()] =  $value;
76
        }
77
        return $parameters;
78
    }
79
80
    public function getParameter(string $name):?Parameter
81
    {
82
        foreach ($this->parameter as $parameter) {
83
            if ($parameter->getIndexName() === $name) {
84
                return $parameter;
85
            }
86
        }
87
        return null;
88
    }
89
90
    /**
91
     * Get 匹配的正则
92
     *
93
     * @return  string
94
     */
95
    public function getMatch()
96
    {
97
        return $this->match;
98
    }
99
100
    /**
101
     * Set 匹配的正则
102
     *
103
     * @param  string  $match  匹配的正则
104
     *
105
     * @return  self
106
     */
107
    public function setMatch(string $match)
108
    {
109
        $this->match = $match;
110
111
        return $this;
112
    }
113
114
    /**
115
     * Get 匹配Uri
116
     *
117
     * @return  string
118
     */
119
    public function getUri()
120
    {
121
        return $this->uri;
122
    }
123
}
124