diff --git a/phpcs.xml b/phpcs.xml
index b771656..ad96b86 100644
--- a/phpcs.xml
+++ b/phpcs.xml
@@ -25,7 +25,6 @@
-
diff --git a/src/Input.php b/src/Input.php
index bcb2c7a..33b450f 100644
--- a/src/Input.php
+++ b/src/Input.php
@@ -11,6 +11,7 @@
use Psr\Http\Message\StreamInterface;
use Gt\Input\Trigger\Trigger;
use Gt\Input\InputData\InputData;
+use Gt\Input\InputData\Datum\StreamNotAvailableException;
use Gt\Input\InputData\Datum\InputDatum;
use Gt\Input\InputData\KeyValueArrayAccess;
use Gt\Input\InputData\KeyValueCountable;
@@ -38,6 +39,7 @@ class Input implements ArrayAccess, Countable, Iterator {
const DATA_COMBINED = "combined";
protected BodyStream $bodyStream;
+ protected string $requestMethod;
protected QueryStringInputData $queryStringParameters;
protected BodyInputData $bodyParameters;
@@ -52,8 +54,10 @@ public function __construct(
array $post = [],
array $files = [],
string $bodyPath = "php://input",
+ ?string $requestMethod = "GET",
) {
$this->bodyStream = new BodyStream($bodyPath);
+ $this->requestMethod = strtoupper($requestMethod);
$this->queryStringParameters = new QueryStringInputData($get);
$this->bodyParameters = new BodyInputData($post);
@@ -73,6 +77,19 @@ public function getStream():StreamInterface {
return $this->bodyStream;
}
+ /**
+ * Returns a streamable PUT file upload body.
+ */
+ public function getPutFileStream():BodyStream {
+ if($this->requestMethod !== "PUT") {
+ throw new StreamNotAvailableException(
+ "PUT file stream is only available for PUT requests."
+ );
+ }
+
+ return $this->bodyStream;
+ }
+
public function add(string $key, InputDatum $datum, string $method):void {
switch($method) {
case self::DATA_QUERYSTRING:
diff --git a/test/phpunit/InputTest.php b/test/phpunit/InputTest.php
index 9ae0989..e6fbda5 100644
--- a/test/phpunit/InputTest.php
+++ b/test/phpunit/InputTest.php
@@ -6,6 +6,7 @@
use Gt\Input\Input;
use Gt\Input\InputData\Datum\FileUpload;
use Gt\Input\InputData\Datum\InputDatum;
+use Gt\Input\InputData\Datum\StreamNotAvailableException;
use Gt\Input\InputData\InputData;
use Gt\Input\InvalidInputMethodException;
use Gt\Input\MissingInputParameterException;
@@ -62,6 +63,31 @@ public function testBodyStreamContents():void {
self::assertEquals($testMessage, (string)$body);
}
+ public function testGetPutFileStream_putRequest():void {
+ $testMessage = "This is a PUT file test message";
+ $tmpPath = implode(DIRECTORY_SEPARATOR, [
+ sys_get_temp_dir(),
+ "phpgt",
+ "input",
+ "test",
+ uniqid(),
+ ]);
+ mkdir(dirname($tmpPath), 0775, true);
+ touch($tmpPath);
+ $fh = fopen($tmpPath, "r+");
+ fwrite($fh, $testMessage);
+
+ $input = new Input([], [], [], $tmpPath, "PUT");
+ $body = $input->getPutFileStream();
+ self::assertEquals($testMessage, (string)$body);
+ }
+
+ public function testGetPutFileStream_notPutRequest():void {
+ self::expectException(StreamNotAvailableException::class);
+ $input = new Input([], [], [], "php://input", "POST");
+ $input->getPutFileStream();
+ }
+
/** @dataProvider dataRandomGetPost */
public function testGetQueryString(array $get, array $post):void {
$input = new Input($get, $post);