You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
1.8 KiB

11 years ago
  1. <?php namespace Majestic\App\Router;
  2. /**
  3. * @copyright NetMonsters <team@netmonsters.ru>
  4. * @link http://netmonsters.ru
  5. * @package Majestic
  6. * @subpackage app
  7. * @since 2010-02-25
  8. */
  9. class Route
  10. {
  11. protected $route;
  12. protected $action;
  13. protected $params;
  14. protected $layout;
  15. public function __construct($route, $action, $params = array(), $layout = null)
  16. {
  17. $this->route = $route;
  18. $this->action = $action;
  19. $this->params = $params;
  20. $this->layout = $layout;
  21. }
  22. /**
  23. * @param array $request
  24. * @return bool
  25. */
  26. public function match($request)
  27. {
  28. $parts = explode('/', $this->route);
  29. $cnt = count($parts);
  30. if(count($request) != $cnt) {
  31. return false;
  32. }
  33. for ($i = 0; $i < $cnt; $i++) {
  34. if (substr($parts[$i], 0, 1) == ':') {
  35. $this->params[substr($parts[$i], 1)] = urldecode($request[$i]);
  36. } elseif (substr($parts[$i], 0, 2) == '(?') {
  37. $match = array();
  38. if (!preg_match('#^' . $parts[$i] . '$#iu', $request[$i], $match)) {
  39. return false;
  40. }
  41. $start = strpos($parts[$i], '<') + 1;
  42. $key = substr($parts[$i], $start, strpos($parts[$i], '>', $start) - $start);
  43. $this->params[$key] = urldecode($match[$key]);
  44. } elseif ($parts[$i] != $request[$i]) {
  45. return false;
  46. }
  47. }
  48. return true;
  49. }
  50. public function getUri()
  51. {
  52. return '/' . $this->route;
  53. }
  54. public function getAction()
  55. {
  56. return $this->action . 'Action';
  57. }
  58. public function getLayout()
  59. {
  60. return $this->layout . 'Layout';
  61. }
  62. public function getParams()
  63. {
  64. return $this->params;
  65. }
  66. }