You are here

function build_query in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 vendor/guzzlehttp/psr7/src/functions.php \GuzzleHttp\Psr7\build_query()

Build a query string from an array of key value pairs.

This function can use the return value of parseQuery() to build a query string. This function does not modify the provided keys when an array is encountered (like http_build_query would).

Parameters

array $params Query string parameters.:

int|false $encoding Set to false to not encode, PHP_QUERY_RFC3986: to encode using RFC3986, or PHP_QUERY_RFC1738 to encode using RFC1738.

Return value

string

File

vendor/guzzlehttp/psr7/src/functions.php, line 548

Namespace

GuzzleHttp\Psr7

Code

function build_query(array $params, $encoding = PHP_QUERY_RFC3986) {
  if (!$params) {
    return '';
  }
  if ($encoding === false) {
    $encoder = function ($str) {
      return $str;
    };
  }
  elseif ($encoding == PHP_QUERY_RFC3986) {
    $encoder = 'rawurlencode';
  }
  elseif ($encoding == PHP_QUERY_RFC1738) {
    $encoder = 'urlencode';
  }
  else {
    throw new \InvalidArgumentException('Invalid type');
  }
  $qs = '';
  foreach ($params as $k => $v) {
    $k = $encoder($k);
    if (!is_array($v)) {
      $qs .= $k;
      if ($v !== null) {
        $qs .= '=' . $encoder($v);
      }
      $qs .= '&';
    }
    else {
      foreach ($v as $vv) {
        $qs .= $k;
        if ($vv !== null) {
          $qs .= '=' . $encoder($vv);
        }
        $qs .= '&';
      }
    }
  }
  return $qs ? (string) substr($qs, 0, -1) : '';
}