1 | <?php |
||
6 | class RouterParser { |
||
7 | |||
8 | /** |
||
9 | * @var string The path requested by the browser(or any client) |
||
10 | */ |
||
11 | private $path; |
||
12 | |||
13 | /** |
||
14 | * The delimiter used to separate different paths(e.g mydomain.com/api/v1/user/Siro_Diaz). |
||
15 | */ |
||
16 | const DELIMITER = '/'; |
||
17 | |||
18 | /** |
||
19 | * The parameter identifier for the path pattern (e.g /api/v1/user/:screen_name). |
||
20 | */ |
||
21 | const PARAMETER_IDENTIFIER = ':'; |
||
22 | |||
23 | public function __construct($path) { |
||
26 | |||
27 | /** |
||
28 | * Sets the path. |
||
29 | * |
||
30 | * @param $path |
||
31 | */ |
||
32 | |||
33 | public function setPath($path) { |
||
36 | |||
37 | /** |
||
38 | * Give the requested path. |
||
39 | * |
||
40 | * @return string |
||
41 | */ |
||
42 | |||
43 | public function getPath() { |
||
46 | |||
47 | /** |
||
48 | * Checks if the path pattern has parameters(strings with : at |
||
49 | * the beginning). |
||
50 | * |
||
51 | * @param $pattern |
||
52 | * @return bool |
||
53 | */ |
||
54 | |||
55 | public function hasParams($pattern) { |
||
59 | |||
60 | /** |
||
61 | * Returns the number of parameters in the path pattern. |
||
62 | * |
||
63 | * @param $pattern |
||
64 | * @return int |
||
65 | */ |
||
66 | |||
67 | public function countParams($pattern) { |
||
82 | |||
83 | /** |
||
84 | * Returns an associative array with the parameter name |
||
85 | * and its value. If there is not any parameter in the pattern |
||
86 | * then return an empty array. |
||
87 | * |
||
88 | * @param $pattern |
||
89 | * @return array |
||
90 | */ |
||
91 | |||
92 | public function getParams($pattern) { |
||
108 | |||
109 | /** |
||
110 | * Checks if the path pattern matches with the requested uri. |
||
111 | * |
||
112 | * @param $pattern |
||
113 | * @return bool |
||
114 | */ |
||
115 | |||
116 | public function match($pattern) { |
||
143 | |||
144 | } |
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: