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; |
15
|
|
|
|
16
|
|
|
use Phauthentic\Pagination\Paginator\PaginatorInterface; |
17
|
|
|
use Phauthentic\Pagination\ParamsFactory\PaginationParamsFactoryInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Pagination Service |
21
|
|
|
* |
22
|
|
|
* Application layer pagination service that should in theory be able to paginate |
23
|
|
|
* any data / persistence layer implementation through the mappers. |
24
|
|
|
*/ |
25
|
|
|
class PaginationService implements PaginationServiceInterface |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* Pagination Params Factory |
29
|
|
|
* |
30
|
|
|
* @var \Phauthentic\Pagination\ParamsFactory\PaginationParamsFactoryInterface |
31
|
|
|
*/ |
32
|
|
|
protected $paginationParamsFactory; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Pagination to data layer implementation mapper |
36
|
|
|
* |
37
|
|
|
* @var \Phauthentic\Pagination\Paginator\PaginatorInterface; |
38
|
|
|
*/ |
39
|
|
|
protected $paginator; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Constructor |
43
|
|
|
* |
44
|
|
|
* @param \Phauthentic\Pagination\ParamsFactory\PaginationParamsFactoryInterface |
45
|
|
|
*/ |
46
|
1 |
|
public function __construct( |
47
|
|
|
PaginationParamsFactoryInterface $paginationParamsFactory, |
48
|
|
|
PaginatorInterface $paginationAdapter |
49
|
|
|
) { |
50
|
1 |
|
$this->paginationParamsFactory = $paginationParamsFactory; |
51
|
1 |
|
$this->paginator = $paginationAdapter; |
52
|
1 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Sets the object that maps the pagination data to the underlying implementation |
56
|
|
|
* |
57
|
|
|
* @param \Phauthentic\Pagination\Paginator\PaginatorInterface |
58
|
|
|
* @return $this |
59
|
|
|
*/ |
60
|
|
|
public function setPaginator(PaginatorInterface $paginator): self |
61
|
|
|
{ |
62
|
|
|
$this->paginator = $paginator; |
63
|
|
|
|
64
|
|
|
return $this; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Sets the pagination params factory |
69
|
|
|
* |
70
|
|
|
* @return $this |
71
|
|
|
*/ |
72
|
|
|
public function setPaginationParamsFactory(PaginationParamsFactoryInterface $factory): self |
73
|
|
|
{ |
74
|
|
|
$this->paginationParamsFactory = $factory; |
75
|
|
|
|
76
|
|
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Extracts the pagination params from the given data |
81
|
|
|
* |
82
|
|
|
* @return \Phauthentic\Pagination\PaginationParamsInterface |
83
|
|
|
*/ |
84
|
|
|
public function extractPaginationParams($repository): PaginationParamsInterface |
85
|
|
|
{ |
86
|
|
|
return $this->paginationParamsFactory->build($repository); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @inheritDoc |
91
|
|
|
*/ |
92
|
1 |
|
public function paginate($repository, ?PaginationParamsInterface $paginationParams = null) |
93
|
|
|
{ |
94
|
1 |
|
if ($paginationParams === null) { |
95
|
|
|
$paginationParams = $this->paginationParamsFactory->build($repository); |
96
|
|
|
} |
97
|
|
|
|
98
|
1 |
|
return $this->paginator->paginate($repository, $paginationParams); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|