1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
/** |
4
|
|
|
* Copyright (c) Phauthentic (https://github.com/Phauthentic) |
5
|
|
|
* |
6
|
|
|
* Licensed under The MIT License |
7
|
|
|
* For full copyright and license information, please see the LICENSE.txt |
8
|
|
|
* Redistributions of files must retain the above copyright notice. |
9
|
|
|
* |
10
|
|
|
* @copyright Copyright (c) Phauthentic (https://github.com/Phauthentic) |
11
|
|
|
* @link https://github.com/Phauthentic |
12
|
|
|
* @license https://opensource.org/licenses/mit-license.php MIT License |
13
|
|
|
*/ |
14
|
|
|
namespace Phauthentic\Pagination\Paginator; |
15
|
|
|
|
16
|
|
|
use Doctrine\ORM\QueryBuilder; |
17
|
|
|
use Doctrine\ORM\Tools\Pagination\Paginator as DoctrinePaginator; |
18
|
|
|
use Doctrine\ORM\Tools\Pagination\Paginator; |
19
|
|
|
use Phauthentic\Pagination\PaginationParamsInterface; |
20
|
|
|
use InvalidArgumentException; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* PaginationToDoctrineRepositoryMapper |
24
|
|
|
* |
25
|
|
|
* @link https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/tutorials/pagination.html |
26
|
|
|
*/ |
27
|
|
|
class Doctrine2Paginator implements PaginatorInterface |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* The Doctrine Paginator Class |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
public static $paginatorClass = Paginator::class; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Maps the params to the repository |
38
|
|
|
* |
39
|
|
|
* @param mixed $repository |
40
|
|
|
* @param \Phauthentic\Pagination\PaginationParamsInterface $paginationParams Pagination params |
41
|
|
|
* @return \Doctrine\ORM\Tools\Pagination\Paginator |
42
|
|
|
*/ |
43
|
1 |
|
public function paginate($repository, PaginationParamsInterface $paginationParams) |
44
|
|
|
{ |
45
|
1 |
|
if (!$repository instanceof QueryBuilder) { |
46
|
|
|
throw new InvalidArgumentException(sprintf( |
47
|
|
|
'The $repository argument must be an instance of %s for this adapter', |
48
|
|
|
QueryBuilder::class |
49
|
|
|
)); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** @var $repository \Doctrine\ORM\QueryBuilder */ |
53
|
1 |
|
$sortBy = $paginationParams->getSortBy(); |
54
|
1 |
|
if (!empty($sortBy)) { |
55
|
1 |
|
$repository->addOrderBy( |
56
|
1 |
|
$paginationParams->getSortBy(), |
57
|
1 |
|
$paginationParams->getDirection() |
58
|
|
|
); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$repository |
62
|
1 |
|
->setFirstResult($paginationParams->getOffset()) |
63
|
1 |
|
->setMaxResults($paginationParams->getLimit()); |
64
|
|
|
|
65
|
1 |
|
$paginator = new self::$paginatorClass($repository, true); |
66
|
1 |
|
$paginationParams->setCount($paginator->count()); |
67
|
|
|
|
68
|
1 |
|
return $paginator; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|