Skip to content

Commit

Permalink
Your descriptive commit message here
Browse files Browse the repository at this point in the history
  • Loading branch information
BMTmohammedtaha committed Jul 25, 2023
1 parent 025b581 commit 006e6b2
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 120 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
55 changes: 4 additions & 51 deletions src/RequestFoundation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
}
Expand All @@ -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;
}
}
4 changes: 4 additions & 0 deletions src/StreamFoundation.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*
* @package Effectra\Http\Foundation
*/

class StreamFoundation
{
/**
Expand All @@ -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);
}
}
86 changes: 18 additions & 68 deletions src/UploadedFileFoundation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}

0 comments on commit 006e6b2

Please sign in to comment.