Skip to content

Instantly share code, notes, and snippets.

@Muetze42
Last active October 14, 2022 19:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Muetze42/905dd2e901bc5c9de95597f48ff52773 to your computer and use it in GitHub Desktop.
Save Muetze42/905dd2e901bc5c9de95597f48ff52773 to your computer and use it in GitHub Desktop.
Laravel JsonResponse
<?php
namespace App\Common\Http;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\Routing\ResponseFactory;
use Illuminate\Http\Response;
class JsonResponse extends \Symfony\Component\HttpFoundation\Response
{
/**
* @param int $code
* @return string
*/
public static function getStatusText(int $code): string
{
return static::$statusTexts[$code];
}
/**
* @param array|string|null $message
* @param array $headers
* @return Response|Application|ResponseFactory
*/
public static function ok(array|string|null $message = '', array $headers = []): Response|Application|ResponseFactory
{
return static::response($message, self::HTTP_OK, $headers);
}
/**
* @param array|string|null $message
* @param array $headers
* @return Response|Application|ResponseFactory
*/
public static function created(array|string|null $message = '', array $headers = []): Response|Application|ResponseFactory
{
return static::response($message, self::HTTP_CREATED, $headers);
}
/**
* @param array|string|null $message
* @param array $headers
* @return Response|Application|ResponseFactory
*/
public static function accepted(array|string|null $message = '', array $headers = []): Response|Application|ResponseFactory
{
return static::response($message, self::HTTP_ACCEPTED, $headers);
}
/**
* @param array|string|null $message
* @param array $headers
* @return Response|Application|ResponseFactory
*/
public static function noContent(array|string|null $message = '', array $headers = []): Response|Application|ResponseFactory
{
return static::response($message, self::HTTP_NO_CONTENT, $headers);
}
/**
* @param array|string|null $message
* @param int $status
* @param array $headers
* @return Response|Application|ResponseFactory
*/
public static function response(array|string|null $message = '', int $status = 200, array $headers = []): Response|Application|ResponseFactory
{
return (new self)->doResponse($message, $status, $headers);
}
/**
* @param array|string|null $message
* @param int $status
* @param array $headers
* @return Response|Application|ResponseFactory
*/
protected function doResponse(array|string|null $message = '', int $status = 200, array $headers = []): Response|Application|ResponseFactory
{
$this->setStatusCode($status);
$message = !empty($message) ? $message : static::getStatusText($status);
$key = is_string($message) ? 'message' : 'data';
return response([
'success' => $this->isSuccessful(),
$key => $message,
'time' => now()->toJSON(),
], $status, $headers);
}
/**
* @param int $status
* @param string|null $message
* @param array $headers
* @return never
*/
public static function abort(int $status, ?string $message = null, array $headers = [])
{
$message = !empty($message) ? $message : static::getStatusText($status);
return app()->abort($status, $message, $headers);
}
/**
* @param array|string|null $message
* @param array $headers
* @return Response|Application|ResponseFactory
*/
public static function badRequest(array|string|null $message = '', array $headers = []): Response|Application|ResponseFactory
{
return static::abort(self::HTTP_BAD_REQUEST, $message, $headers);
}
/**
* @param array|string|null $message
* @param array $headers
* @return Response|Application|ResponseFactory
*/
public static function unauthorized(array|string|null $message = '', array $headers = []): Response|Application|ResponseFactory
{
return static::abort(self::HTTP_UNAUTHORIZED, $message, $headers);
}
/**
* @param array|string|null $message
* @param array $headers
* @return Response|Application|ResponseFactory
*/
public static function paymentRequired(array|string|null $message = '', array $headers = []): Response|Application|ResponseFactory
{
return static::abort(self::HTTP_PAYMENT_REQUIRED, $message, $headers);
}
/**
* @param array|string|null $message
* @param array $headers
* @return Response|Application|ResponseFactory
*/
public static function forbidden(array|string|null $message = '', array $headers = []): Response|Application|ResponseFactory
{
return static::abort(self::HTTP_FORBIDDEN, $message, $headers);
}
/**
* @param array|string|null $message
* @param array $headers
* @return Response|Application|ResponseFactory
*/
public static function notFound(array|string|null $message = '', array $headers = []): Response|Application|ResponseFactory
{
return static::abort(self::HTTP_NOT_FOUND, $message, $headers);
}
}
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
class ForceJsonResponse
{
/**
* Handle an incoming request.
*
* @param Request $request
* @param Closure(Request): (Response|RedirectResponse) $next
* @return mixed
*/
public function handle(Request $request, Closure $next): mixed
{
$request->headers->set('Accept', 'application/json');
return $next($request);
}
}
<?php
namespace App\Traits\Exceptions;
use Illuminate\Support\Arr;
use Throwable;
trait HandlerTrait
{
/**
* Convert the given exception to an array.
*
* @param Throwable $e
* @return array
*/
protected function convertExceptionToArray(Throwable $e): array
{
return config('app.debug') ? [
'success' => false,
'message' => $e->getMessage(),
'time' => now()->toJSON(),
'exception' => get_class($e),
'file' => $e->getFile(),
'line' => $e->getLine(),
'trace' => collect($e->getTrace())->map(fn($trace) => Arr::except($trace, ['args']))->all(),
] : [
'success' => false,
'message' => $this->isHttpException($e) ? $e->getMessage() : 'Server Error',
'time' => now()->toJSON(),
];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment