headers = $headers; return $this; } /** * Set the response meta data. * * @param array $meta * * @return $this */ protected function setMetaData(array $meta) { $this->meta = $meta; return $this; } /** * Set the response meta data. * * @param array $data * * @return $this */ protected function setData(array $data) { $this->data = $data; return $this; } /** * Set the response status code. * * @param int $statusCode * * @return $this */ protected function setStatusCode($satusCode) { $this->satusCode = $satusCode; return $this; } /** * Respond with a pagination response. * * @param \Illuminate\Pagination\Paginator $paginator * @param \Illuminate\Http\Request $request * * @return \Illuminate\Http\JsonResponse */ protected function paginator(Paginator $paginator, Request $request) { foreach ($request->query as $key => $value) { if ($key != 'page') { $paginator->addQuery($key, $value); } } $pagination = [ 'pagination' => [ 'total' => $paginator->total(), 'count' => count($paginator->items()), 'per_page' => $paginator->perPage(), 'current_page' => $paginator->currentPage(), 'total_pages' => $paginator->lastPage(), 'links' => [ 'next_page' => $paginator->nextPageUrl(), 'previous_page' => $paginator->previousPageUrl(), ], ], ]; return $this->setMetaData($pagination)->setData($paginator->items())->respond(); } /** * Respond with a no content response. * * @param string $message * * @return \Illuminate\Http\JsonResponse */ protected function noContent() { return $this->setStatusCode(204)->respond(); } /** * Build the response. * * @return \Illuminate\Http\Response */ protected function respond() { if (! empty($this->meta)) { $response['meta'] = $this->meta; } $response['data'] = $this->data; if ($this->data instanceof Arrayable) { $response['data'] = $this->data->toArray(); } return Response::json($response, $this->statusCode, $this->headers); } }