QueryParamsResolver.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Url;
  7. class QueryParamsResolver extends \Magento\Framework\DataObject implements QueryParamsResolverInterface
  8. {
  9. /**
  10. * {@inheritdoc}
  11. */
  12. public function getQuery($escape = false)
  13. {
  14. if (!$this->hasData('query')) {
  15. $query = '';
  16. $params = $this->getQueryParams();
  17. if (is_array($params)) {
  18. ksort($params);
  19. $query = http_build_query($params, '', $escape ? '&amp;' : '&');
  20. }
  21. $this->setData('query', $query);
  22. }
  23. return $this->_getData('query');
  24. }
  25. /**
  26. * {@inheritdoc}
  27. */
  28. public function setQuery($data)
  29. {
  30. if ($this->_getData('query') !== $data) {
  31. $this->unsetData('query_params');
  32. $this->setData('query', $data);
  33. }
  34. return $this;
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function setQueryParam($key, $data)
  40. {
  41. $params = $this->getQueryParams();
  42. if (isset($params[$key]) && $params[$key] == $data) {
  43. return $this;
  44. }
  45. $params[$key] = $data;
  46. $this->unsetData('query');
  47. $this->setData('query_params', $params);
  48. return $this;
  49. }
  50. /**
  51. * {@inheritdoc}
  52. */
  53. public function getQueryParams()
  54. {
  55. if (!$this->hasData('query_params')) {
  56. $params = [];
  57. if ($this->_getData('query')) {
  58. foreach (explode('&', $this->_getData('query')) as $param) {
  59. $paramArr = explode('=', $param);
  60. $params[$paramArr[0]] = urldecode($paramArr[1]);
  61. }
  62. }
  63. $this->setData('query_params', $params);
  64. }
  65. return $this->_getData('query_params');
  66. }
  67. /**
  68. * {@inheritdoc}
  69. */
  70. public function setQueryParams(array $data)
  71. {
  72. return $this->setData('query_params', $data);
  73. }
  74. /**
  75. * {@inheritdoc}
  76. */
  77. public function addQueryParams(array $data)
  78. {
  79. $this->unsetData('query');
  80. if ($this->_getData('query_params') == $data) {
  81. return $this;
  82. }
  83. $params = $this->_getData('query_params');
  84. if (!is_array($params)) {
  85. $params = [];
  86. }
  87. foreach ($data as $param => $value) {
  88. $params[$param] = $value;
  89. }
  90. $this->setData('query_params', $params);
  91. return $this;
  92. }
  93. }