Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
PaginatorTrait
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
1 / 1
9
100.00% covered (success)
100.00%
1 / 1
 getPaginatorOptions
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
1 / 1
9
1<?php
2
3declare(strict_types=1);
4
5namespace App\Helper\Paginator;
6
7use Symfony\Component\DependencyInjection\Attribute\Autowire;
8use Symfony\Component\HttpFoundation\Request;
9
10trait PaginatorTrait
11{
12    /**
13     * Get pagination options from request.
14     */
15    protected function getPaginatorOptions(
16        Request $request,
17        #[Autowire('%env(LIST_LIMIT)%')]
18        int $listLimit
19    ): PaginatorOptions
20    {
21        /** @var array{page?: string, limit?: string, order?: string, orderDir?: string, criteria?: array<string, string>} $options */
22        $options = $request->query->all('paginatorOptions');
23
24        return new PaginatorOptions()
25            ->setPage(
26                isset($options['page']) && $options['page']
27                    ? (int)$options['page'] : 1
28            )
29            ->setLimit(
30                isset($options['limit']) && $options['limit']
31                    ? (int)$options['limit'] : $listLimit
32            )
33            ->setOrder(
34                isset($options['order']) && $options['order']
35                    ? $options['order'] : 'id'
36            )
37            ->setOrderDir(
38                isset($options['orderDir']) && $options['orderDir']
39                    ? $options['orderDir'] : 'ASC'
40            )
41            ->setCriteria($options['criteria'] ?? []);
42    }
43}