Lae/app/Helpers/ApiResponse.php

153 lines
3.8 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
{
2022-12-18 09:39:51 +00:00
// REST 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
return match ($status) {
200 => $this->success($response),
201 => $this->created($response),
204 => $this->noContent(),
2022-11-26 12:20:47 +00:00
400 => $this->badRequest($response),
2022-11-20 14:35:53 +00:00
401 => $this->serviceUnavailable(),
2022-11-26 12:20:47 +00:00
403 => $this->forbidden($response),
2022-11-20 14:35:53 +00:00
404 => $this->notFound($response),
405 => $this->methodNotAllowed(),
429 => $this->tooManyRequests(),
2022-12-19 06:13:12 +00:00
500 => $this->serverError($response),
2022-11-20 14:35:53 +00:00
2022-12-18 03:54:01 +00:00
default => response()->json($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
{
2022-12-25 13:39:19 +00:00
// if data is string, then it is error message
if (is_string($data)) {
$data = [
'message' => $data,
];
}
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
{
2022-12-11 11:47:30 +00:00
return $this->error($message);
2022-08-12 07:56:56 +00:00
}
// 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-12-11 11:47:30 +00:00
public function success($data = [], $status = 200): JsonResponse
{
2022-12-11 11:47:30 +00:00
return $this->apiResponse($data, $status);
}
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);
}
}