Adding suport for Typed Returns and changins some return methods
This commit is contained in:
+24
-8
@@ -29,14 +29,14 @@ class Response{
|
||||
private function __construct(){
|
||||
}
|
||||
|
||||
private static function newObj(){
|
||||
private static function newObj() : Response {
|
||||
if (!isset( self::$_instance )) {
|
||||
self::$_instance = new Response();
|
||||
}
|
||||
return self::$_instance;
|
||||
}
|
||||
|
||||
public function getInstance(){
|
||||
public function getInstance() : Response {
|
||||
if (!isset(self::$_instance)) {
|
||||
return self::newObj();
|
||||
}
|
||||
@@ -100,13 +100,16 @@ class Response{
|
||||
|
||||
private function sendHeaders(){
|
||||
foreach ($this->_headers as $key => $value) {
|
||||
header($key .': '. $value, false);
|
||||
header($key .': '. $value, true);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
private function sendBody(){
|
||||
if(!empty($this->_body)){
|
||||
echo (string) $this->_body;
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
@@ -119,6 +122,7 @@ class Response{
|
||||
$instance = self::getInstance();
|
||||
$instance->sendHeaders();
|
||||
$instance->sendBody();
|
||||
http_response_code($instance->_code);
|
||||
return $instance;
|
||||
}
|
||||
|
||||
@@ -128,12 +132,16 @@ class Response{
|
||||
* @ctag Response::json($object, $prefix);
|
||||
*
|
||||
*/
|
||||
public static function json($object, $jsonPrefix = null){
|
||||
public static function json($object, $jsonPrefix = null): Response{
|
||||
$instance = self::getInstance();
|
||||
|
||||
$instance->body('');
|
||||
|
||||
$json = $object;
|
||||
if(!is_string($object)){
|
||||
$json = json_encode($object);
|
||||
}
|
||||
|
||||
if (null !== $jsonPrefix) {
|
||||
$instance->header('Content-Type', 'text/javascript');
|
||||
$instance->body("$jsonPrefix($json);");
|
||||
@@ -141,6 +149,7 @@ class Response{
|
||||
$instance->header('Content-Type', 'application/json');
|
||||
$instance->body($json);
|
||||
}
|
||||
|
||||
return $instance;
|
||||
}
|
||||
|
||||
@@ -153,11 +162,18 @@ class Response{
|
||||
public static function redirect($url, $code = 302){
|
||||
$instance = self::getInstance();
|
||||
|
||||
$instance->_code = $code;
|
||||
$instance->header('Location', $url);
|
||||
if(is_ajax_request()){
|
||||
return $instance->json(['location' => $url])->send();
|
||||
}
|
||||
|
||||
## TODO
|
||||
header("Location: $url");
|
||||
die;
|
||||
|
||||
$instance->header('Location: ', $url)->send();
|
||||
|
||||
return $instance->send()->done();
|
||||
|
||||
$instance->body('');
|
||||
$instance->send();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user