mirror of
https://github.com/skipperbent/simple-php-router.git
synced 2026-06-17 08:47:52 +00:00
InputHandler optimisations.
- InputItem can now be used like array (for example: input()->get('items')[0]) if value is array.
- Changed default-value parameter for get, post, file can now be mixed to allow object as return-type.
This commit is contained in:
@@ -308,10 +308,10 @@ class InputHandler
|
|||||||
* Find post-value by index or return default value.
|
* Find post-value by index or return default value.
|
||||||
*
|
*
|
||||||
* @param string $index
|
* @param string $index
|
||||||
* @param string|null $defaultValue
|
* @param mixed|null $defaultValue
|
||||||
* @return InputItem|array|string|null
|
* @return InputItem|array|string|null
|
||||||
*/
|
*/
|
||||||
public function post(string $index, ?string $defaultValue = null)
|
public function post(string $index, $defaultValue = null)
|
||||||
{
|
{
|
||||||
return $this->post[$index] ?? $defaultValue;
|
return $this->post[$index] ?? $defaultValue;
|
||||||
}
|
}
|
||||||
@@ -320,10 +320,10 @@ class InputHandler
|
|||||||
* Find file by index or return default value.
|
* Find file by index or return default value.
|
||||||
*
|
*
|
||||||
* @param string $index
|
* @param string $index
|
||||||
* @param string|null $defaultValue
|
* @param mixed|null $defaultValue
|
||||||
* @return InputFile|array|string|null
|
* @return InputFile|array|string|null
|
||||||
*/
|
*/
|
||||||
public function file(string $index, ?string $defaultValue = null)
|
public function file(string $index, $defaultValue = null)
|
||||||
{
|
{
|
||||||
return $this->file[$index] ?? $defaultValue;
|
return $this->file[$index] ?? $defaultValue;
|
||||||
}
|
}
|
||||||
@@ -332,10 +332,10 @@ class InputHandler
|
|||||||
* Find parameter/query-string by index or return default value.
|
* Find parameter/query-string by index or return default value.
|
||||||
*
|
*
|
||||||
* @param string $index
|
* @param string $index
|
||||||
* @param string|null $defaultValue
|
* @param mixed|null $defaultValue
|
||||||
* @return InputItem|array|string|null
|
* @return InputItem|array|string|null
|
||||||
*/
|
*/
|
||||||
public function get(string $index, ?string $defaultValue = null)
|
public function get(string $index, $defaultValue = null)
|
||||||
{
|
{
|
||||||
return $this->get[$index] ?? $defaultValue;
|
return $this->get[$index] ?? $defaultValue;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,10 +2,11 @@
|
|||||||
|
|
||||||
namespace Pecee\Http\Input;
|
namespace Pecee\Http\Input;
|
||||||
|
|
||||||
|
use ArrayAccess;
|
||||||
use ArrayIterator;
|
use ArrayIterator;
|
||||||
use IteratorAggregate;
|
use IteratorAggregate;
|
||||||
|
|
||||||
class InputItem implements IInputItem, IteratorAggregate
|
class InputItem implements ArrayAccess, IInputItem, IteratorAggregate
|
||||||
{
|
{
|
||||||
public $index;
|
public $index;
|
||||||
public $name;
|
public $name;
|
||||||
@@ -75,9 +76,34 @@ class InputItem implements IInputItem, IteratorAggregate
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function offsetExists($offset): bool
|
||||||
|
{
|
||||||
|
return isset($this->value[$offset]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function offsetGet($offset)
|
||||||
|
{
|
||||||
|
if ($this->offsetExists($offset) === true) {
|
||||||
|
return $this->value[$offset];
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function offsetSet($offset, $value): void
|
||||||
|
{
|
||||||
|
$this->value[$offset] = $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function offsetUnset($offset): void
|
||||||
|
{
|
||||||
|
unset($this->data[$offset]);
|
||||||
|
}
|
||||||
|
|
||||||
public function __toString(): string
|
public function __toString(): string
|
||||||
{
|
{
|
||||||
$value = $this->getValue();
|
$value = $this->getValue();
|
||||||
|
|
||||||
return (is_array($value) === true) ? json_encode($value) : $value;
|
return (is_array($value) === true) ? json_encode($value) : $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user