1: <?php
2:
3: namespace PHPixie\Route;
4:
5: class Matcher
6: {
7: public function match($pattern, $string)
8: {
9: $regex = '#^'.$pattern->regex().'$#i';
10: if(($matches = $this->matchRegex($regex, $string)) === null) {
11: return null;
12: }
13:
14: return $this->mapParameters($pattern, $matches);
15: }
16:
17: public function matchPrefix($pattern, $string)
18: {
19: $regex = '#^'.$pattern->regex().'(.*)$#i';
20: if(($matches = $this->matchRegex($regex, $string)) === null) {
21: return array(null, $string);
22: }
23:
24: $tail = array_pop($matches);
25: $parameters = $this->mapParameters($pattern, $matches);
26: return array($parameters, $tail);
27: }
28:
29: protected function matchRegex($regex, $string)
30: {
31: if(preg_match($regex, $string, $matches) !== 1) {
32: return null;
33: }
34:
35: array_shift($matches);
36: return $matches;
37: }
38:
39: protected function mapParameters($pattern, $matches)
40: {
41: $names = $pattern->parameterNames();
42: $names = array_slice($names, 0, count($matches));
43: if(empty($names)) {
44: return array();
45: }
46: return array_combine($names, $matches);
47: }
48: }
49: