Test Failed
Branch master (fb3e9d)
by 世昌
03:53
created

UriMatcher::getMatch()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
namespace nebula\route\uri;
3
4
/**
5
 * 可执行命令表达式
6
 *
7
 */
8
class UriMatcher
9
{
10
11
    /**
12
     * 匹配的正则
13
     *
14
     * @var string
15
     */
16
    protected $match;
17
18
    /**
19
     * URI中的参数
20
     *
21
     * @var RouteMatcher[]
22
     */
23
    protected $parameter;
24
    
25
    public function __construct(string $match, array $parameter)
26
    {
27
        $this->match = $match;
28
        $this->parameter = $parameter;
29
    }
30
31
    /**
32
     * 匹配URL
33
     *
34
     * @param string $url
35
     * @param boolean $ignoreCase
36
     * @return array|null
37
     */
38
    public function match(string $url, bool $ignoreCase = true):?array
39
    {
40
        $match = '#^'. $this->match.'$#'. ($ignoreCase?'i':'');
41
        if (preg_match($match, $url, $parameter)) {
42
            array_shift($parameter);
43
            return $parameter;
44
        }
45
        return null;
46
    }
47
48
    /**
49
     * 构建参数
50
     *
51
     * @param array $matchParameter
52
     * @return array
53
     */
54
    public function buildParamter(array $matchParameter):array
55
    {
56
        $parameters = [];
57
        foreach ($this->parameter as $index => $parameter) {
58
            if (isset($matchParameter[$index])) {
59
                $value = $parameter->getValue($matchParameter[$index]);
60
            } else {
61
                $value = $parameter->getDefaultValue();
62
            }
63
            $parameters[$parameter->getIndexName()] =  $value;
64
        }
65
        return $parameters;
66
    }
67
68
    /**
69
     * Get 匹配的正则
70
     *
71
     * @return  string
72
     */
73
    public function getMatch()
74
    {
75
        return $this->match;
76
    }
77
78
    /**
79
     * Set 匹配的正则
80
     *
81
     * @param  string  $match  匹配的正则
82
     *
83
     * @return  self
84
     */
85
    public function setMatch(string $match)
86
    {
87
        $this->match = $match;
88
89
        return $this;
90
    }
91
}
92