From 8b80e29167eb93c36c1b58898421c88a4b976eb6 Mon Sep 17 00:00:00 2001 From: Greg Bowler Date: Thu, 22 Feb 2018 19:20:38 +0000 Subject: [PATCH 01/21] Tidy and speed up tests --- test/unit/Helper/Helper.php | 33 +++++++++++++++++++++++++++++++++ test/unit/InputTest.php | 17 +++++++++++++---- test/unit/TriggerTest.php | 2 +- 3 files changed, 47 insertions(+), 5 deletions(-) diff --git a/test/unit/Helper/Helper.php b/test/unit/Helper/Helper.php index 2f7a9f2..30c44ca 100644 --- a/test/unit/Helper/Helper.php +++ b/test/unit/Helper/Helper.php @@ -3,6 +3,8 @@ use Gt\Input\Input; use Gt\Input\InputData; +use RecursiveDirectoryIterator; +use RecursiveIteratorIterator; class Helper { public static function getRandomKvp(int $num, string $prefix = ""):array { @@ -70,4 +72,35 @@ public static function getKeysFromInput(Input $input, int $num):array { return $keys; } + + public static function cleanUp() { + $testDirectory = Helper::getTestDirectory(); + + if(!is_dir($testDirectory)) { + return; + } + + $files = new RecursiveIteratorIterator( + new RecursiveDirectoryIterator( + $testDirectory, + RecursiveDirectoryIterator::SKIP_DOTS), + RecursiveIteratorIterator::CHILD_FIRST + ); + + foreach ($files as $fileInfo) { + $action = ($fileInfo->isDir() ? 'rmdir' : 'unlink'); + $action($fileInfo->getRealPath()); + } + + rmdir($testDirectory); + } + + public static function getTestDirectory() { + return implode(DIRECTORY_SEPARATOR, [ + sys_get_temp_dir(), + "phpgt", + "input", + "test", + ]); + } } \ No newline at end of file diff --git a/test/unit/InputTest.php b/test/unit/InputTest.php index 7b4dfe7..ad1c18e 100644 --- a/test/unit/InputTest.php +++ b/test/unit/InputTest.php @@ -7,12 +7,21 @@ use Gt\Input\Test\Helper\Helper; use Gt\Input\Trigger; use PHPUnit\Framework\TestCase; -use StdClass; class InputTest extends TestCase { + public function tearDown() { + Helper::cleanUp(); + } + public function testBodyStreamContents():void { $testMessage = "This is a test message"; - $tmpPath = "/tmp/phpgt/input/test/" . uniqid(); + $tmpPath = implode(DIRECTORY_SEPARATOR, [ + sys_get_temp_dir(), + "phpgt", + "input", + "test", + uniqid(), + ]); mkdir(dirname($tmpPath), 0775, true); touch($tmpPath); $fh = fopen($tmpPath, "r+"); @@ -312,7 +321,7 @@ public function testUnsettingOwnData(array $get, array $post):void { public function dataRandomGetPost():array { $data = []; - for($i = 0; $i < 100; $i++) { + for($i = 0; $i < 10; $i++) { $params = [ Helper::getRandomKvp(rand(10, 100), "get-"), Helper::getRandomKvp(rand(10, 100), "post-"), @@ -326,7 +335,7 @@ public function dataRandomGetPost():array { public function dataRandomString():array { $data = []; - for($i = 0; $i < 100; $i++) { + for($i = 0; $i < 10; $i++) { $params = [ uniqid(), ]; diff --git a/test/unit/TriggerTest.php b/test/unit/TriggerTest.php index 22f337e..b4cc2b7 100644 --- a/test/unit/TriggerTest.php +++ b/test/unit/TriggerTest.php @@ -249,7 +249,7 @@ public function testFiresWithNoMatches(Input $input):void { public function dataInput():array { $data = []; - for($i = 0; $i < 100; $i++) { + for($i = 0; $i < 10; $i++) { $params = []; $getData = Helper::getRandomKvp(rand(10, 100), "get-"); From 367aa7702eacbbf411e24e3d1bf2284db86d86df Mon Sep 17 00:00:00 2001 From: Greg Bowler Date: Thu, 22 Feb 2018 21:41:10 +0000 Subject: [PATCH 02/21] Iterable to allow for UploadData --- src/InputData.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/InputData.php b/src/InputData.php index da66406..23c078c 100644 --- a/src/InputData.php +++ b/src/InputData.php @@ -10,7 +10,7 @@ class InputData implements ArrayAccess, Countable, Iterator { use InputDataCountable; use InputDataIterator; - public function __construct(array...$sources) { + public function __construct(iterable...$sources) { $this->data = []; foreach($sources as $source) { From f8c6e2271d26dd85e78aefe0b1b21ad48b38353a Mon Sep 17 00:00:00 2001 From: Greg Bowler Date: Thu, 22 Feb 2018 22:14:22 +0000 Subject: [PATCH 03/21] Combine uploaded files into Input's collections --- src/{Body.php => BodyStream.php} | 2 +- src/Input.php | 125 +++++++++++++++++++++++------ src/InputDatum.php | 5 ++ src/{Upload.php => UploadData.php} | 2 +- test/unit/InputTest.php | 6 +- 5 files changed, 111 insertions(+), 29 deletions(-) rename src/{Body.php => BodyStream.php} (58%) create mode 100644 src/InputDatum.php rename src/{Upload.php => UploadData.php} (80%) diff --git a/src/Body.php b/src/BodyStream.php similarity index 58% rename from src/Body.php rename to src/BodyStream.php index e731ceb..b7eef95 100644 --- a/src/Body.php +++ b/src/BodyStream.php @@ -3,4 +3,4 @@ use Gt\Http\Stream; -class Body extends Stream {} \ No newline at end of file +class BodyStream extends Stream {} \ No newline at end of file diff --git a/src/Input.php b/src/Input.php index 5b031b2..85250a1 100644 --- a/src/Input.php +++ b/src/Input.php @@ -12,18 +12,19 @@ class Input implements ArrayAccess, Countable, Iterator { use InputDataIterator; const DATA_QUERYSTRING = "get"; - const DATA_POSTFIELDS = "post"; + const DATA_BODY = "post"; + const DATA_FILES = "files"; const DATA_COMBINED = "both"; - /** @var Body */ - protected $body; + /** @var BodyStream */ + protected $bodyStream; + /** @var UploadData */ + protected $uploadedFileParameters; /** @var InputData */ protected $queryStringParameters; /** @var InputData */ - protected $postFields; - /** @var Upload */ - protected $files; + protected $bodyParameters; public function __construct( array $get = [], @@ -31,11 +32,13 @@ public function __construct( array $files = [], string $bodyPath = "php://input" ) { - $this->body = new Body($bodyPath); + $this->bodyStream = new BodyStream($bodyPath); + // TODO: The following three variables can extend the same base class. AbstractInputData? $this->queryStringParameters = new InputData($get); - $this->postFields = new InputData($post); - $this->files = new Upload($files); - $this->data = new InputData($get, $post); + $this->bodyParameters = new InputData($post); + $this->uploadedFileParameters = new UploadData($files); + + $this->data = new InputData($get, $post, $this->uploadedFileParameters); $this->dataKeys = $this->data->getKeys(); } @@ -43,7 +46,7 @@ public function __construct( * Returns the input payload as a streamable HTTP request body. */ public function getStream():StreamInterface { - return $this->body; + return $this->bodyStream; } /** @@ -51,38 +54,110 @@ public function getStream():StreamInterface { * Input::METHOD_GET or Input::METHOD_POST as the second parameter (defaults to * Input::METHOD_BOTH). */ - public function get(string $key, string $method = null):?string { + public function get(string $key, string $method = null):?InputDatum { if(is_null($method)) { $method = self::DATA_COMBINED; } + $data = null; + + if($this->has($key, $method)) { + switch($method) { + case self::DATA_QUERYSTRING: + $data = $this->getQueryStringParameter($key); + break; + + case self::DATA_BODY: + $data =$this->getBodyParameter($key); + break; + + case self::DATA_FILES: + $data =$this->getUploadedFileParameter($key); + break; + + case self::DATA_COMBINED: + $data = $this->data[$key]; + break; + + default: + throw new InvalidInputMethodException($method); + } + } + + return $data; + } + + public function getQueryStringParameter(string $key):?InputDatum { + if($this->hasQueryStringParameter($key)) { + return $this->queryStringParameters[$key]; + } + return null; + } + + public function getBodyParameter(string $key):?InputDatum { + if($this->hasBodyParameter($key)) { + return $this->bodyParameters[$key]; + } + return null; + } + + public function getUploadedFileParameter(string $key):?InputDatum { + if($this->hasUploadedFileParameter($key)) { + return $this->uploadedFileParameters[$key]; + } + return null; + } + + /** + * Does the input contain the specified key? + */ + public function has(string $key, string $method = null):bool { + if(is_null($method)) { + $method = self::DATA_COMBINED; + } + + $isset = false; + switch($method) { case self::DATA_QUERYSTRING: - $variable = $this->queryStringParameters; + $isset = $this->hasQueryStringParameter($key); break; - case self::DATA_POSTFIELDS: - $variable = $this->postFields; + + case self::DATA_BODY: + $isset =$this->hasBodyParameter($key); + break; + + case self::DATA_FILES: + $isset =$this->hasUploadedFileParameter($key); break; + case self::DATA_COMBINED: - $variable = $this->data; + $isset = isset($this->data[$key]); break; + default: throw new InvalidInputMethodException($method); } - return $variable[$key]; + return $isset; } - /** - * Does the input contain the specified key? - */ - public function has(string $key):bool { - return isset($this->data[$key]); + public function hasQueryStringParameter(string $key):bool { + return isset($this->queryStringParameters[$key]); + } + + public function hasBodyParameter(string $key):bool { + return isset($this->bodyParameters[$key]); + } + + public function hasUploadedFileParameter(string $key):bool { + return isset($this->uploadedFileParameters[$key]); } /** * Get an InputData object containing all request variables. To specify only GET or POST * variables, pass Input::METHOD_GET or Input::METHOD_POST. + * TODO: Return type needs to be AbstractInputData */ public function getAll(string $method = null):InputData { if(is_null($method)) { @@ -92,8 +167,10 @@ public function getAll(string $method = null):InputData { switch($method) { case self::DATA_QUERYSTRING: return $this->queryStringParameters; - case self::DATA_POSTFIELDS: - return $this->postFields; + case self::DATA_BODY: + return $this->bodyParameters; + case self::DATA_FILES: + return $this->uploadedFileParameters; case self::DATA_COMBINED: return $this->data; default: diff --git a/src/InputDatum.php b/src/InputDatum.php new file mode 100644 index 0000000..b9fdabe --- /dev/null +++ b/src/InputDatum.php @@ -0,0 +1,5 @@ +get( $key, - Input::DATA_POSTFIELDS + Input::DATA_BODY ); self::assertEquals($post[$key], $value); @@ -109,7 +109,7 @@ public function testGetAllQueryString(array $get, array $post):void { */ public function testGetAllPostFields(array $get, array $post):void { $input = new Input($get, $post); - $postFields = $input->getAll(Input::DATA_POSTFIELDS); + $postFields = $input->getAll(Input::DATA_BODY); foreach($get as $key => $value) { self::assertFalse(isset($postFields[$key])); @@ -143,7 +143,7 @@ public function testGetAll(array $get, array $post):void { public function testGetAllMethods(array $get, array $post):void { $input = new Input($get, $post); $getVariables = $input->getAll(Input::DATA_QUERYSTRING); - $postVariables = $input->getAll(Input::DATA_POSTFIELDS); + $postVariables = $input->getAll(Input::DATA_BODY); foreach($get as $key => $value) { self::assertTrue(isset($getVariables[$key])); From 8f1a51ba585e603255be5c0ac1df275f8cd9997a Mon Sep 17 00:00:00 2001 From: Greg Bowler Date: Fri, 23 Feb 2018 16:30:50 +0000 Subject: [PATCH 04/21] Test files superglobal is normalised --- src/UploadData.php | 96 ++++++++++++++++++++++++++++- test/unit/Helper/Reflection.php | 14 +++++ test/unit/UploadDataTest.php | 106 ++++++++++++++++++++++++++++++++ 3 files changed, 215 insertions(+), 1 deletion(-) create mode 100644 test/unit/Helper/Reflection.php create mode 100644 test/unit/UploadDataTest.php diff --git a/src/UploadData.php b/src/UploadData.php index 5c3cb41..19d3948 100644 --- a/src/UploadData.php +++ b/src/UploadData.php @@ -1,7 +1,101 @@ normalizeArray($files); + } + + /** + * @link http://php.net/manual/en/iterator.current.php + */ + public function current() { + // TODO: Implement current() method. + } + + /** + * @link http://php.net/manual/en/iterator.next.php + */ + public function next():void { + // TODO: Implement next() method. + } + + /** + * @link http://php.net/manual/en/iterator.key.php + */ + public function key() { + // TODO: Implement key() method. + } + + /** + * @link http://php.net/manual/en/iterator.valid.php + */ + public function valid():bool { + // TODO: Implement valid() method. + } + + /** + * @link http://php.net/manual/en/iterator.rewind.php + */ + public function rewind():void { + // TODO: Implement rewind() method. + } + + /** + * @link http://php.net/manual/en/arrayaccess.offsetexists.php + */ + public function offsetExists($offset):bool { + // TODO: Implement offsetExists() method. + } + + /** + * @link http://php.net/manual/en/arrayaccess.offsetget.php + */ + public function offsetGet($offset) { + // TODO: Implement offsetGet() method. + } + + /** + * @link http://php.net/manual/en/arrayaccess.offsetset.php + */ + public function offsetSet($offset, $value):void { + // TODO: Implement offsetSet() method. + } + + /** + * @link http://php.net/manual/en/arrayaccess.offsetunset.php + */ + public function offsetUnset($offset):void { + // TODO: Implement offsetUnset() method. + } + + /** + * @link http://php.net/manual/en/countable.count.php + */ + public function count():int { + // TODO: Implement count() method. + } + + /** + * The files array is an associative array where the key is the name of the request + * parameter, and the value is another associative array with keys: + * + name + * + type + * + tmp_name + * + error + * + size + * Each key's value is string, unless the request parameter name ends with [], in which case + * each value is another array. This function normalises the array to the latter. + */ + protected function normalizeArray($files):array { + foreach($files as $parameterName => $fileDetailArray) { + foreach($fileDetailArray as $key => $value) { + if(!is_array($value)) { + $files[$parameterName][$key] = [$value]; + } + } + } + + return $files; } } \ No newline at end of file diff --git a/test/unit/Helper/Reflection.php b/test/unit/Helper/Reflection.php new file mode 100644 index 0000000..c04760c --- /dev/null +++ b/test/unit/Helper/Reflection.php @@ -0,0 +1,14 @@ +getMethod($methodName); + $method->setAccessible(true); + return $method; + } +} \ No newline at end of file diff --git a/test/unit/UploadDataTest.php b/test/unit/UploadDataTest.php new file mode 100644 index 0000000..07c7530 --- /dev/null +++ b/test/unit/UploadDataTest.php @@ -0,0 +1,106 @@ + $uploadData) { + foreach($uploadData as $key => $value) { + if(is_string($value)) { + $numberOfStrings++; + } + else if(is_array($value)) { + $numberOfArrays++; + } + } + } + + self::assertGreaterThan(0, $numberOfStrings); + self::assertGreaterThan(0, $numberOfArrays); + + $uploadData = new UploadData($files); + $normalized = $method->invoke($uploadData, $files); + + $numberOfStrings = 0; + $numberOfArrays = 0; + + foreach($normalized as $uploadName => $uploadData) { + foreach($uploadData as $key => $value) { + if(is_string($value)) { + $numberOfStrings++; + } + else if(is_array($value)) { + $numberOfArrays++; + } + } + } + + self::assertEquals(0, $numberOfStrings); + self::assertGreaterThan(0, $numberOfArrays); + } + + public function dataFilesSuperglobal():array { + $data = []; + $num = 10; + $fileKeys = ["name", "type", "tmp_name", "error", "size"]; + + for($i = 0; $i < $num; $i++) { + $files = []; + + $hasAtLeastOneFieldBeenSingle = false; + $hasAtLeastOneFieldBeenMulti = false; + + $numDifferentFileFields = rand(1, 50); + for($iFileUpload = 0; $iFileUpload < $numDifferentFileFields; $iFileUpload++) { + $name = "upload_" . uniqid(); + $files[$name] = []; + + if(!$hasAtLeastOneFieldBeenSingle) { + $numFilesWithinSameField = 1; + $hasAtLeastOneFieldBeenSingle = true; + } + else if(!$hasAtLeastOneFieldBeenMulti) { + $numFilesWithinSameField = 5; + $hasAtLeastOneFieldBeenMulti = true; + } + else { + $numFilesWithinSameField = rand(1, 5); + } + + if($numFilesWithinSameField === 1) { + foreach($fileKeys as $key) { + $files[$name][$key] = uniqid($key . "_"); + } + } + else { + for($iFileInField = 0; $iFileInField < $numFilesWithinSameField; $iFileInField++) { + foreach($fileKeys as $key) { + if(!isset($files[$name][$key])) { + $files[$name][$key] = []; + } + $files[$name][$key] []= uniqid($key . "_"); + } + } + } + } + + $data []= [$files]; + } + + return $data; + } +} \ No newline at end of file From 53279fe22177e197d6ad00fe924f5ebbaa6e37a1 Mon Sep 17 00:00:00 2001 From: Greg Bowler Date: Fri, 23 Feb 2018 18:06:48 +0000 Subject: [PATCH 05/21] Massive refactor to simplify InputData sharing --- src/Input.php | 84 ++++++--------- src/InputData/AbstractInputData.php | 29 +++++ src/InputData/BodyInputData.php | 4 + src/InputData/CombinedInputData.php | 4 + src/{ => InputData}/InputData.php | 23 ++-- src/{ => InputData}/InputDataFactory.php | 0 src/InputData/InputDatum.php | 10 ++ .../KeyValueArrayAccess.php} | 22 +--- .../KeyValueCountable.php} | 2 +- .../KeyValueIterator.php} | 5 +- src/InputData/QueryStringInputData.php | 4 + src/InputData/UploadInputData.php | 34 ++++++ src/InputDatum.php | 5 - src/UploadData.php | 101 ------------------ test/unit/UploadDataTest.php | 6 +- 15 files changed, 134 insertions(+), 199 deletions(-) create mode 100644 src/InputData/AbstractInputData.php create mode 100644 src/InputData/BodyInputData.php create mode 100644 src/InputData/CombinedInputData.php rename src/{ => InputData}/InputData.php (61%) rename src/{ => InputData}/InputDataFactory.php (100%) create mode 100644 src/InputData/InputDatum.php rename src/{InputDataArrayAccess.php => InputData/KeyValueArrayAccess.php} (50%) rename src/{InputDataCountable.php => InputData/KeyValueCountable.php} (77%) rename src/{InputDataIterator.php => InputData/KeyValueIterator.php} (82%) create mode 100644 src/InputData/QueryStringInputData.php create mode 100644 src/InputData/UploadInputData.php delete mode 100644 src/InputDatum.php delete mode 100644 src/UploadData.php diff --git a/src/Input.php b/src/Input.php index 85250a1..7f8a466 100644 --- a/src/Input.php +++ b/src/Input.php @@ -7,24 +7,22 @@ use Psr\Http\Message\StreamInterface; class Input implements ArrayAccess, Countable, Iterator { - use InputDataArrayAccess; - use InputDataCountable; - use InputDataIterator; - const DATA_QUERYSTRING = "get"; const DATA_BODY = "post"; const DATA_FILES = "files"; - const DATA_COMBINED = "both"; + const DATA_COMBINED = "combined"; /** @var BodyStream */ protected $bodyStream; - /** @var UploadData */ - protected $uploadedFileParameters; - /** @var InputData */ + /** @var QueryStringInputData */ protected $queryStringParameters; - /** @var InputData */ + /** @var BodyInputData */ protected $bodyParameters; + /** @var UploadInputData */ + protected $uploadedFileParameters; + /** @var CombinedInputData */ + protected $combinedParameters; public function __construct( array $get = [], @@ -33,13 +31,16 @@ public function __construct( string $bodyPath = "php://input" ) { $this->bodyStream = new BodyStream($bodyPath); - // TODO: The following three variables can extend the same base class. AbstractInputData? - $this->queryStringParameters = new InputData($get); - $this->bodyParameters = new InputData($post); - $this->uploadedFileParameters = new UploadData($files); - $this->data = new InputData($get, $post, $this->uploadedFileParameters); - $this->dataKeys = $this->data->getKeys(); + $this->queryStringParameters = new QueryStringInputData($get); + $this->bodyParameters = new BodyInputData($post); + $this->uploadedFileParameters = new UploadInputData($files); + + $this->combinedParameters = new CombinedInputData( + $this->queryStringParameters, + $this->bodyParameters, + $this->uploadedFileParameters + ); } /** @@ -61,53 +62,30 @@ public function get(string $key, string $method = null):?InputDatum { $data = null; - if($this->has($key, $method)) { - switch($method) { - case self::DATA_QUERYSTRING: - $data = $this->getQueryStringParameter($key); - break; + switch($method) { + case self::DATA_QUERYSTRING: + $data = $this->queryStringParameters->get($key); + break; - case self::DATA_BODY: - $data =$this->getBodyParameter($key); - break; + case self::DATA_BODY: + $data =$this->bodyParameters->get($key); + break; - case self::DATA_FILES: - $data =$this->getUploadedFileParameter($key); - break; + case self::DATA_FILES: + $data = $this->uploadedFileParameters->get($key); + break; - case self::DATA_COMBINED: - $data = $this->data[$key]; - break; + case self::DATA_COMBINED: + $data = $this->combinedParameters->get($key); + break; - default: - throw new InvalidInputMethodException($method); - } + default: + throw new InvalidInputMethodException($method); } return $data; } - public function getQueryStringParameter(string $key):?InputDatum { - if($this->hasQueryStringParameter($key)) { - return $this->queryStringParameters[$key]; - } - return null; - } - - public function getBodyParameter(string $key):?InputDatum { - if($this->hasBodyParameter($key)) { - return $this->bodyParameters[$key]; - } - return null; - } - - public function getUploadedFileParameter(string $key):?InputDatum { - if($this->hasUploadedFileParameter($key)) { - return $this->uploadedFileParameters[$key]; - } - return null; - } - /** * Does the input contain the specified key? */ diff --git a/src/InputData/AbstractInputData.php b/src/InputData/AbstractInputData.php new file mode 100644 index 0000000..a66735c --- /dev/null +++ b/src/InputData/AbstractInputData.php @@ -0,0 +1,29 @@ +data[$key] ?? null; + } + + protected function set(string $key, InputDatum $value):void { + $this->data[$key] = $value; + } + + public function withKeyValue(string $key, InputDatum $value):self { + $clone = clone($this); + $clone->data[$key] = $value; + return $clone; + } +} \ No newline at end of file diff --git a/src/InputData/BodyInputData.php b/src/InputData/BodyInputData.php new file mode 100644 index 0000000..1cf7d5c --- /dev/null +++ b/src/InputData/BodyInputData.php @@ -0,0 +1,4 @@ +data = []; foreach($sources as $source) { foreach($source as $key => $value) { + if(is_string($value)) { + $value = new InputDatum($value); + } $this->add($key, $value); } } - - $this->storeDataKeys(); } - public function add(string $key, string $value):self { - $this->data[$key] = $value; - $this->storeDataKeys(); + public function add(string $key, InputDatum $datum):self { + $this->data[$key] = $datum; return $this; } @@ -35,7 +34,6 @@ public function remove(string...$keys):self { } } - $this->storeDataKeys(); return $this; } @@ -46,11 +44,6 @@ public function removeExcept(string...$keys):self { } } - $this->storeDataKeys(); return $this; } - - public function getKeys():array { - return $this->dataKeys; - } } \ No newline at end of file diff --git a/src/InputDataFactory.php b/src/InputData/InputDataFactory.php similarity index 100% rename from src/InputDataFactory.php rename to src/InputData/InputDataFactory.php diff --git a/src/InputData/InputDatum.php b/src/InputData/InputDatum.php new file mode 100644 index 0000000..8e2b88b --- /dev/null +++ b/src/InputData/InputDatum.php @@ -0,0 +1,10 @@ +value = $value; + } +} \ No newline at end of file diff --git a/src/InputDataArrayAccess.php b/src/InputData/KeyValueArrayAccess.php similarity index 50% rename from src/InputDataArrayAccess.php rename to src/InputData/KeyValueArrayAccess.php index 49aecc6..92cc2d8 100644 --- a/src/InputDataArrayAccess.php +++ b/src/InputData/KeyValueArrayAccess.php @@ -1,18 +1,13 @@ data[$offset]); } - public function offsetGet($offset):?string { - return $this->data[$offset] ?? null; + public function offsetGet($offset):?InputDatum { + return $this->get($offset); } public function offsetSet($offset, $value):void { @@ -21,7 +16,6 @@ public function offsetSet($offset, $value):void { } else { $this->data[$offset] = $value; - $this->storeDataKeys(); } } @@ -31,16 +25,6 @@ public function offsetUnset($offset) { } else { unset($this->data[$offset]); - $this->storeDataKeys(); - } - } - - protected function storeDataKeys():void { - if($this->data instanceof InputData) { - $this->dataKeys = $this->data->getKeys(); - } - else { - $this->dataKeys = array_keys($this->data); } } } \ No newline at end of file diff --git a/src/InputDataCountable.php b/src/InputData/KeyValueCountable.php similarity index 77% rename from src/InputDataCountable.php rename to src/InputData/KeyValueCountable.php index ea618aa..43362cb 100644 --- a/src/InputDataCountable.php +++ b/src/InputData/KeyValueCountable.php @@ -1,7 +1,7 @@ data); } diff --git a/src/InputDataIterator.php b/src/InputData/KeyValueIterator.php similarity index 82% rename from src/InputDataIterator.php rename to src/InputData/KeyValueIterator.php index 1904788..1edc1c5 100644 --- a/src/InputDataIterator.php +++ b/src/InputData/KeyValueIterator.php @@ -1,7 +1,7 @@ dataKeys[$this->iteratorKey] ?? null; + $keys = array_keys($this->data); + return $keys[$this->iteratorKey] ?? null; } } \ No newline at end of file diff --git a/src/InputData/QueryStringInputData.php b/src/InputData/QueryStringInputData.php new file mode 100644 index 0000000..27da507 --- /dev/null +++ b/src/InputData/QueryStringInputData.php @@ -0,0 +1,4 @@ +normalizeArray($files); + + // TODO: Set $this->data with kvp of files ($files[filename] => FileUpload(data)) + } + + /** + * The files array is an associative array where the key is the name of the request + * parameter, and the value is another associative array with keys: + * + name + * + type + * + tmp_name + * + error + * + size + * Each key's value is string, unless the request parameter name ends with [], in which case + * each value is another array. This function normalises the array to the latter. + */ + protected function normalizeArray($files):array { + foreach($files as $parameterName => $fileDetailArray) { + foreach($fileDetailArray as $key => $value) { + if(!is_array($value)) { + $files[$parameterName][$key] = [$value]; + } + } + } + + return $files; + } +} \ No newline at end of file diff --git a/src/InputDatum.php b/src/InputDatum.php deleted file mode 100644 index b9fdabe..0000000 --- a/src/InputDatum.php +++ /dev/null @@ -1,5 +0,0 @@ -normalizeArray($files); - } - - /** - * @link http://php.net/manual/en/iterator.current.php - */ - public function current() { - // TODO: Implement current() method. - } - - /** - * @link http://php.net/manual/en/iterator.next.php - */ - public function next():void { - // TODO: Implement next() method. - } - - /** - * @link http://php.net/manual/en/iterator.key.php - */ - public function key() { - // TODO: Implement key() method. - } - - /** - * @link http://php.net/manual/en/iterator.valid.php - */ - public function valid():bool { - // TODO: Implement valid() method. - } - - /** - * @link http://php.net/manual/en/iterator.rewind.php - */ - public function rewind():void { - // TODO: Implement rewind() method. - } - - /** - * @link http://php.net/manual/en/arrayaccess.offsetexists.php - */ - public function offsetExists($offset):bool { - // TODO: Implement offsetExists() method. - } - - /** - * @link http://php.net/manual/en/arrayaccess.offsetget.php - */ - public function offsetGet($offset) { - // TODO: Implement offsetGet() method. - } - - /** - * @link http://php.net/manual/en/arrayaccess.offsetset.php - */ - public function offsetSet($offset, $value):void { - // TODO: Implement offsetSet() method. - } - - /** - * @link http://php.net/manual/en/arrayaccess.offsetunset.php - */ - public function offsetUnset($offset):void { - // TODO: Implement offsetUnset() method. - } - - /** - * @link http://php.net/manual/en/countable.count.php - */ - public function count():int { - // TODO: Implement count() method. - } - - /** - * The files array is an associative array where the key is the name of the request - * parameter, and the value is another associative array with keys: - * + name - * + type - * + tmp_name - * + error - * + size - * Each key's value is string, unless the request parameter name ends with [], in which case - * each value is another array. This function normalises the array to the latter. - */ - protected function normalizeArray($files):array { - foreach($files as $parameterName => $fileDetailArray) { - foreach($fileDetailArray as $key => $value) { - if(!is_array($value)) { - $files[$parameterName][$key] = [$value]; - } - } - } - - return $files; - } -} \ No newline at end of file diff --git a/test/unit/UploadDataTest.php b/test/unit/UploadDataTest.php index 07c7530..112c1ee 100644 --- a/test/unit/UploadDataTest.php +++ b/test/unit/UploadDataTest.php @@ -2,7 +2,7 @@ namespace Gt\Input\Test; use Gt\Input\Test\Helper\Reflection; -use Gt\Input\UploadData; +use Gt\Input\UploadInputData; use PHPUnit\Framework\TestCase; class UploadDataTest extends TestCase { @@ -11,7 +11,7 @@ class UploadDataTest extends TestCase { */ public function testNormaliseArray(array $files) { $method = Reflection::getMethod( - UploadData::class, + UploadInputData::class, "normalizeArray" ); @@ -32,7 +32,7 @@ public function testNormaliseArray(array $files) { self::assertGreaterThan(0, $numberOfStrings); self::assertGreaterThan(0, $numberOfArrays); - $uploadData = new UploadData($files); + $uploadData = new UploadInputData($files); $normalized = $method->invoke($uploadData, $files); $numberOfStrings = 0; From 38d2eb8fdbbb1d5aed30ed0adc0cc97a76bd8501 Mon Sep 17 00:00:00 2001 From: Greg Bowler Date: Fri, 23 Feb 2018 18:24:56 +0000 Subject: [PATCH 06/21] Add todo note for #8 --- src/Callback.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Callback.php b/src/Callback.php index 887306e..64dea14 100644 --- a/src/Callback.php +++ b/src/Callback.php @@ -12,6 +12,8 @@ public function __construct(callable $callback, ...$args) { } public function call(InputData $data):void { + // TODO: Issue #8 Rather than passing all fields into the first parameter, pass them individually. + // @see https://github.com/PhpGt/Input/issues/8 $parameters = [$data]; foreach($this->args as $arg) { $parameters []= $arg; From fd08d5eae7fb6870dd0185ace09ae8909875d519 Mon Sep 17 00:00:00 2001 From: Greg Bowler Date: Fri, 23 Feb 2018 18:30:14 +0000 Subject: [PATCH 07/21] Fix namespacing --- src/Input.php | 36 ++++++++++--------- src/InputData/AbstractInputData.php | 10 +++--- src/InputData/BodyInputData.php | 2 +- src/InputData/CombinedInputData.php | 2 +- ...dInputData.php => FileUploadInputData.php} | 4 +-- src/InputData/InputData.php | 21 ++++------- src/InputData/InputDataFactory.php | 2 +- src/InputData/InputDatum.php | 2 +- src/InputData/KeyValueArrayAccess.php | 16 ++++----- src/InputData/KeyValueCountable.php | 4 +-- src/InputData/KeyValueIterator.php | 6 ++-- src/InputData/QueryStringInputData.php | 2 +- test/unit/UploadDataTest.php | 6 ++-- 13 files changed, 55 insertions(+), 58 deletions(-) rename src/InputData/{UploadInputData.php => FileUploadInputData.php} (91%) diff --git a/src/Input.php b/src/Input.php index 7f8a466..33341fd 100644 --- a/src/Input.php +++ b/src/Input.php @@ -5,8 +5,15 @@ use Countable; use Iterator; use Psr\Http\Message\StreamInterface; +use Gt\Input\InputData\KeyValueArrayAccess; +use Gt\Input\InputData\KeyValueCountable; +use Gt\Input\InputData\KeyValueIterator; class Input implements ArrayAccess, Countable, Iterator { + use KeyValueArrayAccess; + use KeyValueCountable; + use KeyValueIterator; + const DATA_QUERYSTRING = "get"; const DATA_BODY = "post"; const DATA_FILES = "files"; @@ -19,10 +26,10 @@ class Input implements ArrayAccess, Countable, Iterator { protected $queryStringParameters; /** @var BodyInputData */ protected $bodyParameters; - /** @var UploadInputData */ - protected $uploadedFileParameters; + /** @var FileUploadInputData */ + protected $fileUploadParameters; /** @var CombinedInputData */ - protected $combinedParameters; + protected $parameters; public function __construct( array $get = [], @@ -34,12 +41,12 @@ public function __construct( $this->queryStringParameters = new QueryStringInputData($get); $this->bodyParameters = new BodyInputData($post); - $this->uploadedFileParameters = new UploadInputData($files); + $this->fileUploadParameters = new FileUploadInputData($files); - $this->combinedParameters = new CombinedInputData( + $this->parameters = new CombinedInputData( $this->queryStringParameters, $this->bodyParameters, - $this->uploadedFileParameters + $this->fileUploadParameters ); } @@ -72,11 +79,11 @@ public function get(string $key, string $method = null):?InputDatum { break; case self::DATA_FILES: - $data = $this->uploadedFileParameters->get($key); + $data = $this->fileUploadParameters->get($key); break; case self::DATA_COMBINED: - $data = $this->combinedParameters->get($key); + $data = $this->parameters->get($key); break; default: @@ -94,8 +101,6 @@ public function has(string $key, string $method = null):bool { $method = self::DATA_COMBINED; } - $isset = false; - switch($method) { case self::DATA_QUERYSTRING: $isset = $this->hasQueryStringParameter($key); @@ -106,7 +111,7 @@ public function has(string $key, string $method = null):bool { break; case self::DATA_FILES: - $isset =$this->hasUploadedFileParameter($key); + $isset =$this->hasFileUploadParameter($key); break; case self::DATA_COMBINED: @@ -128,14 +133,13 @@ public function hasBodyParameter(string $key):bool { return isset($this->bodyParameters[$key]); } - public function hasUploadedFileParameter(string $key):bool { - return isset($this->uploadedFileParameters[$key]); + public function hasFileUploadParameter(string $key):bool { + return isset($this->fileUploadParameters[$key]); } /** * Get an InputData object containing all request variables. To specify only GET or POST * variables, pass Input::METHOD_GET or Input::METHOD_POST. - * TODO: Return type needs to be AbstractInputData */ public function getAll(string $method = null):InputData { if(is_null($method)) { @@ -148,9 +152,9 @@ public function getAll(string $method = null):InputData { case self::DATA_BODY: return $this->bodyParameters; case self::DATA_FILES: - return $this->uploadedFileParameters; + return $this->fileUploadParameters; case self::DATA_COMBINED: - return $this->data; + return $this->parameters; default: throw new InvalidInputMethodException($method); } diff --git a/src/InputData/AbstractInputData.php b/src/InputData/AbstractInputData.php index a66735c..6c70b01 100644 --- a/src/InputData/AbstractInputData.php +++ b/src/InputData/AbstractInputData.php @@ -1,5 +1,5 @@ data[$key] ?? null; + return $this->parameters[$key] ?? null; } protected function set(string $key, InputDatum $value):void { - $this->data[$key] = $value; + $this->parameters[$key] = $value; } public function withKeyValue(string $key, InputDatum $value):self { $clone = clone($this); - $clone->data[$key] = $value; + $clone->parameters[$key] = $value; return $clone; } } \ No newline at end of file diff --git a/src/InputData/BodyInputData.php b/src/InputData/BodyInputData.php index 1cf7d5c..4d7863a 100644 --- a/src/InputData/BodyInputData.php +++ b/src/InputData/BodyInputData.php @@ -1,4 +1,4 @@ normalizeArray($files); diff --git a/src/InputData/InputData.php b/src/InputData/InputData.php index 1cdef97..11954c9 100644 --- a/src/InputData/InputData.php +++ b/src/InputData/InputData.php @@ -1,16 +1,9 @@ data = []; + $this->parameters = []; foreach($sources as $source) { foreach($source as $key => $value) { @@ -23,14 +16,14 @@ public function __construct(iterable...$sources) { } public function add(string $key, InputDatum $datum):self { - $this->data[$key] = $datum; + $this->parameters[$key] = $datum; return $this; } public function remove(string...$keys):self { foreach($keys as $key) { - if(isset($this->data[$key])) { - unset($this->data[$key]); + if(isset($this->parameters[$key])) { + unset($this->parameters[$key]); } } @@ -38,9 +31,9 @@ public function remove(string...$keys):self { } public function removeExcept(string...$keys):self { - foreach($this->data as $key => $value) { + foreach($this->parameters as $key => $value) { if(!in_array($key, $keys)) { - unset($this->data[$key]); + unset($this->parameters[$key]); } } diff --git a/src/InputData/InputDataFactory.php b/src/InputData/InputDataFactory.php index b807f98..8c838b6 100644 --- a/src/InputData/InputDataFactory.php +++ b/src/InputData/InputDataFactory.php @@ -1,5 +1,5 @@ data[$offset]); + return isset($this->parameters[$offset]); } public function offsetGet($offset):?InputDatum { @@ -11,20 +11,20 @@ public function offsetGet($offset):?InputDatum { } public function offsetSet($offset, $value):void { - if($this->data instanceof InputData) { - $this->data->add($offset, $value); + if($this->parameters instanceof InputData) { + $this->parameters->add($offset, $value); } else { - $this->data[$offset] = $value; + $this->parameters[$offset] = $value; } } public function offsetUnset($offset) { - if($this->data instanceof InputData) { - $this->data->remove($offset); + if($this->parameters instanceof InputData) { + $this->parameters->remove($offset); } else { - unset($this->data[$offset]); + unset($this->parameters[$offset]); } } } \ No newline at end of file diff --git a/src/InputData/KeyValueCountable.php b/src/InputData/KeyValueCountable.php index 43362cb..6f2b5a6 100644 --- a/src/InputData/KeyValueCountable.php +++ b/src/InputData/KeyValueCountable.php @@ -1,8 +1,8 @@ data); + return count($this->parameters); } } \ No newline at end of file diff --git a/src/InputData/KeyValueIterator.php b/src/InputData/KeyValueIterator.php index 1edc1c5..2b34aac 100644 --- a/src/InputData/KeyValueIterator.php +++ b/src/InputData/KeyValueIterator.php @@ -1,12 +1,12 @@ data[$this->getIteratorDataKey()]; + return $this->parameters[$this->getIteratorDataKey()]; } public function next():void { @@ -26,7 +26,7 @@ public function rewind() { } protected function getIteratorDataKey():?string { - $keys = array_keys($this->data); + $keys = array_keys($this->parameters); return $keys[$this->iteratorKey] ?? null; } } \ No newline at end of file diff --git a/src/InputData/QueryStringInputData.php b/src/InputData/QueryStringInputData.php index 27da507..76a15d5 100644 --- a/src/InputData/QueryStringInputData.php +++ b/src/InputData/QueryStringInputData.php @@ -1,4 +1,4 @@ invoke($uploadData, $files); $numberOfStrings = 0; From b2480557a4a22c7f66b3510282e639501c3aa3ba Mon Sep 17 00:00:00 2001 From: Greg Bowler Date: Fri, 23 Feb 2018 18:33:35 +0000 Subject: [PATCH 08/21] Fix namespacing --- src/Input.php | 7 +++++++ src/InputData/InputDataFactory.php | 3 +++ src/{ => Trigger}/Callback.php | 4 +++- src/{ => Trigger}/Trigger.php | 5 ++++- test/unit/{ => InputData}/InputDataFactoryTest.php | 0 test/unit/{ => InputData}/InputDataTest.php | 0 6 files changed, 17 insertions(+), 2 deletions(-) rename src/{ => Trigger}/Callback.php (89%) rename src/{ => Trigger}/Trigger.php (94%) rename test/unit/{ => InputData}/InputDataFactoryTest.php (100%) rename test/unit/{ => InputData}/InputDataTest.php (100%) diff --git a/src/Input.php b/src/Input.php index 33341fd..8ffab49 100644 --- a/src/Input.php +++ b/src/Input.php @@ -3,11 +3,18 @@ use ArrayAccess; use Countable; +use Gt\Input\Trigger\Trigger; use Iterator; use Psr\Http\Message\StreamInterface; +use Gt\Input\InputData\InputData; +use Gt\Input\InputData\InputDatum; use Gt\Input\InputData\KeyValueArrayAccess; use Gt\Input\InputData\KeyValueCountable; use Gt\Input\InputData\KeyValueIterator; +use Gt\Input\InputData\BodyInputData; +use Gt\Input\InputData\CombinedInputData; +use Gt\Input\InputData\FileUploadInputData; +use Gt\Input\InputData\QueryStringInputData; class Input implements ArrayAccess, Countable, Iterator { use KeyValueArrayAccess; diff --git a/src/InputData/InputDataFactory.php b/src/InputData/InputDataFactory.php index 8c838b6..6d8720f 100644 --- a/src/InputData/InputDataFactory.php +++ b/src/InputData/InputDataFactory.php @@ -1,6 +1,9 @@ getAll(); diff --git a/src/Callback.php b/src/Trigger/Callback.php similarity index 89% rename from src/Callback.php rename to src/Trigger/Callback.php index 64dea14..5ff3e7b 100644 --- a/src/Callback.php +++ b/src/Trigger/Callback.php @@ -1,5 +1,7 @@ Date: Fri, 23 Feb 2018 18:35:26 +0000 Subject: [PATCH 09/21] Fix namespacing --- src/Trigger/Trigger.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Trigger/Trigger.php b/src/Trigger/Trigger.php index 8d1c35f..57ce95f 100644 --- a/src/Trigger/Trigger.php +++ b/src/Trigger/Trigger.php @@ -99,6 +99,7 @@ protected function callCallbacks() { ); foreach($this->callbacks as $callback) { + /** @var $callback \Gt\Input\Trigger\Callback */ $callback->call($fields); } } From 184dafa3d6c3ed0412ee38dd75d5d374a511e913 Mon Sep 17 00:00:00 2001 From: Greg Bowler Date: Fri, 23 Feb 2018 18:36:37 +0000 Subject: [PATCH 10/21] Fix trigger tests --- test/unit/{ => Trigger}/CallbackTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) rename test/unit/{ => Trigger}/CallbackTest.php (93%) diff --git a/test/unit/CallbackTest.php b/test/unit/Trigger/CallbackTest.php similarity index 93% rename from test/unit/CallbackTest.php rename to test/unit/Trigger/CallbackTest.php index a8d4e62..3f82d0c 100644 --- a/test/unit/CallbackTest.php +++ b/test/unit/Trigger/CallbackTest.php @@ -2,8 +2,8 @@ namespace Gt\Input\Test; -use Gt\Input\Callback; -use Gt\Input\InputData; +use Gt\Input\InputData\InputData; +use Gt\Input\Trigger\Callback; use PHPUnit\Framework\TestCase; class CallbackTest extends TestCase { From de3aa838f59b3121592b558f9b4f1da6b214f3a6 Mon Sep 17 00:00:00 2001 From: Greg Bowler Date: Fri, 23 Feb 2018 18:37:07 +0000 Subject: [PATCH 11/21] Fix Helper class namespaces --- test/unit/Helper/Helper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/Helper/Helper.php b/test/unit/Helper/Helper.php index 30c44ca..48aa353 100644 --- a/test/unit/Helper/Helper.php +++ b/test/unit/Helper/Helper.php @@ -2,7 +2,7 @@ namespace Gt\Input\Test\Helper; use Gt\Input\Input; -use Gt\Input\InputData; +use Gt\Input\InputData\InputData; use RecursiveDirectoryIterator; use RecursiveIteratorIterator; From 30837679bdb7a8dc92c6e09199745083a10585db Mon Sep 17 00:00:00 2001 From: Greg Bowler Date: Fri, 23 Feb 2018 18:46:54 +0000 Subject: [PATCH 12/21] Pass InputData tests --- src/InputData/InputData.php | 5 +++++ src/InputData/InputDatum.php | 4 ++++ src/InputData/KeyValueArrayAccess.php | 12 +++++++++++- test/unit/InputData/InputDataFactoryTest.php | 2 +- test/unit/InputData/InputDataTest.php | 12 ++++++------ 5 files changed, 27 insertions(+), 8 deletions(-) diff --git a/src/InputData/InputData.php b/src/InputData/InputData.php index 11954c9..b048ca3 100644 --- a/src/InputData/InputData.php +++ b/src/InputData/InputData.php @@ -20,6 +20,11 @@ public function add(string $key, InputDatum $datum):self { return $this; } + public function addKeyValue(string $key, string $value):self { + $datum = new InputDatum($value); + return $this->add($key, $datum); + } + public function remove(string...$keys):self { foreach($keys as $key) { if(isset($this->parameters[$key])) { diff --git a/src/InputData/InputDatum.php b/src/InputData/InputDatum.php index 3750fa7..bcff260 100644 --- a/src/InputData/InputDatum.php +++ b/src/InputData/InputDatum.php @@ -7,4 +7,8 @@ class InputDatum { public function __construct($value) { $this->value = $value; } + + public function __toString():string { + return $this->value; + } } \ No newline at end of file diff --git a/src/InputData/KeyValueArrayAccess.php b/src/InputData/KeyValueArrayAccess.php index 420c8fb..25e618f 100644 --- a/src/InputData/KeyValueArrayAccess.php +++ b/src/InputData/KeyValueArrayAccess.php @@ -12,9 +12,19 @@ public function offsetGet($offset):?InputDatum { public function offsetSet($offset, $value):void { if($this->parameters instanceof InputData) { - $this->parameters->add($offset, $value); + if(is_string($value)) { + $this->parameters->addKeyValue($offset, $value); + + } + else { + $this->parameters->add($offset, $value); + } } else { + if(is_string($value)) { + $value = new InputDatum($value); + } + $this->parameters[$offset] = $value; } } diff --git a/test/unit/InputData/InputDataFactoryTest.php b/test/unit/InputData/InputDataFactoryTest.php index 51c2a9f..938fcca 100644 --- a/test/unit/InputData/InputDataFactoryTest.php +++ b/test/unit/InputData/InputDataFactoryTest.php @@ -2,7 +2,7 @@ namespace Gt\Input\Test; use Gt\Input\Input; -use Gt\Input\InputDataFactory; +use Gt\Input\InputData\InputDataFactory; use Gt\Input\WithWithoutClashingException; use PHPUnit\Framework\TestCase; diff --git a/test/unit/InputData/InputDataTest.php b/test/unit/InputData/InputDataTest.php index ee23387..037b2bf 100644 --- a/test/unit/InputData/InputDataTest.php +++ b/test/unit/InputData/InputDataTest.php @@ -2,7 +2,7 @@ namespace Gt\Input\Test; -use Gt\Input\InputData; +use Gt\Input\InputData\InputData; use PHPUnit\Framework\TestCase; class InputDataTest extends TestCase { @@ -32,14 +32,14 @@ public function testMultipleSources():void { public function testAddFromEmpty():void { $data = new InputData(); - $data->add("name", "Bob"); + $data->addKeyValue("name", "Bob"); self::assertEquals("Bob", $data["name"]); } public function testAddMultipleFromEmpty():void { $data = new InputData(); - $data->add("name", "Bob"); - $data->add("gender", "m"); + $data->addKeyValue("name", "Bob"); + $data->addKeyValue("gender", "m"); self::assertEquals("Bob", $data["name"]); self::assertEquals("m", $data["gender"]); } @@ -53,8 +53,8 @@ public function testAddFromExisting():void { "userAccess" => "admin", ]); - $data->add("view", "tab1"); - $data->add("screenSize", "large"); + $data->addKeyValue("view", "tab1"); + $data->addKeyValue("screenSize", "large"); self::assertEquals("Charles", $data["name"]); self::assertEquals("m", $data["gender"]); From bfc56cfe78c3c8bd560a5e2bf92e1ccf76d723cb Mon Sep 17 00:00:00 2001 From: Greg Bowler Date: Fri, 23 Feb 2018 18:50:36 +0000 Subject: [PATCH 13/21] Pass InputDataFactory tests --- src/InputData/AbstractInputData.php | 2 +- src/InputData/InputData.php | 2 +- test/unit/InputData/InputDataFactoryTest.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/InputData/AbstractInputData.php b/src/InputData/AbstractInputData.php index 6c70b01..0797398 100644 --- a/src/InputData/AbstractInputData.php +++ b/src/InputData/AbstractInputData.php @@ -11,7 +11,7 @@ abstract class AbstractInputData implements ArrayAccess, Countable, Iterator { use KeyValueIterator; /** @var InputDatum[] */ - protected $parameters; + protected $parameters = []; public function get(string $key):?InputDatum { return $this->parameters[$key] ?? null; diff --git a/src/InputData/InputData.php b/src/InputData/InputData.php index b048ca3..4ec06cb 100644 --- a/src/InputData/InputData.php +++ b/src/InputData/InputData.php @@ -7,7 +7,7 @@ public function __construct(iterable...$sources) { foreach($sources as $source) { foreach($source as $key => $value) { - if(is_string($value)) { + if(!$value instanceof InputDatum) { $value = new InputDatum($value); } $this->add($key, $value); diff --git a/test/unit/InputData/InputDataFactoryTest.php b/test/unit/InputData/InputDataFactoryTest.php index 938fcca..16badbf 100644 --- a/test/unit/InputData/InputDataFactoryTest.php +++ b/test/unit/InputData/InputDataFactoryTest.php @@ -11,7 +11,7 @@ public function testCreateNoCriteria():void { $input = $this->createInput(); $data = InputDataFactory::create($input); self::assertEquals("Edward", $data["name"]); - self::assertEquals(51, $data["age"]); + self::assertEquals("51", $data["age"]); self::assertEquals("01234 567890", $data["telephone"]); self::assertEquals("AB12 3CD", $data["postcode"]); } From b0681f11c15c63d964cdf03e7e134ac8d18f7a84 Mon Sep 17 00:00:00 2001 From: Greg Bowler Date: Fri, 23 Feb 2018 18:54:05 +0000 Subject: [PATCH 14/21] Fix testing trigger's do method --- src/Input.php | 2 +- test/unit/InputTest.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Input.php b/src/Input.php index 8ffab49..0ab18f7 100644 --- a/src/Input.php +++ b/src/Input.php @@ -122,7 +122,7 @@ public function has(string $key, string $method = null):bool { break; case self::DATA_COMBINED: - $isset = isset($this->data[$key]); + $isset = isset($this->parameters[$key]); break; default: diff --git a/test/unit/InputTest.php b/test/unit/InputTest.php index 9c9c6be..1e234fb 100644 --- a/test/unit/InputTest.php +++ b/test/unit/InputTest.php @@ -5,7 +5,7 @@ use Gt\Input\InputData; use Gt\Input\InvalidInputMethodException; use Gt\Input\Test\Helper\Helper; -use Gt\Input\Trigger; +use Gt\Input\Trigger\Trigger; use PHPUnit\Framework\TestCase; class InputTest extends TestCase { @@ -158,7 +158,7 @@ public function testDo(string $doName):void { $input = new Input(["do" => $doName]); $trigger = $input->do($doName); $this->assertInstanceOf(Trigger::class, $trigger); - self::assertTrue($trigger->fire()); + self::assertTrue($trigger->fire(), "Triggers should fire"); } /** From 49b3086fd9205f0b1d40d9b879bb039b4528492f Mon Sep 17 00:00:00 2001 From: Greg Bowler Date: Fri, 23 Feb 2018 18:55:02 +0000 Subject: [PATCH 15/21] Import correct classes --- test/unit/InputTest.php | 2 +- test/unit/TriggerTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/unit/InputTest.php b/test/unit/InputTest.php index 1e234fb..df5ce7b 100644 --- a/test/unit/InputTest.php +++ b/test/unit/InputTest.php @@ -2,7 +2,7 @@ namespace Gt\Input\Test; use Gt\Input\Input; -use Gt\Input\InputData; +use Gt\Input\InputData\InputData; use Gt\Input\InvalidInputMethodException; use Gt\Input\Test\Helper\Helper; use Gt\Input\Trigger\Trigger; diff --git a/test/unit/TriggerTest.php b/test/unit/TriggerTest.php index b4cc2b7..a76d878 100644 --- a/test/unit/TriggerTest.php +++ b/test/unit/TriggerTest.php @@ -4,7 +4,7 @@ use Gt\Input\Input; use Gt\Input\InputData; use Gt\Input\Test\Helper\Helper; -use Gt\Input\Trigger; +use Gt\Input\Trigger\Trigger; use PHPUnit\Framework\TestCase; class TriggerTest extends TestCase { From 496cf2be81bebe9912d9226bb4dfa22b2e1b14c6 Mon Sep 17 00:00:00 2001 From: Greg Bowler Date: Fri, 23 Feb 2018 18:55:41 +0000 Subject: [PATCH 16/21] Import correct classes --- test/unit/TriggerTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/TriggerTest.php b/test/unit/TriggerTest.php index a76d878..cfd2cff 100644 --- a/test/unit/TriggerTest.php +++ b/test/unit/TriggerTest.php @@ -2,7 +2,7 @@ namespace Gt\Input\Test; use Gt\Input\Input; -use Gt\Input\InputData; +use Gt\Input\InputData\InputData; use Gt\Input\Test\Helper\Helper; use Gt\Input\Trigger\Trigger; use PHPUnit\Framework\TestCase; From 5d7d32474d082dc815c8ac2963790f092578d8af Mon Sep 17 00:00:00 2001 From: Greg Bowler Date: Fri, 23 Feb 2018 18:57:21 +0000 Subject: [PATCH 17/21] Reorder imports --- src/Input.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Input.php b/src/Input.php index 0ab18f7..a2dcd76 100644 --- a/src/Input.php +++ b/src/Input.php @@ -3,9 +3,9 @@ use ArrayAccess; use Countable; -use Gt\Input\Trigger\Trigger; use Iterator; use Psr\Http\Message\StreamInterface; +use Gt\Input\Trigger\Trigger; use Gt\Input\InputData\InputData; use Gt\Input\InputData\InputDatum; use Gt\Input\InputData\KeyValueArrayAccess; From c7b00fc2927a5a2d8c5db7795cddceaa0d077de6 Mon Sep 17 00:00:00 2001 From: Greg Bowler Date: Fri, 23 Feb 2018 19:01:28 +0000 Subject: [PATCH 18/21] Extract keys from InputData --- src/InputData/InputData.php | 4 ++++ src/InputData/KeyValueIterator.php | 10 +++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/InputData/InputData.php b/src/InputData/InputData.php index 4ec06cb..7bcfd01 100644 --- a/src/InputData/InputData.php +++ b/src/InputData/InputData.php @@ -44,4 +44,8 @@ public function removeExcept(string...$keys):self { return $this; } + + public function getKeys():array { + return array_keys($this->parameters); + } } \ No newline at end of file diff --git a/src/InputData/KeyValueIterator.php b/src/InputData/KeyValueIterator.php index 2b34aac..ecd67c5 100644 --- a/src/InputData/KeyValueIterator.php +++ b/src/InputData/KeyValueIterator.php @@ -26,7 +26,15 @@ public function rewind() { } protected function getIteratorDataKey():?string { - $keys = array_keys($this->parameters); + $keys = []; + + if(is_array($this->parameters)) { + $keys = $this->parameters; + } + else if($this->parameters instanceof InputData) { + $keys = $this->parameters->getKeys(); + } + return $keys[$this->iteratorKey] ?? null; } } \ No newline at end of file From e9c99ce4a2159892616f22e644cf7625636d09cc Mon Sep 17 00:00:00 2001 From: Greg Bowler Date: Fri, 23 Feb 2018 19:11:53 +0000 Subject: [PATCH 19/21] Few tidy ups while diagnosing problem with TriggerTest --- test/unit/InputData/InputDataFactoryTest.php | 2 +- test/unit/InputData/InputDataTest.php | 2 +- test/unit/Trigger/CallbackTest.php | 3 +-- test/unit/{ => Trigger}/TriggerTest.php | 9 +++------ 4 files changed, 6 insertions(+), 10 deletions(-) rename test/unit/{ => Trigger}/TriggerTest.php (97%) diff --git a/test/unit/InputData/InputDataFactoryTest.php b/test/unit/InputData/InputDataFactoryTest.php index 16badbf..5371f70 100644 --- a/test/unit/InputData/InputDataFactoryTest.php +++ b/test/unit/InputData/InputDataFactoryTest.php @@ -1,5 +1,5 @@ Date: Fri, 23 Feb 2018 19:16:55 +0000 Subject: [PATCH 20/21] Add missing call to array_keys --- src/InputData/KeyValueIterator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/InputData/KeyValueIterator.php b/src/InputData/KeyValueIterator.php index ecd67c5..495659f 100644 --- a/src/InputData/KeyValueIterator.php +++ b/src/InputData/KeyValueIterator.php @@ -29,7 +29,7 @@ protected function getIteratorDataKey():?string { $keys = []; if(is_array($this->parameters)) { - $keys = $this->parameters; + $keys = array_keys($this->parameters); } else if($this->parameters instanceof InputData) { $keys = $this->parameters->getKeys(); From ebd8af3f4a06c096c34f9b5f55943656e15e1805 Mon Sep 17 00:00:00 2001 From: Greg Bowler Date: Fri, 23 Feb 2018 19:18:11 +0000 Subject: [PATCH 21/21] Import FileUploadImportData from new namespace --- test/unit/UploadDataTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/unit/UploadDataTest.php b/test/unit/UploadDataTest.php index a6be773..f85ef5d 100644 --- a/test/unit/UploadDataTest.php +++ b/test/unit/UploadDataTest.php @@ -1,9 +1,9 @@