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.

78 lines
1.8 KiB

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