Skip to content

Commit

Permalink
Improve return type of get* functions, closes #21
Browse files Browse the repository at this point in the history
  • Loading branch information
g105b committed Apr 8, 2019
1 parent 727689b commit e587469
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 12 deletions.
3 changes: 2 additions & 1 deletion src/Input.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,9 @@ public function add(string $key, InputDatum $datum, string $method):void {
* Get a particular input value by its key. To specify either GET or POST variables, pass
* Input::METHOD_GET or Input::METHOD_POST as the second parameter (defaults to
* Input::METHOD_BOTH).
* @return mixed|null
*/
public function get(string $key, string $method = null):?InputDatum {
public function get(string $key, string $method = null) {
if(is_null($method)) {
$method = self::DATA_COMBINED;
}
Expand Down
3 changes: 2 additions & 1 deletion src/InputData/AbstractInputData.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ abstract class AbstractInputData implements ArrayAccess, Countable, Iterator {
/** @var InputDatum[] */
protected $parameters = [];

public function get(string $key):?InputDatum {
/** @return mixed|null */
public function get(string $key) {
return $this->parameters[$key] ?? null;
}

Expand Down
2 changes: 0 additions & 2 deletions src/InputData/FileUploadInputData.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ class FileUploadInputData extends InputData {

public function __construct(array $files) {
$files = $this->normalizeArray($files);

// TODO: Set $this->parameters with kvp of files ($files[filename] => FileUpload(data))
$data = $this->createData($files);
parent::__construct($data);
}
Expand Down
15 changes: 13 additions & 2 deletions src/InputData/KeyValueArrayAccess.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
<?php
namespace Gt\Input\InputData;

use Gt\Input\Input;
use Gt\Input\InputData\Datum\InputDatum;

trait KeyValueArrayAccess {
public function offsetExists($offset):bool {
return isset($this->parameters[$offset]);
}

public function offsetGet($offset):?InputDatum {
return $this->get($offset);
/** @return mixed|null */
public function offsetGet($offset) {
if($this instanceof FileUploadInputData) {
return $this->getFile($offset);
}
elseif($this instanceof Input || $this instanceof InputData) {
if($this->contains($offset)) {
return $this->get($offset);
}
}

return null;
}

public function offsetSet($offset, $value):void {
Expand Down
14 changes: 11 additions & 3 deletions src/InputValueGetter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
use DateTime;
use DateTimeInterface;
use Exception;
use Gt\Input\InputData\CombinedInputData;
use Gt\Input\InputData\Datum\FileUpload;
use Gt\Input\InputData\Datum\InputDatum;
use Gt\Input\InputData\Datum\MultipleInputDatum;
use Gt\Input\InputData\FileUploadInputData;
use TypeError;
Expand Down Expand Up @@ -42,12 +44,18 @@ public function getBool(string $key):?bool {
}

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

try {
/** @var MultipleInputDatum $datum */
$datum = $params[$key];
return $datum->current();
/** @var MultipleInputDatum|FileUpload $file */
$file = $params[$key];

if($file instanceof MultipleInputDatum) {
return $file->current();
}

return $file;
}
catch(TypeError $exception) {
throw new DataNotFileUploadException($key);
Expand Down
4 changes: 1 addition & 3 deletions test/unit/InputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,7 @@ public function testGetPostField(array $get, array $post):void {
public function testGetFileFieldSingle(array $get, array $post):void {
$files = self::FAKE_FILE;
$input = new Input($get, $post, $files);
$file = $input->get("exampleFile", Input::DATA_FILES);

/** @var FileUpload $file */
$file = $input->getFile("exampleFile");

self::assertInstanceOf(
FileUpload::class,
Expand Down

0 comments on commit e587469

Please sign in to comment.