Last active
July 13, 2021 07:53
-
-
Save allankiezel/8049c98ca3186e752da4fae689a07016 to your computer and use it in GitHub Desktop.
API Response Trait
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; | |
/** | |
* Class ApiResponseTrait | |
* | |
* @package App\Traits | |
* @author Allan Kiezel <allan.kiezel@gmail.com> | |
*/ | |
trait ApiResponseTrait | |
{ | |
/** | |
* Resource was successfully created | |
* | |
* @param $data | |
* @return \Illuminate\Http\JsonResponse | |
*/ | |
protected function createdResponse($data) | |
{ | |
$response = $this->successEnvelope(201, $data, 'Created'); | |
return response()->json($response, 201); | |
} | |
/** | |
* Resource was successfully deleted | |
* | |
* @return \Illuminate\Http\JsonResponse | |
*/ | |
protected function deletedResponse() | |
{ | |
$response = $this->successEnvelope(204, [], 'Deleted'); | |
return response()->json($response, 204); | |
} | |
/** | |
* Returns general error | |
* | |
* @param $errors | |
* @return \Illuminate\Http\JsonResponse | |
*/ | |
protected function errorResponse($errors) | |
{ | |
$response = $this->errorEnvelope(400, $errors); | |
return response()->json($response, 400); | |
} | |
/** | |
* Client does not have proper permissions to perform action. | |
* | |
* @param $errors | |
* @return \Illuminate\Http\JsonResponse | |
*/ | |
protected function insufficientPrivilegesResponse($errors) | |
{ | |
$response = $this->errorEnvelope(403, $errors, | |
'Forbidden'); | |
return response()->json($response, 403); | |
} | |
/** | |
* Returns a list of resources | |
* | |
* @param $data | |
* @return \Illuminate\Http\JsonResponse | |
*/ | |
protected function listResponse($data) | |
{ | |
$response = $this->successEnvelope(200, $data); | |
return response()->json($response); | |
} | |
/** | |
* Requested resource wasn't found | |
* | |
* @return \Illuminate\Http\JsonResponse | |
*/ | |
protected function notFoundResponse() | |
{ | |
$response = $this->errorEnvelope(404, [], 'Not Found'); | |
return response()->json($response, 404); | |
} | |
/** | |
* Return information for single resource | |
* | |
* @param $data | |
* @return \Illuminate\Http\JsonResponse | |
*/ | |
protected function showResponse($data) | |
{ | |
$response = $this->successEnvelope(200, $data); | |
return response()->json($response); | |
} | |
/** | |
* Return error when request is properly formatted, but contains validation errors | |
* | |
* @param $errors | |
* @return \Illuminate\Http\JsonResponse | |
*/ | |
protected function validationErrorResponse($errors) | |
{ | |
$response = $this->errorEnvelope(422, $errors, 'Unprocessable Entity'); | |
return response()->json($response, 422); | |
} | |
/** | |
* Standard error envelope structure | |
* | |
* @param int $status | |
* @param array $errors | |
* @param string $message | |
* @return array | |
*/ | |
private function errorEnvelope( | |
$status = 400, | |
$errors = [], | |
$message = 'Bad Request' | |
) { | |
return [ | |
'status' => $status, | |
'message' => $message, | |
'errors' => $errors, | |
]; | |
} | |
/** | |
* Standard success envelope structure | |
* | |
* @param int $status | |
* @param array $data | |
* @param string $message | |
* @return array | |
*/ | |
private function successEnvelope( | |
$status = 200, | |
$data = [], | |
$message = 'OK' | |
) { | |
return [ | |
'status' => $status, | |
'message' => $message, | |
'data' => $data, | |
]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can you give me a example of errorResponse?