Skip to content

Commit 377480e

Browse files
committed
feature: PUT style BodyStream
closes #57
1 parent d15b0d3 commit 377480e

2 files changed

Lines changed: 45 additions & 0 deletions

File tree

src/Input.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Psr\Http\Message\StreamInterface;
1212
use Gt\Input\Trigger\Trigger;
1313
use Gt\Input\InputData\InputData;
14+
use Gt\Input\InputData\Datum\StreamNotAvailableException;
1415
use Gt\Input\InputData\Datum\InputDatum;
1516
use Gt\Input\InputData\KeyValueArrayAccess;
1617
use Gt\Input\InputData\KeyValueCountable;
@@ -38,6 +39,7 @@ class Input implements ArrayAccess, Countable, Iterator {
3839
const DATA_COMBINED = "combined";
3940

4041
protected BodyStream $bodyStream;
42+
protected string $requestMethod;
4143
protected QueryStringInputData $queryStringParameters;
4244
protected BodyInputData $bodyParameters;
4345

@@ -52,8 +54,12 @@ public function __construct(
5254
array $post = [],
5355
array $files = [],
5456
string $bodyPath = "php://input",
57+
?string $requestMethod = null,
5558
) {
5659
$this->bodyStream = new BodyStream($bodyPath);
60+
$this->requestMethod = strtoupper(
61+
$requestMethod ?? ($_SERVER["REQUEST_METHOD"] ?? "GET")
62+
);
5763

5864
$this->queryStringParameters = new QueryStringInputData($get);
5965
$this->bodyParameters = new BodyInputData($post);
@@ -73,6 +79,19 @@ public function getStream():StreamInterface {
7379
return $this->bodyStream;
7480
}
7581

82+
/**
83+
* Returns a streamable PUT file upload body.
84+
*/
85+
public function getPutFileStream():BodyStream {
86+
if($this->requestMethod !== "PUT") {
87+
throw new StreamNotAvailableException(
88+
"PUT file stream is only available for PUT requests."
89+
);
90+
}
91+
92+
return $this->bodyStream;
93+
}
94+
7695
public function add(string $key, InputDatum $datum, string $method):void {
7796
switch($method) {
7897
case self::DATA_QUERYSTRING:

test/phpunit/InputTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Gt\Input\Input;
77
use Gt\Input\InputData\Datum\FileUpload;
88
use Gt\Input\InputData\Datum\InputDatum;
9+
use Gt\Input\InputData\Datum\StreamNotAvailableException;
910
use Gt\Input\InputData\InputData;
1011
use Gt\Input\InvalidInputMethodException;
1112
use Gt\Input\MissingInputParameterException;
@@ -62,6 +63,31 @@ public function testBodyStreamContents():void {
6263
self::assertEquals($testMessage, (string)$body);
6364
}
6465

66+
public function testGetPutFileStream_putRequest():void {
67+
$testMessage = "This is a PUT file test message";
68+
$tmpPath = implode(DIRECTORY_SEPARATOR, [
69+
sys_get_temp_dir(),
70+
"phpgt",
71+
"input",
72+
"test",
73+
uniqid(),
74+
]);
75+
mkdir(dirname($tmpPath), 0775, true);
76+
touch($tmpPath);
77+
$fh = fopen($tmpPath, "r+");
78+
fwrite($fh, $testMessage);
79+
80+
$input = new Input([], [], [], $tmpPath, "PUT");
81+
$body = $input->getPutFileStream();
82+
self::assertEquals($testMessage, (string)$body);
83+
}
84+
85+
public function testGetPutFileStream_notPutRequest():void {
86+
self::expectException(StreamNotAvailableException::class);
87+
$input = new Input([], [], [], "php://input", "POST");
88+
$input->getPutFileStream();
89+
}
90+
6591
/** @dataProvider dataRandomGetPost */
6692
public function testGetQueryString(array $get, array $post):void {
6793
$input = new Input($get, $post);

0 commit comments

Comments
 (0)