Last active
October 28, 2021 22:31
-
-
Save ivandez/94ee4e78f2620709e5caeb393e55296b to your computer and use it in GitHub Desktop.
Laravel 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; | |
/* | |
|-------------------------------------------------------------------------- | |
| Api Response Trait | |
|-------------------------------------------------------------------------- | |
| | |
| This trait will be used for any response we sent to clients. | |
| | |
| This trait is designed to be used with the named parameters of php 8 | |
| | |
| Fork from allankiezel/ApiResponseTrait.php | |
| | |
| https://gist.github.com/allankiezel/8049c98ca3186e752da4fae689a07016 | |
*/ | |
use Illuminate\Http\JsonResponse; | |
use JetBrains\PhpStorm\ArrayShape; | |
/** | |
* Class ApiResponseTrait | |
* | |
* @author Ivan Mendez | |
*/ | |
trait ApiResponseTrait | |
{ | |
/** | |
* Resource was successfully created | |
* | |
* @param $data | |
* @return JsonResponse | |
*/ | |
protected function createdResponse($data): JsonResponse | |
{ | |
$response = $this->successStructure(201, $data, 'Created'); | |
return response()->json($response, 201); | |
} | |
/** | |
* Operation was successfully executed | |
* | |
* @param int $status | |
* @param array|null $data | |
* @param string $message | |
* @return JsonResponse | |
*/ | |
protected function okResponse(int $status = 200, array $data = null, string $message = 'Ok'): JsonResponse | |
{ | |
$response = $this->successStructure($status, $data, $message); | |
return response()->json($response, $status); | |
} | |
/** | |
* Resource was successfully deleted | |
* | |
* @return JsonResponse | |
*/ | |
protected function deletedResponse(): JsonResponse | |
{ | |
$response = $this->successStructure(204, null, 'Deleted'); | |
return response()->json($response, 204); | |
} | |
/** | |
* Returns general error | |
* | |
* @param $errors | |
* @return JsonResponse | |
*/ | |
protected function errorResponse($errors): JsonResponse | |
{ | |
$response = $this->errorStructure(400, $errors); | |
return response()->json($response, 400); | |
} | |
/** | |
* Client does not have proper permissions to perform action. | |
* | |
* @param $errors | |
* @return JsonResponse | |
*/ | |
protected function insufficientPrivilegesResponse($errors): JsonResponse | |
{ | |
$response = $this->errorStructure(403, $errors, | |
'Forbidden'); | |
return response()->json($response, 403); | |
} | |
/** | |
* Client has lacks valid authentication credentials to perform action. | |
* | |
* @param $errors | |
* @return JsonResponse | |
*/ | |
protected function unauthorizedResponse($errors): JsonResponse | |
{ | |
$response = $this->errorStructure(401, $errors, | |
'Unauthorized'); | |
return response()->json($response, 401); | |
} | |
/** | |
* Returns a list of resources | |
* | |
* @param $data | |
* @return JsonResponse | |
*/ | |
protected function listResponse($data): JsonResponse | |
{ | |
$response = $this->successStructure(200, $data); | |
return response()->json($response); | |
} | |
/** | |
* Requested resource was not found | |
* @param array $errors | |
* @return JsonResponse | |
*/ | |
protected function notFoundResponse(array $errors = []): JsonResponse | |
{ | |
$response = $this->errorStructure(404, $errors, 'Not Found'); | |
return response()->json($response, 404); | |
} | |
/** | |
* Return information for single resource | |
* | |
* @param $data | |
* @return JsonResponse | |
*/ | |
protected function showResponse($data): JsonResponse | |
{ | |
$response = $this->successStructure(200, $data); | |
return response()->json($response); | |
} | |
/** | |
* Return error when request is properly formatted, but contains validation errors | |
* | |
* @param $errors | |
* @return JsonResponse | |
*/ | |
protected function validationErrorResponse($errors): JsonResponse | |
{ | |
$response = $this->errorStructure(422, $errors, 'Unprocessable Entity'); | |
return response()->json($response, 422); | |
} | |
/** | |
* Standard error structure | |
* | |
* @param int $status | |
* @param array|null $errors | |
* @param string $message | |
* @return array | |
*/ | |
#[ArrayShape(['status' => "int", 'message' => "string", 'errors' => "array|null"])] private function errorStructure( | |
int $status = 400, | |
array $errors = null, | |
string $message = 'Bad Request' | |
): array | |
{ | |
return [ | |
'status' => $status, | |
'message' => $message, | |
'errors' => $errors, | |
]; | |
} | |
/** | |
* Standard success structure | |
* | |
* @param int $status | |
* @param array|null $data | |
* @param string $message | |
* @return array | |
*/ | |
#[ArrayShape(['status' => "int", 'message' => "string", 'data' => "array|null"])] private function successStructure( | |
int $status = 200, | |
array $data = null, | |
string $message = 'OK' | |
):array { | |
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