bytic /
router
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Nip\Router\Parsers; |
||
| 4 | |||
| 5 | /** |
||
| 6 | * Class Regex |
||
| 7 | * @package Nip\Router\Parsers |
||
| 8 | */ |
||
| 9 | class Regex extends AbstractParser |
||
| 10 | { |
||
| 11 | protected $regex; |
||
| 12 | |||
| 13 | 3 | public function parseMap() |
|
| 14 | { |
||
| 15 | 3 | parent::parseMap(); |
|
| 16 | 3 | $this->regex = false; |
|
| 17 | 3 | } |
|
| 18 | |||
| 19 | /** |
||
| 20 | * @param $uri |
||
| 21 | * @return bool |
||
| 22 | */ |
||
| 23 | 1 | public function match($uri) |
|
| 24 | { |
||
| 25 | 1 | $return = parent::match($uri); |
|
| 26 | |||
| 27 | 1 | if ($return) { |
|
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 28 | 1 | $match = preg_match_all("`^" . $this->getRegex() . "$`i", $uri, $matches); |
|
| 29 | |||
| 30 | 1 | if ($match > 0) { |
|
| 31 | 1 | $variables = $this->getVariables(); |
|
| 32 | 1 | if (count($variables)) { |
|
| 33 | 1 | foreach ($variables as $key => $variable) { |
|
| 34 | 1 | $this->matches[$variable] = $matches[$key + 1][0]; |
|
| 35 | } |
||
| 36 | } |
||
| 37 | 1 | return true; |
|
| 38 | } |
||
| 39 | } |
||
| 40 | |||
| 41 | 1 | return false; |
|
| 42 | } |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @return mixed|string |
||
| 46 | */ |
||
| 47 | 2 | public function getRegex() |
|
| 48 | { |
||
| 49 | 2 | if (!$this->regex) { |
|
| 50 | 2 | $map = $this->map; |
|
| 51 | 2 | foreach ($this->params as $key => $value) { |
|
| 52 | 2 | if (stristr($map, ":" . $key) !== false) { |
|
| 53 | 2 | $map = str_replace(":" . $key, "(" . $value . ")", $map); |
|
| 54 | // unset($params[$key]); |
||
| 55 | 2 | $this->variables[] = $key; |
|
| 56 | } |
||
| 57 | } |
||
| 58 | 2 | $this->regex = $map; |
|
| 59 | } |
||
| 60 | |||
| 61 | 2 | return $this->regex; |
|
| 62 | } |
||
| 63 | } |
||
| 64 |