-
-
Save it-can/ca1a632fbd8fddffd1fcbfd008b6d9f6 to your computer and use it in GitHub Desktop.
Trait for easy creation of API responses
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
<?php | |
namespace App\Traits; | |
use Illuminate\Http\Request; | |
use Symfony\Component\HttpFoundation\Response as SymfonyResponse; | |
trait ApiRespond | |
{ | |
/** | |
* The status code of the response. | |
* | |
* @var integer | |
*/ | |
protected $_httpStatusCode = SymfonyResponse::HTTP_OK; | |
/** | |
* Status OK "constant" | |
* | |
* @var string | |
*/ | |
protected static $statusOk = 'ok'; | |
/** | |
* Status error "constant" | |
* | |
* @var string | |
*/ | |
protected static $statusBad = 'error'; | |
/** | |
* The response array | |
* | |
* @var array | |
*/ | |
protected $response = []; | |
/** | |
* The request object. | |
* | |
* @var Illuminate\Http\Request | |
*/ | |
protected $request; | |
public function __construct(Request $request) | |
{ | |
$this->request = $request; | |
} | |
/** | |
* Respond with an error. | |
* | |
* @param mixed $message The message you want to display. | |
* @param array $data Additional data to include in the response. | |
* @param int|integer $code Optional status code. Default 500. | |
* @return string | |
*/ | |
public function respondWithError($message, $code = SymfonyResponse::HTTP_INTERNAL_SERVER_ERROR, array $data = []) | |
{ | |
$response = [ | |
'errors' => [ | |
'status' => self::$statusBad, | |
'message' => $message, | |
'code' => $code, | |
] | |
]; | |
return $this->setStatusCode($code) | |
->respond($response) | |
->bind($data) | |
->output(); | |
} | |
/** | |
* Response when a resource is created. | |
* | |
* @param mixed $message The message you want to display. | |
* @param array $data Additional data to include in the response. | |
* @return string | |
*/ | |
public function respondCreated(array $data = []) | |
{ | |
return $this->setStatusCode(SymfonyResponse::HTTP_CREATED) | |
->respond($this->successStub()) | |
->bind($data) | |
->output(); | |
} | |
/** | |
* Successful response. | |
* | |
* @param mixed $message The message you want to display. | |
* @param array $data Additional data to include in the response. | |
* @return string | |
*/ | |
public function respondWithSuccess(array $data = []) | |
{ | |
return $this->respond($this->successStub())->bind($data)->output(); | |
} | |
/** | |
* Outputs response as JSON | |
* | |
* @return string | |
*/ | |
protected function output() | |
{ | |
return response()->json($this->response, $this->_httpStatusCode); | |
} | |
/** | |
* Sets the respond attribute | |
* @param array $response The response array | |
* @return $this | |
*/ | |
protected function respond(array $response) | |
{ | |
$this->response = $response; | |
return $this; | |
} | |
/** | |
* Sets the status code | |
* | |
* @param int $statusCode | |
* @return $this | |
*/ | |
protected function setStatusCode($statusCode) | |
{ | |
$this->_httpStatusCode = $statusCode; | |
return $this; | |
} | |
/** | |
* Array merge alias. | |
* | |
* @param array $response The current response array | |
* @param array $data The additional data that is being added | |
* @return array The merged arrays | |
*/ | |
protected function bind(array $data = []) | |
{ | |
if ($data) { | |
$this->response = array_merge($this->response, $data); | |
} | |
return $this; | |
} | |
/** | |
* Generates a stub array for a success response. | |
* | |
* @param mixed $message The message to show | |
* @return array The success array stub | |
*/ | |
protected function successStub() | |
{ | |
return ['status' => self::$statusOk]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment