| Total Complexity | 9 |
| Total Lines | 82 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 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) |
||
| 90 | } |
||
| 91 | } |
||
| 92 |