Optimized InputHandler to better support nested values.

This commit is contained in:
Simon Sessingø
2021-03-18 21:23:03 +01:00
parent 24ef438334
commit 252cc4a75d
5 changed files with 61 additions and 21 deletions

33
.idea/workspace.xml generated
View File

@@ -6,9 +6,10 @@
<component name="ChangeListManager">
<list default="true" id="a7058529-bdc4-40b4-a50d-c50564dc83f0" name="Default" comment="">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/README.md" beforeDir="false" afterPath="$PROJECT_DIR$/README.md" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/Pecee/SimpleRouter/Route/Route.php" beforeDir="false" afterPath="$PROJECT_DIR$/src/Pecee/SimpleRouter/Route/Route.php" afterDir="false" />
<change beforePath="$PROJECT_DIR$/tests/Pecee/SimpleRouter/RouterRouteTest.php" beforeDir="false" afterPath="$PROJECT_DIR$/tests/Pecee/SimpleRouter/RouterRouteTest.php" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/Pecee/Http/Input/IInputItem.php" beforeDir="false" afterPath="$PROJECT_DIR$/src/Pecee/Http/Input/IInputItem.php" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/Pecee/Http/Input/InputHandler.php" beforeDir="false" afterPath="$PROJECT_DIR$/src/Pecee/Http/Input/InputHandler.php" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/Pecee/Http/Input/InputItem.php" beforeDir="false" afterPath="$PROJECT_DIR$/src/Pecee/Http/Input/InputItem.php" afterDir="false" />
<change beforePath="$PROJECT_DIR$/tests/Pecee/SimpleRouter/InputHandlerTest.php" beforeDir="false" afterPath="$PROJECT_DIR$/tests/Pecee/SimpleRouter/InputHandlerTest.php" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@@ -285,6 +286,7 @@
<property name="node.js.selected.package.eslint" value="" />
<property name="node.js.selected.package.standard" value="" />
<property name="nodejs_package_manager_path" value="npm" />
<property name="php.override.implement.member.chooser.php.doc" value="NONE" />
<property name="run.code.analysis.last.selected.profile" value="pProject Default" />
<property name="settings.editor.selected.configurable" value="preferences.pluginManager" />
<property name="vue.rearranger.settings.migration" value="true" />
@@ -424,7 +426,9 @@
<workItem from="1535806837271" duration="204000" />
<workItem from="1543101575756" duration="1207000" />
<workItem from="1616029119335" duration="207000" />
<workItem from="1616030812009" duration="3298000" />
<workItem from="1616030812009" duration="3745000" />
<workItem from="1616076234772" duration="377000" />
<workItem from="1616086876180" duration="6191000" />
</task>
<servers />
</component>
@@ -504,6 +508,27 @@
<component name="TypeScriptGeneratedFilesManager">
<option name="version" value="3" />
</component>
<component name="Vcs.Log.History.Properties">
<option name="COLUMN_ID_ORDER">
<list>
<option value="Default.Root" />
<option value="Default.Author" />
<option value="Default.Date" />
<option value="Default.Subject" />
</list>
</option>
</component>
<component name="Vcs.Log.Tabs.Properties">
<option name="TAB_STATES">
<map>
<entry key="MAIN">
<value>
<State />
</value>
</entry>
</map>
</option>
</component>
<component name="XSLT-Support.FileAssociations.UIState">
<expand />
<select />

View File

@@ -13,7 +13,7 @@ interface IInputItem
public function setName(string $name): self;
public function getValue(): ?string;
public function getValue();
public function setValue(string $value): self;

View File

@@ -171,14 +171,11 @@ class InputHandler
foreach ($array as $key => $value) {
// Handle array input
if (\is_array($value) === false) {
$list[$key] = new InputItem($key, $value);
continue;
if (\is_array($value) === true) {
$value = $this->parseInputItem($value);
}
$output = $this->parseInputItem($value);
$list[$key] = $output;
$list[$key] = new InputItem($key, $value);
}
return $list;
@@ -222,19 +219,18 @@ class InputHandler
{
$input = $this->find($index, ...$methods);
$output = [];
/* Handle collection */
if (\is_array($input) === true) {
$output = [];
/* @var $item InputItem */
foreach ($input as $item) {
$output[] = $item->getValue();
$output[] = \is_array($item) ? $item : $item->getValue();
}
return (\count($output) === 0) ? $defaultValue : $output;
}
return ($input === null || ($input !== null && trim($input->getValue()) === '')) ? $defaultValue : $input->getValue();
return ($input === null || (\is_string($input->getValue()) && trim($input->getValue()) === '')) ? $defaultValue : $input->getValue();
}
/**
@@ -354,4 +350,4 @@ class InputHandler
$this->file[$key] = $item;
}
}
}

View File

@@ -2,13 +2,16 @@
namespace Pecee\Http\Input;
class InputItem implements IInputItem
use Exception;
use Traversable;
class InputItem implements IInputItem, \IteratorAggregate
{
public $index;
public $name;
public $value;
public function __construct(string $index, ?string $value = null)
public function __construct(string $index, $value = null)
{
$this->index = $index;
$this->value = $value;
@@ -53,10 +56,19 @@ class InputItem implements IInputItem
}
/**
* @return string
* @return mixed
*/
public function getValue(): ?string
public function getValue()
{
/*if(is_array($this->value) === true) {
$output = [];
foreach($this->value as $key => $val) {
$output[$key] = $val->getValue();
}
return $output;
}*/
return $this->value;
}
@@ -74,7 +86,12 @@ class InputItem implements IInputItem
public function __toString(): string
{
return (string)$this->value;
$value = $this->getValue();
return (\is_array($value) === true) ? json_encode($value) : $value;
}
public function getIterator()
{
return new \ArrayIterator($this->getValue());
}
}

View File

@@ -47,6 +47,7 @@ class InputHandlerTest extends \PHPUnit\Framework\TestCase
$objects = $handler->find('names');
$this->assertInstanceOf(\Pecee\Http\Input\InputItem::class, $objects);
$this->assertCount(4, $objects);
/* @var $object \Pecee\Http\Input\InputItem */
@@ -98,6 +99,7 @@ class InputHandlerTest extends \PHPUnit\Framework\TestCase
$objects = $handler->find('names');
$this->assertInstanceOf(\Pecee\Http\Input\InputItem::class, $objects);
$this->assertCount(4, $objects);
/* @var $object \Pecee\Http\Input\InputItem */