Development

- Optimised Input-classes.
- `get` and `getObject` methods on `Input` now supports filtering on multiple method-types when using the `$method` parameter.
- Input classes now know how to parse that stupid nested $_FILES array.
- It's now possible to change method-names on ResourceControllers.
- Removed `getValue` and `setValue` from `InputFile` classes.
- Ensured that request-method are only parsed from $_POST or $_SERVER.
- Fixed minor parameter-issues with subdomain routing.
- Added PHPDocs.
- Added even more unit-tests.
- Many small optimisations tweaks.
This commit is contained in:
Simon Sessingø
2016-11-26 04:30:00 +01:00
parent 68fc6b76c0
commit 6213f2fb75
27 changed files with 685 additions and 417 deletions
-4
View File
@@ -12,10 +12,6 @@ interface IInputItem
public function setName($name);
public function getValue();
public function setValue($value);
public function __toString();
}
+140 -74
View File
@@ -32,7 +32,7 @@ class Input
$this->parseInputs();
}
protected function parseInputs()
public function parseInputs()
{
/* Parse get requests */
if (count($_GET) > 0) {
@@ -42,7 +42,7 @@ class Input
/* Parse post requests */
$postVars = $_POST;
if (in_array($this->request->getMethod(), ['put', 'patch', 'delete']) === true) {
if (in_array($this->request->getMethod(), ['put', 'patch', 'delete'], false) === true) {
parse_str(file_get_contents('php://input'), $postVars);
}
@@ -51,49 +51,86 @@ class Input
}
/* Parse get requests */
if (count($_FILES) > 0) {
$max = count($_FILES) - 1;
$keys = array_keys($_FILES);
for ($i = $max; $i >= 0; $i--) {
$key = $keys[$i];
$value = $_FILES[$key];
// Handle array input
if (is_array($value['name']) === false) {
$values['index'] = $key;
$this->file[$key] = InputFile::createFromArray(array_merge($value, $values));
continue;
}
$subMax = count($value['name']) - 1;
$keys = array_keys($value['name']);
$output = [];
for ($i = $subMax; $i >= 0; $i--) {
$output[$keys[$i]] = InputFile::createFromArray([
'index' => $key,
'error' => $value['error'][$keys[$i]],
'tmp_name' => $value['tmp_name'][$keys[$i]],
'type' => $value['type'][$keys[$i]],
'size' => $value['size'][$keys[$i]],
'name' => $value['name'][$keys[$i]],
]);
}
$this->file[$key] = $output;
}
}
$this->parseFiles();
}
protected function handleGetPost($array)
public function parseFiles()
{
$tmp = [];
if (count($_FILES) === 0) {
return [];
}
$list = [];
foreach ($_FILES as $key => $value) {
// Handle array input
if (is_array($value['name']) === false) {
$values['index'] = $key;
$list[$key] = InputFile::createFromArray(array_merge($value, $values));
continue;
}
$keys = [];
$list = array_merge_recursive($list, [$key => $this->rearrangeFiles($value['name'], $keys, $value)]);
}
return $list;
}
protected function rearrangeFiles(array $values, &$index, $original)
{
$output = [];
$getItem = function ($key, $property = 'name') use ($original, $index) {
$path = $original[$property];
foreach (array_values($index) as $i) {
$path = $path[$i];
}
return $path[$key];
};
foreach ($values as $key => $value) {
if (is_array($getItem($key)) === false) {
$file = InputFile::createFromArray([
'index' => $key,
'error' => $getItem($key, 'error'),
'tmp_name' => $getItem($key, 'tmp_name'),
'type' => $getItem($key, 'type'),
'size' => $getItem($key, 'size'),
'filename' => $getItem($key, 'name'),
]);
$output = array_merge_recursive($output, [$key => $file]);
if (isset($output[$key])) {
$output[$key][] = $file;
} else {
$output[$key] = $file;
}
continue;
}
$index[] = $key;
$output = array_merge_recursive($output, [$key => $this->rearrangeFiles($value, $index, $original)]);
}
return $output;
}
protected function handleGetPost(array $array)
{
$list = [];
$max = count($array) - 1;
$keys = array_keys($array);
@@ -105,77 +142,110 @@ class Input
// Handle array input
if (is_array($value) === false) {
$tmp[$key] = new InputItem($key, $value);
$list[$key] = new InputItem($key, $value);
continue;
}
$subMax = count($value) - 1;
$keys = array_keys($value);
$output = [];
$output = $this->handleGetPost($value);
for ($i = $subMax; $i >= 0; $i--) {
$output[$keys[$i]] = new InputItem($key, $value[$keys[$i]]);
}
$tmp[$key] = $output;
$list[$key] = $output;
}
return $tmp;
return $list;
}
public function findPost($index, $default = null)
/**
* Find post-value by index or return default value.
*
* @param string $index
* @param string|null $defaultValue
* @return InputItem|string
*/
public function findPost($index, $defaultValue = null)
{
return isset($this->post[$index]) ? $this->post[$index] : $default;
return $this->post[$index] ?? $defaultValue;
}
public function findFile($index, $default = null)
/**
* Find file by index or return default value.
*
* @param string $index
* @param string|null $defaultValue
* @return InputFile|string
*/
public function findFile($index, $defaultValue = null)
{
return isset($this->file[$index]) ? $this->file[$index] : $default;
return $this->file[$index] ?? $defaultValue;
}
public function findGet($index, $default = null)
/**
* Find parameter/query-string by index or return default value.
*
* @param string $index
* @param string|null $defaultValue
* @return InputItem|string
*/
public function findGet($index, $defaultValue = null)
{
return isset($this->get[$index]) ? $this->get[$index] : $default;
return $this->get[$index] ?? $defaultValue;
}
public function getObject($index, $default = null, $method = null)
/**
* Get input object
*
* @param string $index
* @param string|null $defaultValue
* @param array|string|null $methods
* @return IInputItem|string
*/
public function getObject($index, $defaultValue = null, $methods = null)
{
if ($methods !== null && is_string($methods) === true) {
$methods = [$methods];
}
$element = null;
if ($method === null || strtolower($method) === 'get') {
if ($methods === null || in_array('get', $methods)) {
$element = $this->findGet($index);
}
if ($element === null && $method === null || strtolower($method) === 'post') {
if (($element === null && $methods === null) || ($methods !== null && in_array('post', $methods))) {
$element = $this->findPost($index);
}
if ($element === null && $method === null || strtolower($method) === 'file') {
if (($element === null && $methods === null) || ($methods !== null && in_array('file', $methods))) {
$element = $this->findFile($index);
}
return ($element === null) ? $default : $element;
return ($element !== null) ? $element : $defaultValue;
}
/**
* Get input element value matching index
*
* @param string $index
* @param string|null $default
* @param string|null $method
* @param string|null $defaultValue
* @param array|string|null $methods
* @return InputItem|string
*/
public function get($index, $default = null, $method = null)
public function get($index, $defaultValue = null, $methods = null)
{
$input = $this->getObject($index, $default, $method);
$input = $this->getObject($index, $defaultValue, $methods);
if ($input instanceof InputItem) {
return (trim($input->getValue()) === '') ? $default : $input->getValue();
return (trim($input->getValue()) === '') ? $defaultValue : $input->getValue();
}
return $input;
}
/**
* Check if a input-item exist
*
* @param string $index
* @return bool
*/
public function exists($index)
{
return ($this->getObject($index) !== null);
@@ -194,7 +264,7 @@ class Input
$contents = file_get_contents('php://input');
if (stripos(trim($contents), '{') === 0) {
if (strpos(trim($contents), '{') === 0) {
$output = json_decode($contents, true);
if ($output === false) {
$output = [];
@@ -206,11 +276,7 @@ class Input
if ($filter !== null) {
$output = array_filter($output, function ($key) use ($filter) {
if (in_array($key, $filter)) {
return true;
}
return false;
return (in_array($key, $filter) === true);
}, ARRAY_FILTER_USE_KEY);
}
+100 -34
View File
@@ -4,8 +4,8 @@ namespace Pecee\Http\Input;
class InputFile implements IInputItem
{
public $index;
public $value;
public $name;
public $filename;
public $size;
public $type;
public $error;
@@ -21,7 +21,9 @@ class InputFile implements IInputItem
/**
* Create from array
*
* @param array $values
* @throws \InvalidArgumentException
* @return static
*/
public static function createFromArray(array $values)
@@ -30,12 +32,22 @@ class InputFile implements IInputItem
throw new \InvalidArgumentException('Index key is required');
}
/* Easy way of ensuring that all indexes-are set and not filling the screen with isset() */
$values = array_merge([
'tmp_name' => null,
'type' => null,
'size' => null,
'name' => null,
'error' => null,
], $values);
$input = new static($values['index']);
$input->setError(isset($values['error']) ? $values['error'] : null);
$input->setName(isset($values['name']) ? $values['name'] : null);
$input->setSize(isset($values['size']) ? $values['size'] : null);
$input->setType(isset($values['type']) ? $values['type'] : null);
$input->setTmpName(isset($values['tmp_name']) ? $values['tmp_name'] : null);
$input->setError($values['error'])
->setSize($values['size'])
->setType($values['type'])
->setTmpName($values['tmp_name'])
->setFilename($values['name']);
return $input;
}
@@ -48,6 +60,11 @@ class InputFile implements IInputItem
return $this->index;
}
/**
* Set input index
* @param string $index
* @return static $this
*/
public function setIndex($index)
{
$this->index = $index;
@@ -75,6 +92,10 @@ class InputFile implements IInputItem
return $this;
}
/**
* Get mime-type of file
* @return string
*/
public function getMime()
{
return $this->getType();
@@ -100,12 +121,19 @@ class InputFile implements IInputItem
return $this;
}
/**
* Returns extension without "."
*
* @return string
*/
public function getExtension()
{
return pathinfo($this->getName(), PATHINFO_EXTENSION);
}
/**
* Get human friendly name
*
* @return string
*/
public function getName()
@@ -113,6 +141,13 @@ class InputFile implements IInputItem
return $this->name;
}
/**
* Set human friendly name.
* Useful for adding validation etc.
*
* @param string $name
* @return static $this
*/
public function setName($name)
{
$this->name = $name;
@@ -120,29 +155,63 @@ class InputFile implements IInputItem
return $this;
}
/**
* Set filename
*
* @param string $name
* @return static $this
*/
public function setFilename($name)
{
$this->filename = $name;
return $this;
}
/**
* Get filename
*
* @return string mixed
*/
public function getFilename()
{
return $this->filename;
}
/**
* Move the uploaded temporary file to it's new home
*
* @param string $destination
* @return bool
*/
public function move($destination)
{
return move_uploaded_file($this->tmpName, $destination);
}
/**
* Get file contents
*
* @return string
*/
public function getContents()
{
return file_get_contents($this->tmpName);
}
public function setValue($value)
{
$this->value = $value;
return $this;
}
/**
* Return true if an upload error occured.
*
* @return bool
*/
public function hasError()
{
return ($this->getError() !== 0);
}
/**
* Get upload-error code.
*
* @return string
*/
public function getError()
@@ -152,6 +221,7 @@ class InputFile implements IInputItem
/**
* Set error
*
* @param int $error
* @return static $this
*/
@@ -162,27 +232,6 @@ class InputFile implements IInputItem
return $this;
}
public function toArray()
{
return [
'tmp_name' => $this->tmpName,
'type' => $this->type,
'size' => $this->size,
'name' => $this->name,
'error' => $this->error,
];
}
public function __toString()
{
return $this->getValue();
}
public function getValue()
{
return $this->getTmpName();
}
/**
* @return string
*/
@@ -202,4 +251,21 @@ class InputFile implements IInputItem
return $this;
}
public function __toString()
{
return $this->getTmpName();
}
public function toArray()
{
return [
'tmp_name' => $this->tmpName,
'type' => $this->type,
'size' => $this->size,
'name' => $this->filename,
'error' => $this->error,
];
}
}
+1
View File
@@ -75,4 +75,5 @@ class InputItem implements IInputItem
{
return (string)$this->value;
}
}