master

laravel/framework

Last updated at: 29/12/2023 09:23

PaginatedResourceResponse.php

TLDR

The PaginatedResourceResponse.php file is a part of the Demo Projects project and is located at src/Illuminate/Http/Resources/Json/PaginatedResourceResponse.php. This file contains a class called PaginatedResourceResponse which extends the ResourceResponse class. It defines a method toResponse that generates and returns an HTTP response in JSON format. The class also contains several protected methods (paginationInformation, paginationLinks, and meta) that are used by the toResponse method to gather and include pagination information in the response.

Methods

toResponse

The toResponse method creates and returns an HTTP response that represents the object. It takes a $request object as a parameter and generates a JSON response using the ResourceResponse class. It wraps the resource data and merges it with pagination information, additional resource data, and any custom JSON options specified by the resource. The method also sets the original resource data on the response object and calls the withResponse method on the resource object.

paginationInformation

The paginationInformation method adds pagination information to the response. It takes a $request object as a parameter and returns an array containing the pagination links and meta data for the response. If the resource class has a paginationInformation method or a macro named paginationInformation, it calls that method to retrieve the pagination information. Otherwise, it returns a default array containing the links and meta data.

paginationLinks

The paginationLinks method retrieves and returns the pagination links for the response. It takes an array $paginated as a parameter and returns an array containing the first, last, previous, and next page URLs based on the values in the $paginated array.

meta

The meta method gathers and returns the meta data for the response. It takes an array $paginated as a parameter and removes certain keys from the array (data, first_page_url, last_page_url, prev_page_url, next_page_url). The remaining keys and values are included in the meta data for the response.

<?php

namespace Illuminate\Http\Resources\Json;

use Illuminate\Support\Arr;

class PaginatedResourceResponse extends ResourceResponse
{
    /**
     * Create an HTTP response that represents the object.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function toResponse($request)
    {
        return tap(response()->json(
            $this->wrap(
                $this->resource->resolve($request),
                array_merge_recursive(
                    $this->paginationInformation($request),
                    $this->resource->with($request),
                    $this->resource->additional
                )
            ),
            $this->calculateStatus(),
            [],
            $this->resource->jsonOptions()
        ), function ($response) use ($request) {
            $response->original = $this->resource->resource->map(function ($item) {
                return is_array($item) ? Arr::get($item, 'resource') : $item->resource;
            });

            $this->resource->withResponse($request, $response);
        });
    }

    /**
     * Add the pagination information to the response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    protected function paginationInformation($request)
    {
        $paginated = $this->resource->resource->toArray();

        $default = [
            'links' => $this->paginationLinks($paginated),
            'meta' => $this->meta($paginated),
        ];

        if (method_exists($this->resource, 'paginationInformation') ||
            $this->resource->hasMacro('paginationInformation')) {
            return $this->resource->paginationInformation($request, $paginated, $default);
        }

        return $default;
    }

    /**
     * Get the pagination links for the response.
     *
     * @param  array  $paginated
     * @return array
     */
    protected function paginationLinks($paginated)
    {
        return [
            'first' => $paginated['first_page_url'] ?? null,
            'last' => $paginated['last_page_url'] ?? null,
            'prev' => $paginated['prev_page_url'] ?? null,
            'next' => $paginated['next_page_url'] ?? null,
        ];
    }

    /**
     * Gather the meta data for the response.
     *
     * @param  array  $paginated
     * @return array
     */
    protected function meta($paginated)
    {
        return Arr::except($paginated, [
            'data',
            'first_page_url',
            'last_page_url',
            'prev_page_url',
            'next_page_url',
        ]);
    }
}