httpsoft / http-server-request
Infrastructure for creating PSR-7 ServerRequest and UploadedFile
Installs: 60 671
Dependents: 32
Suggesters: 0
Security: 0
Stars: 13
Watchers: 3
Forks: 0
Open Issues: 0
Requires
- php: ^7.4|^8.0
- httpsoft/http-message: ^1.1
Requires (Dev)
- phpunit/phpunit: ^9.5
- squizlabs/php_codesniffer: ^3.7
- vimeo/psalm: ^4.9|^5.2
Provides
README
This package makes it easy and flexible to create PSR-7 components ServerRequest and UploadedFile.
Depends on the httpsoft/http-message package.
Documentation
Installation
This package requires PHP version 7.4 or later.
composer require httpsoft/http-server-request
Usage ServerRequestCreator
use HttpSoft\ServerRequest\ServerRequestCreator; // All necessary data will be received automatically: $request = ServerRequestCreator::createFromGlobals($_SERVER, $_FILES, $_COOKIE, $_GET, $_POST); // equivalently to: $request = ServerRequestCreator::createFromGlobals(); // equivalently to: $request = ServerRequestCreator::create();
By default HttpSoft\ServerRequest\SapiNormalizer is used for normalization of server parameters. You can use your own server parameters normalizer, for this you need to implement an HttpSoft\ServerRequest\ServerNormalizerInterface interface.
$normalizer = new YouCustomServerNormalizer(); $request = ServerRequestCreator::create($normalizer); // equivalently to: $request = ServerRequestCreator::createFromGlobals($_SERVER, $_FILES, $_COOKIE, $_GET, $_POST, $normalizer); // or with custom superglobals: $request = ServerRequestCreator::createFromGlobals($server, $files, $cookie, $get, $post, $normalizer);
Usage UploadedFileCreator
use HttpSoft\ServerRequest\UploadedFileCreator; /** @var StreamInterface|string|resource $streamOrFile */ $uploadedFile = UploadedFileCreator::create($streamOrFile, 1024, UPLOAD_ERR_OK, 'file.txt', 'text/plain'); // Create a new `HttpSoft\UploadedFile\UploadedFile` instance from array (the item `$_FILES`) $uploadedFile = UploadedFileCreator::createFromArray([ 'name' => 'filename.jpg', // optional 'type' => 'image/jpeg', // optional 'tmp_name' => '/tmp/php/php6hst32', 'error' => 0, // UPLOAD_ERR_OK 'size' => 98174, ]); // Normalizes the superglobal structure and converts each array // value to an instance of `Psr\Http\Message\UploadedFileInterface`. $uploadedFiles = UploadedFileCreator::createFromGlobals($_FILES);