Query.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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) { return $str; };
  34. }
  35. foreach (explode('&', $str) as $kvp) {
  36. $parts = explode('=', $kvp, 2);
  37. $key = $decoder($parts[0]);
  38. $value = isset($parts[1]) ? $decoder($parts[1]) : null;
  39. if (!isset($result[$key])) {
  40. $result[$key] = $value;
  41. } else {
  42. if (!is_array($result[$key])) {
  43. $result[$key] = [$result[$key]];
  44. }
  45. $result[$key][] = $value;
  46. }
  47. }
  48. return $result;
  49. }
  50. /**
  51. * Build a query string from an array of key value pairs.
  52. *
  53. * This function can use the return value of `parse()` to build a query
  54. * string. This function does not modify the provided keys when an array is
  55. * encountered (like `http_build_query()` would).
  56. *
  57. * @param array $params Query string parameters.
  58. * @param int|false $encoding Set to false to not encode, PHP_QUERY_RFC3986
  59. * to encode using RFC3986, or PHP_QUERY_RFC1738
  60. * to encode using RFC1738.
  61. * @return string
  62. */
  63. public static function build(array $params, $encoding = PHP_QUERY_RFC3986)
  64. {
  65. if (!$params) {
  66. return '';
  67. }
  68. if ($encoding === false) {
  69. $encoder = function ($str) { return $str; };
  70. } elseif ($encoding === PHP_QUERY_RFC3986) {
  71. $encoder = 'rawurlencode';
  72. } elseif ($encoding === PHP_QUERY_RFC1738) {
  73. $encoder = 'urlencode';
  74. } else {
  75. throw new \InvalidArgumentException('Invalid type');
  76. }
  77. $qs = '';
  78. foreach ($params as $k => $v) {
  79. $k = $encoder($k);
  80. if (!is_array($v)) {
  81. $qs .= $k;
  82. if ($v !== null) {
  83. $qs .= '=' . $encoder($v);
  84. }
  85. $qs .= '&';
  86. } else {
  87. foreach ($v as $vv) {
  88. $qs .= $k;
  89. if ($vv !== null) {
  90. $qs .= '=' . $encoder($vv);
  91. }
  92. $qs .= '&';
  93. }
  94. }
  95. }
  96. return $qs ? (string) substr($qs, 0, -1) : '';
  97. }
  98. }