Adding suport for Typed Returns and changins some return methods

This commit is contained in:
ahwelp
2023-01-24 13:39:08 +00:00
parent 189c69d2aa
commit a8089e483b
+28 -12
View File
@@ -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();
}
@@ -44,11 +44,11 @@ class Response{
}
//=======================================================
/**
*
* @ctag Response::header($key, $value);
*
*
*/
public static function header($key, $value){
$istance = self::getInstance();
@@ -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(){
echo (string) $this->_body;
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 = json_encode($object);
$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();
}
/**