mirror of
https://github.com/skipperbent/simple-php-router.git
synced 2026-06-17 08:47:52 +00:00
Development
- all() in Input class now returns correct array. - all() now supports json data. - Minor bugfixes.
This commit is contained in:
@@ -38,17 +38,31 @@ class Input {
|
|||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function all(array $filter = null) {
|
public function all(array $filter = null) {
|
||||||
$output = $this->get->getData();
|
|
||||||
$output = array_merge($output, $this->post->getData());
|
|
||||||
|
|
||||||
if($filter !== null) {
|
$output = $_POST;
|
||||||
$tmp = array();
|
|
||||||
foreach($output as $key => $val) {
|
if($this->request->getMethod() === 'post') {
|
||||||
if(in_array($key, $filter)) {
|
|
||||||
$tmp[$key] = $val;
|
$contents = file_get_contents('php://input');
|
||||||
|
|
||||||
|
if (stripos(trim($contents), '{') === 0) {
|
||||||
|
$output = json_decode($contents, true);
|
||||||
|
if($output === false) {
|
||||||
|
$output = array();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return $tmp;
|
}
|
||||||
|
|
||||||
|
$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;
|
||||||
@@ -93,7 +107,7 @@ class Input {
|
|||||||
|
|
||||||
if($item !== null) {
|
if($item !== null) {
|
||||||
|
|
||||||
if(is_array($item) || $item instanceof InputFile) {
|
if($item instanceof InputCollection || $item instanceof InputFile) {
|
||||||
return $item;
|
return $item;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -117,10 +131,10 @@ class Input {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
$output = array();
|
$output = new InputCollection();
|
||||||
|
|
||||||
foreach($get as $k => $g) {
|
foreach($get as $k => $g) {
|
||||||
$output[$k] = new InputItem($k, $g);
|
$output->{$k} = new InputItem($k, $g);
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->get->{$key} = $output;
|
$this->get->{$key} = $output;
|
||||||
@@ -131,12 +145,10 @@ class Input {
|
|||||||
public function setPost() {
|
public function setPost() {
|
||||||
$this->post = new InputCollection();
|
$this->post = new InputCollection();
|
||||||
|
|
||||||
$postVars = array();
|
$postVars = $_POST;
|
||||||
|
|
||||||
if(isset($_SERVER['REQUEST_METHOD']) && in_array($_SERVER['REQUEST_METHOD'], ['PUT', 'PATCH', 'DELETE'])) {
|
if(in_array($this->request->getMethod(), ['put', 'patch', 'delete'])) {
|
||||||
parse_str(file_get_contents('php://input'), $postVars);
|
parse_str(file_get_contents('php://input'), $postVars);
|
||||||
} else {
|
|
||||||
$postVars = $_POST;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if(count($postVars)) {
|
if(count($postVars)) {
|
||||||
@@ -147,10 +159,10 @@ class Input {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
$output = array();
|
$output = new InputCollection();
|
||||||
|
|
||||||
foreach($post as $k=>$p) {
|
foreach($post as $k => $p) {
|
||||||
$output[$k] = new InputItem($k, $p);
|
$output->{$k} = new InputItem($k, $p);
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->post->{strtolower($key)} = $output;
|
$this->post->{strtolower($key)} = $output;
|
||||||
@@ -178,7 +190,7 @@ class Input {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
$output = array();
|
$output = new InputCollection();
|
||||||
|
|
||||||
foreach($value['name'] as $k=>$val) {
|
foreach($value['name'] as $k=>$val) {
|
||||||
// Strip empty values
|
// Strip empty values
|
||||||
@@ -189,7 +201,7 @@ class Input {
|
|||||||
$file->setType($value['type'][$k]);
|
$file->setType($value['type'][$k]);
|
||||||
$file->setTmpName($value['tmp_name'][$k]);
|
$file->setTmpName($value['tmp_name'][$k]);
|
||||||
$file->setError($value['error'][$k]);
|
$file->setError($value['error'][$k]);
|
||||||
$output[$k] = $file;
|
$output->{$k} = $file;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,8 +5,21 @@ use Pecee\Http\Input\Input;
|
|||||||
|
|
||||||
class Request {
|
class Request {
|
||||||
|
|
||||||
|
protected static $instance;
|
||||||
|
|
||||||
protected $data;
|
protected $data;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return new instance
|
||||||
|
* @return static
|
||||||
|
*/
|
||||||
|
public static function getInstance() {
|
||||||
|
if(self::$instance === null) {
|
||||||
|
self::$instance = new static();
|
||||||
|
}
|
||||||
|
return self::$instance;
|
||||||
|
}
|
||||||
|
|
||||||
public function __construct() {
|
public function __construct() {
|
||||||
$this->data = array();
|
$this->data = array();
|
||||||
$this->host = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : array();
|
$this->host = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : array();
|
||||||
|
|||||||
Reference in New Issue
Block a user