$headers */ protected function json(mixed $data, int $status = 200, array $headers = []): Response { return new Response($data, $status, $headers); } /** * A paginated collection in the shape shared with `Camelia\Orm\Pagination\Page`. * * @param list> $items */ protected function page(array $items, int $page, int $perPage, int $total): Response { $safePage = max(1, $page); $safePerPage = max(1, min(1000, $perPage)); $safeTotal = max(0, $total); $totalPages = (int) ceil($safeTotal / $safePerPage); return $this->json([ 'items' => $items, 'page' => $safePage, 'per_page' => $safePerPage, 'total' => $safeTotal, 'total_pages' => $totalPages, 'has_next' => $safePage < $totalPages, 'has_prev' => $safePage > 1, ]); } /** 201 with a `Location` header pointing at the new resource. */ protected function created(mixed $data, string $location): Response { return $this->json($data, 201, ['Location' => $location]); } /** 204 with no body. */ protected function noContent(): Response { return Response::noContent(); } /** * Turns any error into a response. This method never throws, so an adapter can always rely on * getting a `Response` back. */ protected function error(HttpError $error): Response { return $this->json($error->toArray(), $error->status(), $error->headers()); } /** Convenience wrapper that keeps the error code consistent with the rest of the library. */ protected function notFound(string $message): Response { return $this->error(new NotFound($message)); } }