vendor/api-platform/core/src/Core/DataProvider/Pagination.php line 169

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the API Platform project.
  4.  *
  5.  * (c) Kévin Dunglas <dunglas@gmail.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace ApiPlatform\Core\DataProvider;
  12. use ApiPlatform\Core\Exception\InvalidArgumentException;
  13. use ApiPlatform\Core\Exception\ResourceClassNotFoundException;
  14. use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
  15. /**
  16.  * Pagination configuration.
  17.  *
  18.  * @author Baptiste Meyer <baptiste.meyer@gmail.com>
  19.  */
  20. final class Pagination
  21. {
  22.     private $options;
  23.     private $graphQlOptions;
  24.     private $resourceMetadataFactory;
  25.     public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFactory, array $options = [], array $graphQlOptions = [])
  26.     {
  27.         $this->resourceMetadataFactory $resourceMetadataFactory;
  28.         $this->options array_merge([
  29.             'enabled' => true,
  30.             'client_enabled' => false,
  31.             'client_items_per_page' => false,
  32.             'items_per_page' => 30,
  33.             'page_default' => 1,
  34.             'page_parameter_name' => 'page',
  35.             'enabled_parameter_name' => 'pagination',
  36.             'items_per_page_parameter_name' => 'itemsPerPage',
  37.             'maximum_items_per_page' => null,
  38.             'partial' => false,
  39.             'client_partial' => false,
  40.             'partial_parameter_name' => 'partial',
  41.         ], $options);
  42.         $this->graphQlOptions array_merge([
  43.             'enabled' => true,
  44.         ], $graphQlOptions);
  45.     }
  46.     /**
  47.      * Gets the current page.
  48.      *
  49.      * @throws InvalidArgumentException
  50.      */
  51.     public function getPage(array $context = []): int
  52.     {
  53.         $page = (int) $this->getParameterFromContext(
  54.             $context,
  55.             $this->options['page_parameter_name'],
  56.             $this->options['page_default']
  57.         );
  58.         if ($page) {
  59.             throw new InvalidArgumentException('Page should not be less than 1');
  60.         }
  61.         return $page;
  62.     }
  63.     /**
  64.      * Gets the current offset.
  65.      */
  66.     public function getOffset(string $resourceClass nullstring $operationName null, array $context = []): int
  67.     {
  68.         $graphql = (bool) ($context['graphql_operation_name'] ?? false);
  69.         $limit $this->getLimit($resourceClass$operationName$context);
  70.         if ($graphql && null !== ($after $this->getParameterFromContext($context'after'))) {
  71.             return false === ($after base64_decode($aftertrue)) ? : (int) $after 1;
  72.         }
  73.         if ($graphql && null !== ($before $this->getParameterFromContext($context'before'))) {
  74.             return ($offset = (false === ($before base64_decode($beforetrue)) ? : (int) $before $limit)) < $offset;
  75.         }
  76.         if ($graphql && null !== ($last $this->getParameterFromContext($context'last'))) {
  77.             return ($offset = ($context['count'] ?? 0) - $last) < $offset;
  78.         }
  79.         $offset = ($this->getPage($context) - 1) * $limit;
  80.         if (!\is_int($offset)) {
  81.             throw new InvalidArgumentException('Page parameter is too large.');
  82.         }
  83.         return $offset;
  84.     }
  85.     /**
  86.      * Gets the current limit.
  87.      *
  88.      * @throws InvalidArgumentException
  89.      */
  90.     public function getLimit(string $resourceClass nullstring $operationName null, array $context = []): int
  91.     {
  92.         $graphql = (bool) ($context['graphql_operation_name'] ?? false);
  93.         $limit $this->options['items_per_page'];
  94.         $clientLimit $this->options['client_items_per_page'];
  95.         if (null !== $resourceClass) {
  96.             $resourceMetadata $this->resourceMetadataFactory->create($resourceClass);
  97.             $limit $resourceMetadata->getCollectionOperationAttribute($operationName'pagination_items_per_page'$limittrue);
  98.             $clientLimit $resourceMetadata->getCollectionOperationAttribute($operationName'pagination_client_items_per_page'$clientLimittrue);
  99.         }
  100.         if ($graphql && null !== ($first $this->getParameterFromContext($context'first'))) {
  101.             $limit $first;
  102.         }
  103.         if ($graphql && null !== ($last $this->getParameterFromContext($context'last'))) {
  104.             $limit $last;
  105.         }
  106.         if ($graphql && null !== ($before $this->getParameterFromContext($context'before'))
  107.             && (false === ($before base64_decode($beforetrue)) ? : (int) $before $limit) < 0) {
  108.             $limit = (int) $before;
  109.         }
  110.         if ($clientLimit) {
  111.             $limit = (int) $this->getParameterFromContext($context$this->options['items_per_page_parameter_name'], $limit);
  112.             $maxItemsPerPage null;
  113.             if (null !== $resourceClass) {
  114.                 $resourceMetadata $this->resourceMetadataFactory->create($resourceClass);
  115.                 $maxItemsPerPage $resourceMetadata->getCollectionOperationAttribute($operationName'maximum_items_per_page'nulltrue);
  116.                 if (null !== $maxItemsPerPage) {
  117.                     @trigger_error('The "maximum_items_per_page" option has been deprecated since API Platform 2.5 in favor of "pagination_maximum_items_per_page" and will be removed in API Platform 3.', \E_USER_DEPRECATED);
  118.                 }
  119.                 $maxItemsPerPage $resourceMetadata->getCollectionOperationAttribute($operationName'pagination_maximum_items_per_page'$maxItemsPerPage ?? $this->options['maximum_items_per_page'], true);
  120.             }
  121.             if (null !== $maxItemsPerPage && $limit $maxItemsPerPage) {
  122.                 $limit $maxItemsPerPage;
  123.             }
  124.         }
  125.         if ($limit) {
  126.             throw new InvalidArgumentException('Limit should not be less than 0');
  127.         }
  128.         return $limit;
  129.     }
  130.     /**
  131.      * Gets info about the pagination.
  132.      *
  133.      * Returns an array with the following info as values:
  134.      *   - the page {@see Pagination::getPage()}
  135.      *   - the offset {@see Pagination::getOffset()}
  136.      *   - the limit {@see Pagination::getLimit()}
  137.      *
  138.      * @throws InvalidArgumentException
  139.      */
  140.     public function getPagination(string $resourceClass nullstring $operationName null, array $context = []): array
  141.     {
  142.         $page $this->getPage($context);
  143.         $limit $this->getLimit($resourceClass$operationName$context);
  144.         if (=== $limit && $page) {
  145.             throw new InvalidArgumentException('Page should not be greater than 1 if limit is equal to 0');
  146.         }
  147.         return [$page$this->getOffset($resourceClass$operationName$context), $limit];
  148.     }
  149.     /**
  150.      * Is the pagination enabled?
  151.      */
  152.     public function isEnabled(string $resourceClass nullstring $operationName null, array $context = []): bool
  153.     {
  154.         return $this->getEnabled($context$resourceClass$operationName);
  155.     }
  156.     /**
  157.      * Is the pagination enabled for GraphQL?
  158.      */
  159.     public function isGraphQlEnabled(string $resourceClass nullstring $operationName null, array $context = []): bool
  160.     {
  161.         return $this->getGraphQlEnabled($resourceClass$operationName);
  162.     }
  163.     /**
  164.      * Is the partial pagination enabled?
  165.      */
  166.     public function isPartialEnabled(string $resourceClass nullstring $operationName null, array $context = []): bool
  167.     {
  168.         return $this->getEnabled($context$resourceClass$operationNametrue);
  169.     }
  170.     public function getOptions(): array
  171.     {
  172.         return $this->options;
  173.     }
  174.     public function getGraphQlPaginationType(string $resourceClassstring $operationName): string
  175.     {
  176.         try {
  177.             $resourceMetadata $this->resourceMetadataFactory->create($resourceClass);
  178.         } catch (ResourceClassNotFoundException $e) {
  179.             return 'cursor';
  180.         }
  181.         return (string) $resourceMetadata->getGraphqlAttribute($operationName'pagination_type''cursor'true);
  182.     }
  183.     /**
  184.      * Is the classic or partial pagination enabled?
  185.      */
  186.     private function getEnabled(array $contextstring $resourceClass nullstring $operationName nullbool $partial false): bool
  187.     {
  188.         $enabled $this->options[$partial 'partial' 'enabled'];
  189.         $clientEnabled $this->options[$partial 'client_partial' 'client_enabled'];
  190.         if (null !== $resourceClass) {
  191.             $resourceMetadata $this->resourceMetadataFactory->create($resourceClass);
  192.             $enabled $resourceMetadata->getCollectionOperationAttribute($operationName$partial 'pagination_partial' 'pagination_enabled'$enabledtrue);
  193.             $clientEnabled $resourceMetadata->getCollectionOperationAttribute($operationName$partial 'pagination_client_partial' 'pagination_client_enabled'$clientEnabledtrue);
  194.         }
  195.         if ($clientEnabled) {
  196.             return filter_var($this->getParameterFromContext($context$this->options[$partial 'partial_parameter_name' 'enabled_parameter_name'], $enabled), \FILTER_VALIDATE_BOOLEAN);
  197.         }
  198.         return (bool) $enabled;
  199.     }
  200.     private function getGraphQlEnabled(?string $resourceClass, ?string $operationName): bool
  201.     {
  202.         $enabled $this->graphQlOptions['enabled'];
  203.         if (null !== $resourceClass) {
  204.             try {
  205.                 $resourceMetadata $this->resourceMetadataFactory->create($resourceClass);
  206.             } catch (ResourceClassNotFoundException $e) {
  207.                 return $enabled;
  208.             }
  209.             return (bool) $resourceMetadata->getGraphqlAttribute($operationName'pagination_enabled'$enabledtrue);
  210.         }
  211.         return $enabled;
  212.     }
  213.     /**
  214.      * Gets the given pagination parameter name from the given context.
  215.      *
  216.      * @param mixed|null $default
  217.      */
  218.     private function getParameterFromContext(array $contextstring $parameterName$default null)
  219.     {
  220.         $filters $context['filters'] ?? [];
  221.         return \array_key_exists($parameterName$filters) ? $filters[$parameterName] : $default;
  222.     }
  223. }