You are here

public static function ServerRequestFactory::marshalUriFromServer in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/zendframework/zend-diactoros/src/ServerRequestFactory.php \Zend\Diactoros\ServerRequestFactory::marshalUriFromServer()

Marshal the URI from the $_SERVER array and headers

Parameters

array $server:

array $headers:

Return value

Uri

1 call to ServerRequestFactory::marshalUriFromServer()
ServerRequestFactory::fromGlobals in vendor/zendframework/zend-diactoros/src/ServerRequestFactory.php
Create a request from the supplied superglobal values.

File

vendor/zendframework/zend-diactoros/src/ServerRequestFactory.php, line 232

Class

ServerRequestFactory
Class for marshaling a request object from the current PHP environment.

Namespace

Zend\Diactoros

Code

public static function marshalUriFromServer(array $server, array $headers) {
  $uri = new Uri('');

  // URI scheme
  $scheme = 'http';
  $https = self::get('HTTPS', $server);
  if ($https && 'off' !== $https || self::getHeader('x-forwarded-proto', $headers, false) === 'https') {
    $scheme = 'https';
  }
  if (!empty($scheme)) {
    $uri = $uri
      ->withScheme($scheme);
  }

  // Set the host
  $accumulator = (object) [
    'host' => '',
    'port' => null,
  ];
  self::marshalHostAndPortFromHeaders($accumulator, $server, $headers);
  $host = $accumulator->host;
  $port = $accumulator->port;
  if (!empty($host)) {
    $uri = $uri
      ->withHost($host);
    if (!empty($port)) {
      $uri = $uri
        ->withPort($port);
    }
  }

  // URI path
  $path = self::marshalRequestUri($server);
  $path = self::stripQueryString($path);

  // URI query
  $query = '';
  if (isset($server['QUERY_STRING'])) {
    $query = ltrim($server['QUERY_STRING'], '?');
  }
  return $uri
    ->withPath($path)
    ->withQuery($query);
}