Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
<rule ref="Generic.Files.OneObjectStructurePerFile" />
<rule ref="Generic.Files.OneTraitPerFile" />
<rule ref="Generic.Formatting.DisallowMultipleStatements" />
<rule ref="Generic.Formatting.NoSpaceAfterCast" />
<rule ref="Generic.Functions.FunctionCallArgumentSpacing" />
<rule ref="Generic.Functions.OpeningFunctionBraceKernighanRitchie" />
<rule ref="Generic.Metrics.CyclomaticComplexity" />
Expand Down
17 changes: 17 additions & 0 deletions src/Input.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand All @@ -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);
Expand All @@ -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:
Expand Down
26 changes: 26 additions & 0 deletions test/phpunit/InputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down