Skip to content

Instantly share code, notes, and snippets.

@Semdevmaster
Created October 7, 2024 09:23
Show Gist options
  • Save Semdevmaster/c4e0ba524fea913468d377de7ff708a9 to your computer and use it in GitHub Desktop.
Save Semdevmaster/c4e0ba524fea913468d377de7ff708a9 to your computer and use it in GitHub Desktop.
Universal response builder for Laravel
<?php
declare(strict_types=1);
namespace App\Http\Responses;
use Illuminate\Pagination\LengthAwarePaginator;
use Symfony\Component\HttpFoundation\Response as HttpResponse;
class ResponseBuilder
{
protected function __construct(
protected bool $success = false,
protected int $http_code = HttpResponse::HTTP_BAD_REQUEST,
protected ?string $message = null,
protected ?int $json_options = null,
protected array $http_headers = [],
protected mixed $data = null
) {
}
public static function success(
mixed $data = null,
int $http_code = HttpResponse::HTTP_OK,
string $message = 'ok',
int $json_options = null,
array $http_headers = [],
): HttpResponse {
return static::asSuccess()
->withData($data)
->withMessage($message)
->withHttpCode($http_code)
->withJsonOptions($json_options)
->withHttpHeaders($http_headers)
->make();
}
public static function error(
mixed $data = null,
int $http_code = HttpResponse::HTTP_BAD_REQUEST,
string $message = 'error',
int $json_options = null,
array $http_headers = [],
): HttpResponse {
return static::asError()
->withData($data)
->withMessage($message)
->withHttpCode($http_code)
->withJsonOptions($json_options)
->withHttpHeaders($http_headers)
->make();
}
protected static function asSuccess(): self
{
return new static(true);
}
protected static function asError(): self
{
return new static();
}
protected function withHttpCode(int $http_code): self
{
$this->http_code = $http_code;
return $this;
}
protected function withData(mixed $data): self
{
if ($data instanceof LengthAwarePaginator) {
$data = [
'items' => $data->getCollection(),
'pagination' => $this->getPaginationData($data),
];
}
$this->data = $data;
return $this;
}
protected function withJsonOptions(?int $json_options): self
{
$this->json_options = $json_options ?? JSON_UNESCAPED_SLASHES ^ JSON_UNESCAPED_UNICODE ^ JSON_PRETTY_PRINT;
return $this;
}
protected function withMessage(string $message): self
{
$this->message = $message;
return $this;
}
protected function withHttpHeaders(array $http_headers): self
{
$this->http_headers = $http_headers;
return $this;
}
protected function make(): HttpResponse
{
return response()->json(
[
'success' => $this->success,
'status' => $this->http_code,
'message' => $this->message,
'data' => $this->data,
],
$this->http_code,
$this->http_headers,
$this->json_options
);
}
protected function getPaginationData($paginator): array
{
return [
'current_page' => $paginator->currentPage(),
'last_page' => $paginator->lastPage(),
'per_page' => $paginator->perPage(),
'total' => $paginator->total(),
'from' => $paginator->firstItem(),
'to' => $paginator->lastItem(),
];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment