Skip to content

Commit

Permalink
Merge pull request #45 from PhpGt/21-get-type-improvement
Browse files Browse the repository at this point in the history
get* type improvement
  • Loading branch information
g105b authored Apr 8, 2019
2 parents 727689b + 97e4e7b commit 53f0075
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 19 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
7 changes: 0 additions & 7 deletions src/InputData/InputData.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
<?php
namespace Gt\Input\InputData;

use DateTime;
use DateTimeInterface;
use Exception;
use Gt\Input\DataNotCompatibleFormatException;
use Gt\Input\DataNotFileUploadException;
use Gt\Input\InputData\Datum\FileUpload;
use Gt\Input\InputData\Datum\InputDatum;
use Gt\Input\InputData\Datum\MultipleInputDatum;
use Gt\Input\InputValueGetter;
use TypeError;

class InputData extends AbstractInputData {
use InputValueGetter;
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
13 changes: 10 additions & 3 deletions src/InputValueGetter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use DateTimeInterface;
use Exception;
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 +43,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 53f0075

Please sign in to comment.