mirror of
https://github.com/skipperbent/simple-php-router.git
synced 2026-07-11 03:22:13 +00:00
Development
- Ensure that request-method is always lowercase. - Fixed spaces instead of tabs to comply with PSR-2.
This commit is contained in:
+165
-192
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user