# Class HttpSoft\ServerRequest\ServerRequestCreator

Helper for creating HttpSoft\Message\ServerRequest.

Source code on GitHub.

use HttpSoft\ServerRequest\ServerRequestCreator;

// Instance of `HttpSoft\Message\ServerRequest` with empty data:
$request = ServerRequestCreator::createFromGlobals([], [], [], [], []);

// Instance of `HttpSoft\Message\ServerRequest` with data taken from superglobal arrays:
$request = ServerRequestCreator::createFromGlobals($_SERVER, $_FILES, $_COOKIE, $_GET, $_POST);
// equivalently to:
$request = ServerRequestCreator::createFromGlobals(null, null, null, null, null);
// equivalently to:
$request = ServerRequestCreator::createFromGlobals();
// equivalently to:
$request = ServerRequestCreator::create();

# Public methods

# create

Creates a new HttpSoft\Message\ServerRequest instance from superglobals.

public static function create(ServerNormalizerInterface $normalizer = null): ServerRequestInterface;

See the createFromGlobals() method, since this method is a wrapper over it.

# createFromGlobals

Creates a new HttpSoft\Message\ServerRequest instance from superglobals.

public static function createFromGlobals(
    array $server = null,
    array $files = null,
    array $cookie = null,
    array $get = null,
    array $post = null,
    ServerNormalizerInterface $normalizer = null
): ServerRequestInterface;

If any of the arrays ($server, $files, $cookie, $get, $post) was not specified or is null, values from superglobals ($_SERVER, $_FILES, $_COOKIE, $_GET, $_POST) will be taken, but if an array was specified (even empty), it will be used.

$request = ServerRequestCreator::createFromGlobals([], null, [], null, []);

$request->getServerParams(); // []
$request->getUploadedFiles(); // $_FILES
$request->getCookieParams(); // []
$request->getQueryParams(); // $_GET
$request->getParsedBody(); // []

The $normalizer is responsible for normalizing the data needed to create a HttpSoft\Message\ServerRequest.

$request = ServerRequestCreator::createFromGlobals();

$request->getMethod(); // 'GET'
$request->getProtocolVersion(); // '1.1'
$request->getBody()->getContents(); // 'Page content'
$request->getBody()->getMetadata('uri'); // 'php://input'
(string) $request->getUri(); // 'http://example.com/path'
$request->getHeaders(); // ['Host' => ['example.com'], 'Content-Type' => ['text/html']]

$request->getServerParams(); // $_SERVER
$request->getUploadedFiles(); // $_FILES
$request->getCookieParams(); // $_COOKIE
$request->getQueryParams(); // $_GET
$request->getParsedBody(); // $_POST
$request->getAttributes(); // []

If $normalizer was not specified or is null, HttpSoft\ServerRequest\SapiNormalizer will be used, which normalizes data from PHP SAPI. If HttpSoft\ServerRequest\SapiNormalizer is not suitable for you, then create your own by implementing HttpSoft\ServerRequest\ServerNormalizerInterface interface.