Standard.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /**
  3. *
  4. * Cube Framework $Id$ cOvGSa9QPt4189rmC7aLgem5hXPRB+LLhHrBiSlt62c=
  5. *
  6. * @link http://codecu.be/framework
  7. * @copyright Copyright (c) 2014 CodeCube SRL
  8. * @license http://codecu.be/framework/license Commercial License
  9. *
  10. * @version 1.2
  11. */
  12. /**
  13. * route object when mod rewrite or equivalent is not available
  14. */
  15. namespace Cube\Controller\Router\Route;
  16. use Cube\Controller\Router\Standard as StandardRouter;
  17. class Standard extends AbstractRoute
  18. {
  19. /**
  20. *
  21. * set route default parameters
  22. * if there are keys in the defaults array that are different than controller and action,
  23. * then add these as params
  24. *
  25. * @param array $defaults
  26. *
  27. * @return $this
  28. */
  29. public function setDefaults(array $defaults)
  30. {
  31. foreach ($defaults as $key => $value) {
  32. $this->setParam($key, $value);
  33. }
  34. $this->_defaults = $defaults;
  35. return $this;
  36. }
  37. /**
  38. *
  39. * get all params
  40. *
  41. * @return array
  42. */
  43. public function getAllParams()
  44. {
  45. $params['module'] = $this->getModule();
  46. $params['controller'] = $this->getController();
  47. $params['action'] = $this->getAction();
  48. $params += array_filter($this->getParams());
  49. return $params;
  50. }
  51. /**
  52. *
  53. * get an array of params and if the route matches, return a routed url
  54. * the method will also route requests if all params in the route match the params in
  55. * the request, but there are extra params in the request
  56. *
  57. * @param array $params
  58. * @param bool $named if the flag is set to true, we need to match by params only
  59. *
  60. * @return string|null the assembled url or null if the route doesnt match
  61. */
  62. public function assemble($params, $named = false)
  63. {
  64. if (!is_array($params)) {
  65. $params = (array)$params;
  66. }
  67. $allParams = $this->getAllParams() + array_filter($params);
  68. foreach ($params as $key => $value) {
  69. $allParams[$key] = $value;
  70. }
  71. $params = $allParams;
  72. $get = array();
  73. foreach ((array)$params as $key => $value) {
  74. if (preg_match('#^[a-zA-Z0-9_-]+$#', $key)) {
  75. if (!is_array($value)) {
  76. if (in_array($key, array('module', 'controller', 'action'))) {
  77. $value = $this->normalize($value, true);
  78. }
  79. $get[] = $key . '=' . $value;
  80. }
  81. else {
  82. foreach ((array)$value as $val) {
  83. if (!empty($val)) {
  84. $get[] = $key . '[]=' . $val;
  85. }
  86. }
  87. }
  88. }
  89. }
  90. $uri = StandardRouter::DEFAULT_PATH;
  91. if (count($get) > 0) {
  92. $uri .= '?' . implode('&', $get);
  93. }
  94. return $uri;
  95. }
  96. }