Lae/app/Helpers/ApiResponse.php

175 lines
4.5 KiB
PHP
Raw Normal View History

2022-08-12 07:56:56 +00:00
<?php
namespace App\Helpers;
2022-11-20 14:35:53 +00:00
use Illuminate\Http\JsonResponse;
use Illuminate\Pagination\Paginator;
2022-08-12 07:56:56 +00:00
trait ApiResponse
{
// RESTful API response
2022-11-20 14:35:53 +00:00
public function moduleResponse($response, int $status = 200): JsonResponse
{
2022-11-20 14:35:53 +00:00
if (isset($response['data'])) {
$response = $response['data'];
}
return match ($status) {
200 => $this->success($response),
201 => $this->created($response),
204 => $this->noContent(),
400 => $this->badRequest(),
401 => $this->serviceUnavailable(),
403 => $this->forbidden(),
404 => $this->notFound($response),
405 => $this->methodNotAllowed(),
429 => $this->tooManyRequests(),
500 => $this->serverError(),
2022-11-25 11:00:58 +00:00
default => response()->json($response['data'] ?? $response, $status),
2022-11-20 14:35:53 +00:00
};
}
2022-11-20 14:35:53 +00:00
public function notFound($message = 'Not found'): JsonResponse
{
return $this->error($message, 404);
}
2022-11-20 14:35:53 +00:00
public function error($message = '', $code = 400): JsonResponse
{
return $this->apiResponse($message, $code);
}
2022-11-20 14:35:53 +00:00
public function apiResponse($data = [], $status = 200): JsonResponse
2022-08-12 07:56:56 +00:00
{
// if data is paginated, return paginated data
2022-11-20 14:35:53 +00:00
if ($data instanceof Paginator) {
2022-08-12 07:56:56 +00:00
$data = $data->toArray();
2022-11-06 14:57:01 +00:00
$data['data'] = $data['data'] ?? [];
2022-08-12 07:56:56 +00:00
$data['meta'] = [
2022-11-06 14:57:01 +00:00
'per_page' => $data['per_page'] ?? 0,
'current_page' => $data['current_page'] ?? 0,
'from' => $data['from'] ?? 0,
'to' => $data['to'] ?? 0,
2022-08-12 07:56:56 +00:00
];
$data['paginate'] = 1;
} else {
$data = [
'data' => $data,
];
}
2022-11-20 14:35:53 +00:00
2022-08-12 07:56:56 +00:00
$data['status'] = $status;
if ($status >= 200 && $status <= 299) {
$data['success'] = 1;
} else {
$data['success'] = 0;
}
2022-10-06 10:44:47 +00:00
return response()->json($data, $status)->setEncodingOptions(JSON_UNESCAPED_UNICODE);
2022-08-12 07:56:56 +00:00
}
2022-11-20 14:35:53 +00:00
public function forbidden($message = 'Forbidden'): JsonResponse
2022-08-12 07:56:56 +00:00
{
return $this->error($message, 403);
}
2022-11-20 14:35:53 +00:00
public function unauthorized($message = 'Unauthorized'): JsonResponse
2022-08-12 07:56:56 +00:00
{
return $this->error($message, 401);
}
2022-11-20 14:35:53 +00:00
public function badRequest($message = 'Bad request'): JsonResponse
2022-08-12 07:56:56 +00:00
{
return $this->error($message, 400);
}
// bad request
2022-11-20 14:35:53 +00:00
public function created($message = 'Created'): JsonResponse
2022-08-12 07:56:56 +00:00
{
return $this->success($message, 201);
}
// created
2022-11-20 14:35:53 +00:00
public function success($data = []): JsonResponse
{
return $this->apiResponse($data, 200);
}
2022-08-12 07:56:56 +00:00
// accepted
2022-11-20 14:35:53 +00:00
public function accepted($message = 'Accepted'): JsonResponse
2022-08-12 07:56:56 +00:00
{
return $this->success($message, 202);
}
// no content
2022-11-20 14:35:53 +00:00
public function noContent($message = 'No content'): JsonResponse
2022-08-12 07:56:56 +00:00
{
return $this->success($message, 204);
}
// updated
2022-11-20 14:35:53 +00:00
public function updated($message = 'Updated'): JsonResponse
2022-08-12 07:56:56 +00:00
{
return $this->success($message, 200);
}
// deleted
2022-11-20 14:35:53 +00:00
public function deleted($message = 'Deleted'): JsonResponse
2022-08-12 07:56:56 +00:00
{
return $this->success($message, 200);
}
// not allowed
2022-11-20 14:35:53 +00:00
public function notAllowed($message = 'Not allowed'): JsonResponse
2022-08-12 07:56:56 +00:00
{
return $this->error($message, 405);
}
// conflict
2022-11-20 14:35:53 +00:00
public function conflict($message = 'Conflict'): JsonResponse
2022-08-12 07:56:56 +00:00
{
return $this->error($message, 409);
}
// too many requests
2022-11-20 14:35:53 +00:00
public function tooManyRequests($message = 'Too many requests'): JsonResponse
2022-08-12 07:56:56 +00:00
{
return $this->error($message, 429);
}
// server error
2022-11-20 14:35:53 +00:00
public function serverError($message = 'Server error'): JsonResponse
2022-08-12 07:56:56 +00:00
{
return $this->error($message, 500);
}
// service unavailable
2022-11-20 14:35:53 +00:00
public function serviceUnavailable($message = 'Service unavailable'): JsonResponse
2022-08-12 07:56:56 +00:00
{
return $this->error($message, 503);
}
// method not allowed
2022-11-20 14:35:53 +00:00
public function methodNotAllowed($message = 'Method not allowed'): JsonResponse
2022-08-12 07:56:56 +00:00
{
return $this->error($message, 405);
}
// not acceptable
2022-11-20 14:35:53 +00:00
public function notAcceptable($message = 'Not acceptable'): JsonResponse
2022-08-12 07:56:56 +00:00
{
return $this->error($message, 406);
}
// precondition failed
2022-11-20 14:35:53 +00:00
public function preconditionFailed($message = 'Precondition failed'): JsonResponse
2022-08-12 07:56:56 +00:00
{
return $this->error($message, 412);
}
}