UrlTrait.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace Tools\View\Helper;
  3. trait UrlTrait {
  4. /**
  5. * @param array $url
  6. * @return array
  7. */
  8. public function resetArray(array $url): array {
  9. $url += $this->defaults();
  10. return $url;
  11. }
  12. /**
  13. * @param array $url
  14. * @return array
  15. */
  16. public function completeArray(array $url): array {
  17. $url = $this->addQueryStrings($url);
  18. $url = $this->addPassed($url);
  19. return $url;
  20. }
  21. /**
  22. * Creates a reset URL.
  23. * The prefix and plugin params are resetting to default false.
  24. *
  25. * Can only add defaults for array URLs.
  26. *
  27. * @param string|array|null $url URL.
  28. * @param array $options
  29. * @return string Full translated URL with base path.
  30. */
  31. public function buildReset($url, array $options = []): string {
  32. if (is_array($url)) {
  33. $url += $this->defaults();
  34. }
  35. return $this->build($url, $options);
  36. }
  37. /**
  38. * Returns a URL based on provided parameters.
  39. *
  40. * Can only add query strings for array URLs.
  41. *
  42. * @param string|array|null $url URL.
  43. * @param array $options
  44. * @return string Full translated URL with base path.
  45. */
  46. public function buildComplete($url, array $options = []): string {
  47. if (is_array($url)) {
  48. $url = $this->addQueryStrings($url);
  49. }
  50. return $this->build($url, $options);
  51. }
  52. /**
  53. * @return array
  54. */
  55. public function defaults(): array {
  56. return [
  57. 'prefix' => false,
  58. 'plugin' => false,
  59. ];
  60. }
  61. /**
  62. * @param array $url
  63. *
  64. * @return array
  65. */
  66. protected function addQueryStrings(array $url): array {
  67. if (!isset($url['?'])) {
  68. $url['?'] = [];
  69. }
  70. $url['?'] += $this->_View->getRequest()->getQuery();
  71. return $url;
  72. }
  73. /**
  74. * @param array $url
  75. *
  76. * @return array
  77. */
  78. protected function addPassed(array $url) {
  79. $pass = $this->_View->getRequest()->getParam('pass');
  80. $url = array_merge($url, $pass);
  81. return $url;
  82. }
  83. }