Merge pull request #127 from skipperbent/v1-development

Development
This commit is contained in:
Simon Sessingø
2016-11-08 18:21:41 +02:00
committed by GitHub
2 changed files with 41 additions and 23 deletions
+39 -21
View File
@@ -20,7 +20,13 @@ class Input {
*/ */
public $file; public $file;
public function __construct() { /**
* @var Request
*/
protected $request;
public function __construct(Request &$request) {
$this->request = $request;
$this->setGet(); $this->setGet();
$this->setPost(); $this->setPost();
$this->setFile(); $this->setFile();
@@ -32,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()); $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 = array();
}
}
}
$output = array_merge($_GET, $output);
if($filter !== null) { if($filter !== null) {
$tmp = array(); $output = array_filter($output, function ($key) use ($filter) {
foreach($output as $key => $val) {
if (in_array($key, $filter)) { if (in_array($key, $filter)) {
$tmp[$key] = $val; return true;
} }
}
return $tmp; return false;
}, ARRAY_FILTER_USE_KEY);
} }
return $output; return $output;
@@ -58,7 +78,7 @@ class Input {
return ($key !== null) ? $element[$key] : $element; return ($key !== null) ? $element[$key] : $element;
} }
if(Request::getInstance()->getMethod() !== 'get') { if($this->request->getMethod() !== 'get') {
$element = $this->post->findFirst($index); $element = $this->post->findFirst($index);
@@ -87,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;
} }
@@ -111,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;
@@ -125,12 +145,10 @@ class Input {
public function setPost() { public function setPost() {
$this->post = new InputCollection(); $this->post = new InputCollection();
$postVars = array();
if(isset($_SERVER['REQUEST_METHOD']) && in_array($_SERVER['REQUEST_METHOD'], ['PUT', 'PATCH', 'DELETE'])) {
parse_str(file_get_contents('php://input'), $postVars);
} else {
$postVars = $_POST; $postVars = $_POST;
if(in_array($this->request->getMethod(), ['put', 'patch', 'delete'])) {
parse_str(file_get_contents('php://input'), $postVars);
} }
if(count($postVars)) { if(count($postVars)) {
@@ -141,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;
@@ -172,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
@@ -183,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;
} }
} }
+1 -1
View File
@@ -26,7 +26,7 @@ class Request {
$this->uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : array(); $this->uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : array();
$this->method = (isset($_POST['_method'])) ? strtolower($_POST['_method']) : (isset($_SERVER['REQUEST_METHOD']) ? strtolower($_SERVER['REQUEST_METHOD']) : array()); $this->method = (isset($_POST['_method'])) ? strtolower($_POST['_method']) : (isset($_SERVER['REQUEST_METHOD']) ? strtolower($_SERVER['REQUEST_METHOD']) : array());
$this->headers = $this->getAllHeaders(); $this->headers = $this->getAllHeaders();
$this->input = new Input(); $this->input = new Input($this);
} }
protected function getAllHeaders() { protected function getAllHeaders() {