You are here

function modify_request in Lockr 7.3

Clone and modify a request with the given changes.

The changes can be one of:

  • method: (string) Changes the HTTP method.
  • set_headers: (array) Sets the given headers.
  • remove_headers: (array) Remove the given headers.
  • body: (mixed) Sets the given body.
  • uri: (UriInterface) Set the URI.
  • query: (string) Set the query string value of the URI.
  • version: (string) Set the protocol version.

Parameters

RequestInterface $request Request to clone and modify.:

array $changes Changes to apply.:

Return value

RequestInterface

File

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

Namespace

GuzzleHttp\Psr7

Code

function modify_request(RequestInterface $request, array $changes) {
  if (!$changes) {
    return $request;
  }
  $headers = $request
    ->getHeaders();
  if (!isset($changes['uri'])) {
    $uri = $request
      ->getUri();
  }
  else {

    // Remove the host header if one is on the URI
    if ($host = $changes['uri']
      ->getHost()) {
      $changes['set_headers']['Host'] = $host;
      if ($port = $changes['uri']
        ->getPort()) {
        $standardPorts = [
          'http' => 80,
          'https' => 443,
        ];
        $scheme = $changes['uri']
          ->getScheme();
        if (isset($standardPorts[$scheme]) && $port != $standardPorts[$scheme]) {
          $changes['set_headers']['Host'] .= ':' . $port;
        }
      }
    }
    $uri = $changes['uri'];
  }
  if (!empty($changes['remove_headers'])) {
    $headers = _caseless_remove($changes['remove_headers'], $headers);
  }
  if (!empty($changes['set_headers'])) {
    $headers = _caseless_remove(array_keys($changes['set_headers']), $headers);
    $headers = $changes['set_headers'] + $headers;
  }
  if (isset($changes['query'])) {
    $uri = $uri
      ->withQuery($changes['query']);
  }
  if ($request instanceof ServerRequestInterface) {
    return (new ServerRequest(isset($changes['method']) ? $changes['method'] : $request
      ->getMethod(), $uri, $headers, isset($changes['body']) ? $changes['body'] : $request
      ->getBody(), isset($changes['version']) ? $changes['version'] : $request
      ->getProtocolVersion(), $request
      ->getServerParams()))
      ->withParsedBody($request
      ->getParsedBody())
      ->withQueryParams($request
      ->getQueryParams())
      ->withCookieParams($request
      ->getCookieParams())
      ->withUploadedFiles($request
      ->getUploadedFiles());
  }
  return new Request(isset($changes['method']) ? $changes['method'] : $request
    ->getMethod(), $uri, $headers, isset($changes['body']) ? $changes['body'] : $request
    ->getBody(), isset($changes['version']) ? $changes['version'] : $request
    ->getProtocolVersion());
}