Skip to content

Commit

Permalink
feature: make getFile consistently nullable
Browse files Browse the repository at this point in the history
closes #287
  • Loading branch information
g105b committed Apr 25, 2024
1 parent 7e82009 commit d8d33ff
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/InputValueGetter.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,18 @@ public function getMultipleBool(string $key):array {
}


public function getFile(string $key):FileUpload {
public function getFile(string $key):?FileUpload {
/** @var FileUploadInputData|InputDatum[] $params */
$params = $this->fileUploadParameters ?? $this->parameters;

try {
/** @var MultipleInputDatum|FileUpload $file */
$file = $params[$key];
/** @var MultipleInputDatum|FileUpload|null $file */
$file = $params[$key] ?? null;

if(is_null($file)) {
return null;
}

try {
if($file instanceof MultipleInputDatum) {
return $file->current();
}
Expand Down
6 changes: 6 additions & 0 deletions test/phpunit/InputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,12 @@ public function testGetFileFieldSingle(array $get, array $post):void {
);
}

public function testGetFile_null():void {
$input = new Input([], [], []);
$file = $input->getFile("exampleFile");
self::assertNull($file);
}

/** @dataProvider dataRandomGetPost */
public function testGetInvalidDataType(array $get, array $post):void {
self::expectException(InvalidInputMethodException::class);
Expand Down

0 comments on commit d8d33ff

Please sign in to comment.