Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a851162
commit 3a438bd
Showing
1 changed file
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
defined('BASEPATH') OR exit('No direct script access allowed'); | ||
|
||
class Response extends CI_Controller | ||
{ | ||
/** | ||
* [protected HTTP Response Code] | ||
* @var int | ||
*/ | ||
protected $code; | ||
/** | ||
* [protected Response Data] | ||
* @var mixed | ||
*/ | ||
protected $data; | ||
/** | ||
* [protected Shoud Response be JSON Encoded?] | ||
* @var bool | ||
*/ | ||
protected $json; | ||
function __construct($data, int $code) | ||
{ | ||
$this->data = $data; | ||
$this->code = $code; | ||
} | ||
/** | ||
* [__toString description] | ||
* @date 2019-11-09 | ||
* @return string [description] | ||
*/ | ||
public function __toString():string | ||
{ | ||
return !$this->json ? $this->data : n_encode($this->data); | ||
} | ||
/** | ||
* [setJSON description] | ||
* @date 2019-11-09 | ||
* @param bool $json [description] | ||
* @return Response [description] | ||
*/ | ||
public function setJSON(bool $json):Response | ||
{ | ||
$this->json = $json; | ||
return $this; | ||
} | ||
public function send():void | ||
{ | ||
http_response_code($this->code); | ||
echo !$this->json ? $this->data : n_encode($this->data); | ||
if (get_instance()->config->item('rest')['response_exit']) { | ||
exit(EXIT_SUCCESS); | ||
} | ||
} | ||
} | ||
?> |