Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. ServerRequestCreator.php

function ServerRequestCreator::fromArrays

Overrides ServerRequestCreatorInterface::fromArrays

1 call to ServerRequestCreator::fromArrays()
ServerRequestCreator::fromGlobals in vendor/nyholm/psr7-server/src/ServerRequestCreator.php
Create a new server request from the current environment variables. Defaults to a GET request to minimise the risk of an \InvalidArgumentException. Includes the current request headers as supplied by the server through `getallheaders()`. If…

File

vendor/nyholm/psr7-server/src/ServerRequestCreator.php, line 77

Class

ServerRequestCreator
@author Tobias Nyholm <tobias.nyholm@gmail.com> @author Martijn van der Ven <martijn@vanderven.se>

Namespace

Nyholm\Psr7Server

Code

public function fromArrays(array $server, array $headers = [], array $cookie = [], array $get = [], ?array $post = null, array $files = [], $body = null) : ServerRequestInterface {
    $method = $this->getMethodFromEnv($server);
    $uri = $this->getUriFromEnvWithHTTP($server);
    $protocol = isset($server['SERVER_PROTOCOL']) ? \str_replace('HTTP/', '', $server['SERVER_PROTOCOL']) : '1.1';
    $serverRequest = $this->serverRequestFactory
        ->createServerRequest($method, $uri, $server);
    foreach ($headers as $name => $value) {
        // Because PHP automatically casts array keys set with numeric strings to integers, we have to make sure
        // that numeric headers will not be sent along as integers, as withAddedHeader can only accept strings.
        if (\is_int($name)) {
            $name = (string) $name;
        }
        $serverRequest = $serverRequest->withAddedHeader($name, $value);
    }
    $serverRequest = $serverRequest->withProtocolVersion($protocol)
        ->withCookieParams($cookie)
        ->withQueryParams($get)
        ->withParsedBody($post)
        ->withUploadedFiles($this->normalizeFiles($files));
    if (null === $body) {
        return $serverRequest;
    }
    if (\is_resource($body)) {
        $body = $this->streamFactory
            ->createStreamFromResource($body);
    }
    elseif (\is_string($body)) {
        $body = $this->streamFactory
            ->createStream($body);
    }
    elseif (!$body instanceof StreamInterface) {
        throw new \InvalidArgumentException('The $body parameter to ServerRequestCreator::fromArrays must be string, resource or StreamInterface');
    }
    return $serverRequest->withBody($body);
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal