From 006e6b279a4a15e724bca6468099c0f8ca6bea30 Mon Sep 17 00:00:00 2001 From: BMTmohammedtaha Date: Tue, 25 Jul 2023 16:41:08 +0200 Subject: [PATCH] Your descriptive commit message here --- composer.json | 2 +- src/RequestFoundation.php | 55 ++-------------------- src/StreamFoundation.php | 4 ++ src/UploadedFileFoundation.php | 86 +++++++--------------------------- 4 files changed, 27 insertions(+), 120 deletions(-) diff --git a/composer.json b/composer.json index c3e0a61..658b0f8 100644 --- a/composer.json +++ b/composer.json @@ -18,6 +18,6 @@ "require": { "psr/http-message": "^1.1 || ^2.0", "psr/http-server-handler": "^1.0", - "effectra/http-message":"1.0" + "effectra/http-message":"^1.0" } } diff --git a/src/RequestFoundation.php b/src/RequestFoundation.php index 459de76..570f451 100644 --- a/src/RequestFoundation.php +++ b/src/RequestFoundation.php @@ -5,31 +5,13 @@ namespace Effectra\Http\Foundation; use Effectra\Http\Message\ServerRequest; -use Effectra\Http\Message\UploadedFile; use Psr\Http\Message\ServerRequestInterface; /** * Represents the Request Foundation implementation. */ -class RequestFoundation +class RequestFoundation { - /** - * RequestFoundation constructor. - * - * @param array $getParams The GET parameters. - * @param array $postParams The POST parameters. - * @param array $cookies The cookies. - * @param array $files The uploaded files. - * @param array $server The server variables. - */ - public function __construct( - public array $getParams, - public array $postParams, - public array $cookies, - public array $files, - public array $server - ) { - } /** * Creates a ServerRequestInterface instance from global variables. @@ -43,20 +25,12 @@ public static function createFromGlobals(): ServerRequestInterface $uri = UriFoundation::getFromGlobals(); $body = StreamFoundation::create(); $protocol = isset($_SERVER['SERVER_PROTOCOL']) ? str_replace('HTTP/', '', $_SERVER['SERVER_PROTOCOL']) : '1.1'; + $files = UploadedFileFoundation::createFromGlobals(); - $serverRequest = new ServerRequest( - method: $method, - uri: $uri, - headers: $headers, - body: $body, - queryParams: $_GET, - parsedBody: $_POST, - protocolVersion: $protocol, - attributes: $_SERVER, - ); + $serverRequest = new ServerRequest($method, $uri, $headers, $body, $_GET, $_POST, $protocol, $_SERVER); $serverRequest ->withCookieParams($_COOKIE) - ->withUploadedFiles(self::normalizeFiles($_FILES)); + ->withUploadedFiles($files); return $serverRequest; } @@ -77,25 +51,4 @@ private static function getHeaders(): array } return $headers; } - - /** - * Normalizes the uploaded files array. - * - * @param array $files The uploaded files array. - * @return array The normalized files array. - */ - private static function normalizeFiles(array $files): array - { - $normalizedFiles = []; - foreach ($files as $key => $file) { - $normalizedFiles[$key] = new UploadedFile( - $file['tmp_name'], - $file['size'], - $file['error'], - $file['name'], - $file['type'] - ); - } - return $normalizedFiles; - } } diff --git a/src/StreamFoundation.php b/src/StreamFoundation.php index ee61a24..210d01c 100644 --- a/src/StreamFoundation.php +++ b/src/StreamFoundation.php @@ -13,6 +13,7 @@ * * @package Effectra\Http\Foundation */ + class StreamFoundation { /** @@ -23,8 +24,11 @@ class StreamFoundation public static function create(): Stream { $resource = fopen('php://temp', 'r+'); + fwrite($resource, file_get_contents('php://input')); + rewind($resource); + return new Stream($resource); } } diff --git a/src/UploadedFileFoundation.php b/src/UploadedFileFoundation.php index 1e0292c..cc574a0 100644 --- a/src/UploadedFileFoundation.php +++ b/src/UploadedFileFoundation.php @@ -4,78 +4,28 @@ namespace Effectra\Http\Foundation; -use Psr\Http\Message\StreamInterface; +use Effectra\Http\Message\UploadedFile; -/** - * Represents the Uploaded File Foundation implementation. - */ -class UploadedFileFoundation +class UploadedFileFoundation { /** - * UploadedFileFoundation constructor. + * Normalizes the uploaded files array. * - * @param StreamInterface $stream The underlying stream representing the uploaded file. - * @param int $size The size of the uploaded file in bytes. - * @param int $error The PHP file upload error code. - * @param string|null $clientFilename The client-provided filename of the uploaded file. - * @param string|null $clientMediaType The client-provided media type of the uploaded file. + * @param array $files The uploaded files array. + * @return array The normalized files array. */ - public function __construct( - private StreamInterface $stream, - private int $size, - private int $error, - private ?string $clientFilename = null, - private ?string $clientMediaType = null - ) { - } - - /** - * Retrieves the underlying stream representing the uploaded file. - * - * @return StreamInterface The underlying stream representing the uploaded file. - */ - public function getStream(): StreamInterface - { - return $this->stream; - } - - /** - * Retrieves the size of the uploaded file in bytes. - * - * @return int The size of the uploaded file in bytes. - */ - public function getSize(): int - { - return $this->size; - } - - /** - * Retrieves the PHP file upload error code. - * - * @return int The PHP file upload error code. - */ - public function getError(): int - { - return $this->error; - } - - /** - * Retrieves the client-provided filename of the uploaded file. - * - * @return string|null The client-provided filename of the uploaded file. - */ - public function getClientFilename(): ?string - { - return $this->clientFilename; - } - - /** - * Retrieves the client-provided media type of the uploaded file. - * - * @return string|null The client-provided media type of the uploaded file. - */ - public function getClientMediaType(): ?string + public static function createFromGlobals():array { - return $this->clientMediaType; + $normalizedFiles = []; + foreach ($_FILES as $key => $file) { + $normalizedFiles[$key] = new UploadedFile( + $file['tmp_name'], + $file['size'], + $file['error'], + $file['name'], + $file['type'] + ); + } + return $normalizedFiles; } -} +} \ No newline at end of file