UrlHelper.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 0.9.1
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Tools\View\Helper;
  16. use Cake\View\Helper\UrlHelper as CoreUrlHelper;
  17. /**
  18. * Url Helper class.
  19. *
  20. * @author Mark Scherer
  21. * @license MIT
  22. */
  23. class UrlHelper extends CoreUrlHelper {
  24. /**
  25. * @param array $url
  26. * @return array
  27. */
  28. public function resetArray(array $url) {
  29. $url += $this->defaults();
  30. return $url;
  31. }
  32. /**
  33. * @param array $url
  34. * @return array
  35. */
  36. public function completeArray(array $url) {
  37. $url = $this->addQueryStrings($url);
  38. $url = $this->addPassed($url);
  39. return $url;
  40. }
  41. /**
  42. * Creates a reset URL.
  43. * The prefix and plugin params are resetting to default false.
  44. *
  45. * Can only add defaults for array URLs.
  46. *
  47. * @param string|array|null $url URL.
  48. * @param bool $full If true, the full base URL will be prepended to the result
  49. * @return string Full translated URL with base path.
  50. */
  51. public function buildReset($url = null, $full = false) {
  52. if (is_array($url)) {
  53. $url += $this->defaults();
  54. }
  55. return $this->build($url, $full);
  56. }
  57. /**
  58. * Returns a URL based on provided parameters.
  59. *
  60. * Can only add query strings for array URLs.
  61. *
  62. * @param string|array|null $url URL.
  63. * @param bool $full If true, the full base URL will be prepended to the result
  64. * @return string Full translated URL with base path.
  65. */
  66. public function buildComplete($url = null, $full = false) {
  67. if (is_array($url)) {
  68. $url = $this->addQueryStrings($url);
  69. }
  70. return $this->build($url, $full);
  71. }
  72. /**
  73. * @return array
  74. */
  75. public function defaults() {
  76. return [
  77. 'prefix' => false,
  78. 'plugin' => false,
  79. ];
  80. }
  81. /**
  82. * @param array $url
  83. *
  84. * @return array
  85. */
  86. protected function addQueryStrings(array $url) {
  87. if (!isset($url['?'])) {
  88. $url['?'] = [];
  89. }
  90. $url['?'] += $this->_View->getRequest()->getQuery();
  91. return $url;
  92. }
  93. /**
  94. * @param array $url
  95. *
  96. * @return array
  97. */
  98. protected function addPassed(array $url) {
  99. $pass = $this->_View->getRequest()->getParam('pass');
  100. $url = array_merge($url, $pass);
  101. return $url;
  102. }
  103. }