Development

- Ensure that request-method is always lowercase.
- Fixed spaces instead of tabs to comply with PSR-2.
This commit is contained in:
Simon Sessingø
2016-11-25 12:51:45 +01:00
parent 2dd2d95af5
commit 1c515119b4
24 changed files with 3052 additions and 3066 deletions
+4 -4
View File
@@ -4,12 +4,12 @@ namespace Pecee\Http\Input;
interface IInputItem
{
public function getIndex();
public function getIndex();
public function getName();
public function getName();
public function getValue();
public function getValue();
public function __toString();
public function __toString();
}
+165 -192
View File
@@ -5,247 +5,220 @@ use Pecee\Http\Request;
class Input
{
/**
* @var array
*/
public $get = [];
/**
* @var array
*/
public $get = [];
/**
* @var array
*/
public $post = [];
/**
* @var array
*/
public $post = [];
/**
* @var array
*/
public $file = [];
/**
* @var array
*/
public $file = [];
/**
* @var Request
*/
protected $request;
/**
* @var Request
*/
protected $request;
protected $invalidContentType = false;
public function __construct(Request $request)
{
$this->request = $request;
protected $invalidContentTypes = [
'text/plain',
'application/x-www-form-urlencoded',
];
$this->parseInputs();
}
public function __construct(Request $request)
{
$this->request = $request;
protected function parseInputs()
{
/* Parse get requests */
if (count($_GET) > 0) {
$this->get = $this->handleGetPost($_GET);
}
if ($request->getMethod() !== 'get') {
/* Parse post requests */
$postVars = $_POST;
$requestContentType = $request->getHeader('http-content-type');
if (in_array($this->request->getMethod(), ['put', 'patch', 'delete']) === true) {
parse_str(file_get_contents('php://input'), $postVars);
}
$max = count($this->invalidContentTypes) - 1;
if (count($postVars) > 0) {
$this->post = $this->handleGetPost($postVars);
}
for ($i = $max; $i >= 0; $i--) {
/* Parse get requests */
$contentType = $this->invalidContentType[$i];
if (count($_FILES) > 0) {
if (stripos($requestContentType, $contentType) === 0) {
$this->invalidContentType = true;
break;
}
}
}
$max = count($_FILES) - 1;
$keys = array_keys($_FILES);
if ($this->invalidContentType === false) {
$this->parseInputs();
}
for ($i = $max; $i >= 0; $i--) {
}
$key = $keys[$i];
$value = $_FILES[$key];
protected function parseInputs()
{
/* Parse get requests */
if (count($_GET) > 0) {
$this->get = $this->handleGetPost($_GET);
}
// Handle array input
if (is_array($value['name']) === false) {
$values['index'] = $key;
$this->file[$key] = InputFile::createFromArray(array_merge($value, $values));
continue;
}
/* Parse post requests */
$postVars = $_POST;
$subMax = count($value['name']) - 1;
$keys = array_keys($value['name']);
$output = [];
if (in_array($this->request->getMethod(), ['put', 'patch', 'delete']) === true) {
parse_str(file_get_contents('php://input'), $postVars);
}
for ($i = $subMax; $i >= 0; $i--) {
if (count($postVars) > 0) {
$this->post = $this->handleGetPost($postVars);
}
$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]],
]);
/* Parse get requests */
}
if (count($_FILES) > 0) {
$this->file[$key] = $output;
}
}
}
$max = count($_FILES) - 1;
$keys = array_keys($_FILES);
protected function handleGetPost($array)
{
$tmp = [];
for ($i = $max; $i >= 0; $i--) {
$max = count($array) - 1;
$keys = array_keys($array);
$key = $keys[$i];
$value = $_FILES[$key];
for ($i = $max; $i >= 0; $i--) {
// Handle array input
if (is_array($value['name']) === false) {
$values['index'] = $key;
$this->file[$key] = InputFile::createFromArray(array_merge($value, $values));
continue;
}
$key = $keys[$i];
$value = $array[$key];
$subMax = count($value['name']) - 1;
$keys = array_keys($value['name']);
$output = [];
// Handle array input
if (is_array($value) === false) {
$tmp[$key] = new InputItem($key, $value);
continue;
}
for ($i = $subMax; $i >= 0; $i--) {
$subMax = count($value) - 1;
$keys = array_keys($value);
$output = [];
$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]],
]);
for ($i = $subMax; $i >= 0; $i--) {
$output[$keys[$i]] = new InputItem($key, $value[$keys[$i]]);
}
}
$tmp[$key] = $output;
}
$this->file[$key] = $output;
}
}
}
return $tmp;
}
protected function handleGetPost($array)
{
$tmp = [];
public function findPost($index, $default = null)
{
return isset($this->post[$index]) ? $this->post[$index] : $default;
}
$max = count($array) - 1;
$keys = array_keys($array);
public function findFile($index, $default = null)
{
return isset($this->file[$index]) ? $this->file[$index] : $default;
}
for ($i = $max; $i >= 0; $i--) {
public function findGet($index, $default = null)
{
return isset($this->get[$index]) ? $this->get[$index] : $default;
}
$key = $keys[$i];
$value = $array[$key];
public function getObject($index, $default = null, $method = null)
{
$element = null;
// Handle array input
if (is_array($value) === false) {
$tmp[$key] = new InputItem($key, $value);
continue;
}
if ($method === null || strtolower($method) === 'get') {
$element = $this->findGet($index);
}
$subMax = count($value) - 1;
$keys = array_keys($value);
$output = [];
if ($element === null && $method === null || strtolower($method) === 'post') {
$element = $this->findPost($index);
}
for ($i = $subMax; $i >= 0; $i--) {
$output[$keys[$i]] = new InputItem($key, $value[$keys[$i]]);
}
if ($element === null && $method === null || strtolower($method) === 'file') {
$element = $this->findFile($index);
}
$tmp[$key] = $output;
}
return ($element === null) ? $default : $element;
}
return $tmp;
}
/**
* Get input element value matching index
*
* @param string $index
* @param string|null $default
* @param string|null $method
* @return InputItem|string
*/
public function get($index, $default = null, $method = null)
{
$input = $this->getObject($index, $default, $method);
public function findPost($index, $default = null)
{
return isset($this->post[$index]) ? $this->post[$index] : $default;
}
if ($input instanceof InputItem) {
return (trim($input->getValue()) === '') ? $default : $input->getValue();
}
public function findFile($index, $default = null)
{
return isset($this->file[$index]) ? $this->file[$index] : $default;
}
return $input;
}
public function findGet($index, $default = null)
{
return isset($this->get[$index]) ? $this->get[$index] : $default;
}
public function exists($index)
{
return ($this->getObject($index) !== null);
}
public function getObject($index, $default = null, $method = null)
{
$element = null;
/**
* Get all get/post items
* @param array|null $filter Only take items in filter
* @return array
*/
public function all(array $filter = null)
{
if ($this->invalidContentType === true) {
return [];
}
if ($method === null || strtolower($method) === 'get') {
$element = $this->findGet($index);
}
$output = $_POST;
if ($element === null && $method === null || strtolower($method) === 'post') {
$element = $this->findPost($index);
}
if ($this->request->getMethod() === 'post') {
if ($element === null && $method === null || strtolower($method) === 'file') {
$element = $this->findFile($index);
}
$contents = file_get_contents('php://input');
return ($element === null) ? $default : $element;
}
if (stripos(trim($contents), '{') === 0) {
$output = json_decode($contents, true);
if ($output === false) {
$output = [];
}
}
}
/**
* Get input element value matching index
*
* @param string $index
* @param string|null $default
* @param string|null $method
* @return InputItem|string
*/
public function get($index, $default = null, $method = null)
{
$input = $this->getObject($index, $default, $method);
$output = array_merge($_GET, $output);
if ($input instanceof InputItem) {
return (trim($input->getValue()) === '') ? $default : $input->getValue();
}
if ($filter !== null) {
$output = array_filter($output, function ($key) use ($filter) {
if (in_array($key, $filter)) {
return true;
}
return $input;
}
return false;
}, ARRAY_FILTER_USE_KEY);
}
public function exists($index)
{
return ($this->getObject($index) !== null);
}
/**
* Get all get/post items
* @param array|null $filter Only take items in filter
* @return array
*/
public function all(array $filter = null)
{
if ($this->invalidContentType === true) {
return [];
}
$output = $_POST;
if ($this->request->getMethod() === 'post') {
$contents = file_get_contents('php://input');
if (stripos(trim($contents), '{') === 0) {
$output = json_decode($contents, true);
if ($output === false) {
$output = [];
}
}
}
$output = array_merge($_GET, $output);
if ($filter !== null) {
$output = array_filter($output, function ($key) use ($filter) {
if (in_array($key, $filter)) {
return true;
}
return false;
}, ARRAY_FILTER_USE_KEY);
}
return $output;
}
return $output;
}
}
+155 -155
View File
@@ -3,188 +3,188 @@ namespace Pecee\Http\Input;
class InputFile implements IInputItem
{
public $index;
public $name;
public $size;
public $type;
public $error;
public $tmpName;
public $index;
public $name;
public $size;
public $type;
public $error;
public $tmpName;
public function __construct($index)
{
$this->index = $index;
public function __construct($index)
{
$this->index = $index;
// Make the name human friendly, by replace _ with space
$this->name = ucfirst(str_replace('_', ' ', $this->index));
}
// Make the name human friendly, by replace _ with space
$this->name = ucfirst(str_replace('_', ' ', $this->index));
}
/**
* @return string
*/
public function getIndex()
{
return $this->index;
}
/**
* @return string
*/
public function getIndex()
{
return $this->index;
}
/**
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @return string
*/
public function getSize()
{
return $this->size;
}
/**
* @return string
*/
public function getSize()
{
return $this->size;
}
/**
* @return string
*/
public function getType()
{
return $this->type;
}
/**
* @return string
*/
public function getType()
{
return $this->type;
}
/**
* @return string
*/
public function getError()
{
return $this->error;
}
/**
* @return string
*/
public function getError()
{
return $this->error;
}
public function getMime()
{
return $this->getType();
}
public function getMime()
{
return $this->getType();
}
/**
* @return string
*/
public function getTmpName()
{
return $this->tmpName;
}
/**
* @return string
*/
public function getTmpName()
{
return $this->tmpName;
}
public function getExtension()
{
return pathinfo($this->getName(), PATHINFO_EXTENSION);
}
public function getExtension()
{
return pathinfo($this->getName(), PATHINFO_EXTENSION);
}
public function move($destination)
{
return move_uploaded_file($this->tmpName, $destination);
}
public function move($destination)
{
return move_uploaded_file($this->tmpName, $destination);
}
public function getContents()
{
return file_get_contents($this->tmpName);
}
public function getContents()
{
return file_get_contents($this->tmpName);
}
public function setName($name)
{
$this->name = $name;
public function setName($name)
{
$this->name = $name;
return $this;
}
return $this;
}
/**
* Set file temp. name
* @param string $name
* @return static $this
*/
public function setTmpName($name)
{
$this->tmpName = $name;
/**
* Set file temp. name
* @param string $name
* @return static $this
*/
public function setTmpName($name)
{
$this->tmpName = $name;
return $this;
}
return $this;
}
/**
* Set file size
* @param int $size
* @return static $this
*/
public function setSize($size)
{
$this->size = $size;
/**
* Set file size
* @param int $size
* @return static $this
*/
public function setSize($size)
{
$this->size = $size;
return $this;
}
return $this;
}
/**
* Set type
* @param string $type
* @return static $this
*/
public function setType($type)
{
$this->type = $type;
/**
* Set type
* @param string $type
* @return static $this
*/
public function setType($type)
{
$this->type = $type;
return $this;
}
return $this;
}
/**
* Set error
* @param int $error
* @return static $this
*/
public function setError($error)
{
$this->error = (int)$error;
/**
* Set error
* @param int $error
* @return static $this
*/
public function setError($error)
{
$this->error = (int)$error;
return $this;
}
return $this;
}
public function getValue()
{
return $this->getTmpName();
}
public function getValue()
{
return $this->getTmpName();
}
public function hasError()
{
return ($this->getError() !== 0);
}
public function hasError()
{
return ($this->getError() !== 0);
}
/**
* Create from array
* @param array $values
* @return static
*/
public static function createFromArray(array $values)
{
if (!isset($values['index'])) {
throw new \InvalidArgumentException('Index key is required');
}
/**
* Create from array
* @param array $values
* @return static
*/
public static function createFromArray(array $values)
{
if (!isset($values['index'])) {
throw new \InvalidArgumentException('Index key is required');
}
$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 = 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);
return $input;
}
return $input;
}
public function toArray()
{
return [
'tmp_name' => $this->tmpName,
'type' => $this->type,
'size' => $this->size,
'name' => $this->name,
'error' => $this->error,
];
}
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 __toString()
{
return $this->getValue();
}
}
+57 -55
View File
@@ -3,68 +3,70 @@ namespace Pecee\Http\Input;
class InputItem implements IInputItem
{
public $index;
public $name;
public $value;
public $index;
public $name;
public $value;
public function __construct($index, $value = null)
{
$this->index = $index;
$this->value = $value;
public function __construct($index, $value = null)
{
$this->index = $index;
$this->value = $value;
// Make the name human friendly, by replace _ with space
$this->name = ucfirst(str_replace('_', ' ', $this->index));
}
// Make the name human friendly, by replace _ with space
$this->name = ucfirst(str_replace('_', ' ', $this->index));
}
/**
* @return string
*/
public function getIndex()
{
return $this->index;
}
/**
* @return string
*/
public function getIndex()
{
return $this->index;
}
/**
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @return string
*/
public function getValue()
{
return $this->value;
}
/**
* @return string
*/
public function getValue()
{
return $this->value;
}
/**
* Set input name
* @param string $name
* @return static $this
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Set input name
* @param string $name
* @return static $this
*/
public function setName($name)
{
$this->name = $name;
/**
* Set input value
* @param string $value
* @return static $this
*/
public function setValue($value)
{
$this->value = $value;
return $this;
}
return $this;
}
public function __toString()
{
return (string)$this->value;
}
/**
* Set input value
* @param string $value
* @return static $this
*/
public function setValue($value)
{
$this->value = $value;
return $this;
}
public function __toString()
{
return (string)$this->value;
}
}