Compare commits

...

6 Commits

Author SHA1 Message Date
Simon Sessingø 4975f24fee Merge pull request #285 from skipperbent/v1-development
Added \JsonSerializable interface to Response->json (issue: #284).
2017-08-31 13:05:26 +02:00
Simon Sessingø 2952f6a3b6 Added \JsonSerializable interface to Response->json (issue: #284). 2017-08-31 11:59:58 +01:00
Simon Sessingø da7348ea82 Merge pull request #127 from skipperbent/v1-development
Development
2016-11-08 18:21:41 +02:00
Simon Sessingø 16e326ad9f Development
- all() in Input class now returns correct array.
- all() now supports json data.
- Minor bugfixes.
2016-11-08 18:16:17 +02:00
Simon Sessingø 0002b45d18 Merge pull request #123 from skipperbent/v1-development
Fixed RouterGroup not pushing multiple middlewares properly
2016-11-07 04:57:30 +01:00
Simon Sessingø d9b2328e82 Fixed RouterGroup not pushing multiple middlewares properly 2016-11-07 04:56:47 +01:00
4 changed files with 56 additions and 29 deletions
+40 -22
View File
@@ -20,7 +20,13 @@ class Input {
*/
public $file;
public function __construct() {
/**
* @var Request
*/
protected $request;
public function __construct(Request &$request) {
$this->request = $request;
$this->setGet();
$this->setPost();
$this->setFile();
@@ -32,17 +38,31 @@ class Input {
* @return array
*/
public function all(array $filter = null) {
$output = $this->get->getData();
$output = array_merge($output, $this->post->getData());
if($filter !== null) {
$tmp = array();
foreach($output as $key => $val) {
if(in_array($key, $filter)) {
$tmp[$key] = $val;
$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();
}
}
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;
@@ -58,7 +78,7 @@ class Input {
return ($key !== null) ? $element[$key] : $element;
}
if(Request::getInstance()->getMethod() !== 'get') {
if($this->request->getMethod() !== 'get') {
$element = $this->post->findFirst($index);
@@ -87,7 +107,7 @@ class Input {
if($item !== null) {
if(is_array($item) || $item instanceof InputFile) {
if($item instanceof InputCollection || $item instanceof InputFile) {
return $item;
}
@@ -111,10 +131,10 @@ class Input {
continue;
}
$output = array();
$output = new InputCollection();
foreach($get as $k => $g) {
$output[$k] = new InputItem($k, $g);
$output->{$k} = new InputItem($k, $g);
}
$this->get->{$key} = $output;
@@ -125,12 +145,10 @@ class Input {
public function setPost() {
$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);
} else {
$postVars = $_POST;
}
if(count($postVars)) {
@@ -141,10 +159,10 @@ class Input {
continue;
}
$output = array();
$output = new InputCollection();
foreach($post as $k=>$p) {
$output[$k] = new InputItem($k, $p);
foreach($post as $k => $p) {
$output->{$k} = new InputItem($k, $p);
}
$this->post->{strtolower($key)} = $output;
@@ -172,7 +190,7 @@ class Input {
continue;
}
$output = array();
$output = new InputCollection();
foreach($value['name'] as $k=>$val) {
// Strip empty values
@@ -183,7 +201,7 @@ class Input {
$file->setType($value['type'][$k]);
$file->setTmpName($value['tmp_name'][$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->method = (isset($_POST['_method'])) ? strtolower($_POST['_method']) : (isset($_SERVER['REQUEST_METHOD']) ? strtolower($_SERVER['REQUEST_METHOD']) : array());
$this->headers = $this->getAllHeaders();
$this->input = new Input();
$this->input = new Input($this);
}
protected function getAllHeaders() {
+10 -4
View File
@@ -69,13 +69,19 @@ class Response {
}
/**
* Json encode array
* @param array $value
* Json encode
* @param array|\JsonSerializable $value
* @throws \InvalidArgumentException;
*/
public function json(array $value) {
public function json($value) {
if(($value instanceof \JsonSerializable) === false && is_array($value) === false) {
throw new \InvalidArgumentException('Invalid type for parameter "value". Must be of type array or object implementing the \JsonSerializable interface.');
}
$this->header('Content-type: application/json');
echo json_encode($value);
die();
exit(0);
}
/**
+5 -2
View File
@@ -97,9 +97,12 @@ class RouterGroup extends RouterEntry {
if($this->getMiddleware() !== null && isset($settings['middleware'])) {
if(!is_array($this->getMiddleware())) {
$middlewares = [$this->getMiddleware(), $settings['middleware']];
$middlewares = [
$this->getMiddleware(),
$settings['middleware']
];
} else {
$middlewares = array_push($settings['middleware']);
$middlewares = array_push($settings['middleware'], $this->getMiddleware());
}
$settings['middleware'] = array_unique(array_reverse($middlewares));