You are here

private function Client::applyOptions in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/guzzlehttp/guzzle/src/Client.php \GuzzleHttp\Client::applyOptions()

Applies the array of request options to a request.

Parameters

RequestInterface $request:

array $options:

Return value

RequestInterface

1 call to Client::applyOptions()
Client::transfer in vendor/guzzlehttp/guzzle/src/Client.php
Transfers the given request and applies request options.

File

vendor/guzzlehttp/guzzle/src/Client.php, line 282

Class

Client
@method ResponseInterface get($uri, array $options = []) @method ResponseInterface head($uri, array $options = []) @method ResponseInterface put($uri, array $options = []) @method ResponseInterface post($uri, array $options = []) @method…

Namespace

GuzzleHttp

Code

private function applyOptions(RequestInterface $request, array &$options) {
  $modify = [];
  if (isset($options['form_params'])) {
    if (isset($options['multipart'])) {
      throw new \InvalidArgumentException('You cannot use ' . 'form_params and multipart at the same time. Use the ' . 'form_params option if you want to send application/' . 'x-www-form-urlencoded requests, and the multipart ' . 'option to send multipart/form-data requests.');
    }
    $options['body'] = http_build_query($options['form_params'], null, '&');
    unset($options['form_params']);
    $options['_conditional']['Content-Type'] = 'application/x-www-form-urlencoded';
  }
  if (isset($options['multipart'])) {
    $elements = $options['multipart'];
    unset($options['multipart']);
    $options['body'] = new Psr7\MultipartStream($elements);
  }
  if (!empty($options['decode_content']) && $options['decode_content'] !== true) {
    $modify['set_headers']['Accept-Encoding'] = $options['decode_content'];
  }
  if (isset($options['headers'])) {
    if (isset($modify['set_headers'])) {
      $modify['set_headers'] = $options['headers'] + $modify['set_headers'];
    }
    else {
      $modify['set_headers'] = $options['headers'];
    }
    unset($options['headers']);
  }
  if (isset($options['body'])) {
    if (is_array($options['body'])) {
      $this
        ->invalidBody();
    }
    $modify['body'] = Psr7\stream_for($options['body']);
    unset($options['body']);
  }
  if (!empty($options['auth'])) {
    $value = $options['auth'];
    $type = is_array($value) ? isset($value[2]) ? strtolower($value[2]) : 'basic' : $value;
    $config['auth'] = $value;
    switch (strtolower($type)) {
      case 'basic':
        $modify['set_headers']['Authorization'] = 'Basic ' . base64_encode("{$value[0]}:{$value[1]}");
        break;
      case 'digest':

        // @todo: Do not rely on curl
        $options['curl'][CURLOPT_HTTPAUTH] = CURLAUTH_DIGEST;
        $options['curl'][CURLOPT_USERPWD] = "{$value[0]}:{$value[1]}";
        break;
    }
  }
  if (isset($options['query'])) {
    $value = $options['query'];
    if (is_array($value)) {
      $value = http_build_query($value, null, '&', PHP_QUERY_RFC3986);
    }
    if (!is_string($value)) {
      throw new \InvalidArgumentException('query must be a string or array');
    }
    $modify['query'] = $value;
    unset($options['query']);
  }
  if (isset($options['json'])) {
    $modify['body'] = Psr7\stream_for(json_encode($options['json']));
    $options['_conditional']['Content-Type'] = 'application/json';
    unset($options['json']);
  }
  $request = Psr7\modify_request($request, $modify);
  if ($request
    ->getBody() instanceof Psr7\MultipartStream) {

    // Use a multipart/form-data POST if a Content-Type is not set.
    $options['_conditional']['Content-Type'] = 'multipart/form-data; boundary=' . $request
      ->getBody()
      ->getBoundary();
  }

  // Merge in conditional headers if they are not present.
  if (isset($options['_conditional'])) {

    // Build up the changes so it's in a single clone of the message.
    $modify = [];
    foreach ($options['_conditional'] as $k => $v) {
      if (!$request
        ->hasHeader($k)) {
        $modify['set_headers'][$k] = $v;
      }
    }
    $request = Psr7\modify_request($request, $modify);

    // Don't pass this internal value along to middleware/handlers.
    unset($options['_conditional']);
  }
  return $request;
}