UrlComponent.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. namespace Tools\Controller\Component;
  3. use Cake\Routing\Router;
  4. use Shim\Controller\Component\Component;
  5. /**
  6. * A component for URL topics
  7. *
  8. * @author Mark Scherer
  9. * @license MIT
  10. */
  11. class UrlComponent extends Component {
  12. /**
  13. * @param array $url
  14. * @return array
  15. */
  16. public function resetArray(array $url) {
  17. $url += $this->defaults();
  18. return $url;
  19. }
  20. /**
  21. * @param array $url
  22. * @return array
  23. */
  24. public function completeArray(array $url) {
  25. $url = $this->addQueryStrings($url);
  26. return $url;
  27. }
  28. /**
  29. * Creates a reset URL.
  30. * The prefix and plugin params are resetting to default false.
  31. *
  32. * Can only add defaults for array URLs.
  33. *
  34. * @param string|array|null $url URL.
  35. * @param bool $full If true, the full base URL will be prepended to the result
  36. * @return string Full translated URL with base path.
  37. */
  38. public function buildReset($url = null, $full = false) {
  39. if (is_array($url)) {
  40. $url += $this->defaults();
  41. }
  42. return Router::url($url, $full);
  43. }
  44. /**
  45. * Returns a URL based on provided parameters.
  46. *
  47. * Can only add query strings for array URLs.
  48. *
  49. * @param string|array|null $url URL.
  50. * @param bool $full If true, the full base URL will be prepended to the result
  51. * @return string Full translated URL with base path.
  52. */
  53. public function buildComplete($url = null, $full = false) {
  54. if (is_array($url)) {
  55. $url = $this->addQueryStrings($url);
  56. }
  57. return Router::url($url, $full);
  58. }
  59. /**
  60. * Returns a URL based on provided parameters.
  61. *
  62. * ### Options:
  63. *
  64. * - `fullBase`: If true, the full base URL will be prepended to the result
  65. *
  66. * @param string|array|null $url Either a relative string url like `/products/view/23` or
  67. * an array of URL parameters. Using an array for URLs will allow you to leverage
  68. * the reverse routing features of CakePHP.
  69. * @param array $options Array of options
  70. * @return string Full translated URL with base path.
  71. */
  72. public function build($url = null, array $options = []) {
  73. $defaults = [
  74. 'fullBase' => false,
  75. ];
  76. $options += $defaults;
  77. $url = Router::url($url, $options['fullBase']);
  78. return $url;
  79. }
  80. /**
  81. * @return array
  82. */
  83. public function defaults() {
  84. return [
  85. 'prefix' => false,
  86. 'plugin' => false
  87. ];
  88. }
  89. /**
  90. * @param array $url
  91. *
  92. * @return array
  93. */
  94. protected function addQueryStrings(array $url) {
  95. if (!isset($url['?'])) {
  96. $url['?'] = [];
  97. }
  98. $url['?'] += $this->request->query;
  99. return $url;
  100. }
  101. }