Query.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace GuzzleHttp\Psr7;
  3. final class Query
  4. {
  5. /**
  6. * Parse a query string into an associative array.
  7. *
  8. * If multiple values are found for the same key, the value of that key
  9. * value pair will become an array. This function does not parse nested
  10. * PHP style arrays into an associative array (e.g., `foo[a]=1&foo[b]=2`
  11. * will be parsed into `['foo[a]' => '1', 'foo[b]' => '2'])`.
  12. *
  13. * @param string $str Query string to parse
  14. * @param int|bool $urlEncoding How the query string is encoded
  15. *
  16. * @return array
  17. */
  18. public static function parse($str, $urlEncoding = true)
  19. {
  20. $result = [];
  21. if ($str === '') {
  22. return $result;
  23. }
  24. if ($urlEncoding === true) {
  25. $decoder = function ($value) {
  26. return rawurldecode(str_replace('+', ' ', $value));
  27. };
  28. } elseif ($urlEncoding === PHP_QUERY_RFC3986) {
  29. $decoder = 'rawurldecode';
  30. } elseif ($urlEncoding === PHP_QUERY_RFC1738) {
  31. $decoder = 'urldecode';
  32. } else {
  33. $decoder = function ($str) {
  34. return $str;
  35. };
  36. }
  37. foreach (explode('&', $str) as $kvp) {
  38. $parts = explode('=', $kvp, 2);
  39. $key = $decoder($parts[0]);
  40. $value = isset($parts[1]) ? $decoder($parts[1]) : null;
  41. if (!isset($result[$key])) {
  42. $result[$key] = $value;
  43. } else {
  44. if (!is_array($result[$key])) {
  45. $result[$key] = [$result[$key]];
  46. }
  47. $result[$key][] = $value;
  48. }
  49. }
  50. return $result;
  51. }
  52. /**
  53. * Build a query string from an array of key value pairs.
  54. *
  55. * This function can use the return value of `parse()` to build a query
  56. * string. This function does not modify the provided keys when an array is
  57. * encountered (like `http_build_query()` would).
  58. *
  59. * @param array $params Query string parameters.
  60. * @param int|false $encoding Set to false to not encode, PHP_QUERY_RFC3986
  61. * to encode using RFC3986, or PHP_QUERY_RFC1738
  62. * to encode using RFC1738.
  63. *
  64. * @return string
  65. */
  66. public static function build(array $params, $encoding = PHP_QUERY_RFC3986)
  67. {
  68. if (!$params) {
  69. return '';
  70. }
  71. if ($encoding === false) {
  72. $encoder = function ($str) {
  73. return $str;
  74. };
  75. } elseif ($encoding === PHP_QUERY_RFC3986) {
  76. $encoder = 'rawurlencode';
  77. } elseif ($encoding === PHP_QUERY_RFC1738) {
  78. $encoder = 'urlencode';
  79. } else {
  80. throw new \InvalidArgumentException('Invalid type');
  81. }
  82. $qs = '';
  83. foreach ($params as $k => $v) {
  84. $k = $encoder($k);
  85. if (!is_array($v)) {
  86. $qs .= $k;
  87. if ($v !== null) {
  88. $qs .= '=' . $encoder($v);
  89. }
  90. $qs .= '&';
  91. } else {
  92. foreach ($v as $vv) {
  93. $qs .= $k;
  94. if ($vv !== null) {
  95. $qs .= '=' . $encoder($vv);
  96. }
  97. $qs .= '&';
  98. }
  99. }
  100. }
  101. return $qs ? (string) substr($qs, 0, -1) : '';
  102. }
  103. }