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 Psr\Http\Message\ServerRequestInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Pagination Service |
20
|
|
|
* |
21
|
|
|
* Application layer pagination service that should in theory be able to paginate |
22
|
|
|
* any data / persistence layer implementation through the mappers. |
23
|
|
|
*/ |
24
|
|
|
class PaginationService |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* Pagination Params Factory |
28
|
|
|
* |
29
|
|
|
* @var \Phauthentic\Pagination\PaginationParamsFactoryInterface |
30
|
|
|
*/ |
31
|
|
|
protected $paginationParamsFactory; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Pagination to data layer implementation mapper |
35
|
|
|
* |
36
|
|
|
* @var \Phauthentic\Pagination\PaginationAdapterInterface |
37
|
|
|
*/ |
38
|
|
|
protected $adapter; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Constructor |
42
|
|
|
*/ |
43
|
|
|
public function __construct( |
44
|
|
|
PaginationParamsFactory $paginationParamsFactory, |
45
|
|
|
PaginationAdapterInterface $paginationAdapter |
46
|
|
|
) { |
47
|
|
|
$this->paginationParamsFactory = $paginationParamsFactory; |
48
|
|
|
$this->adapter = $paginationAdapter; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Sets the object that maps the pagination data to the underlying implementation |
53
|
|
|
* |
54
|
|
|
* @return $this |
55
|
|
|
*/ |
56
|
|
|
public function setPaginationAdapter(PaginationAdapterInterface $adapter): self |
57
|
|
|
{ |
58
|
|
|
$this->adapter = $adapter; |
59
|
|
|
|
60
|
|
|
return $this; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Sets the pagination params factory |
65
|
|
|
* |
66
|
|
|
* @return $this |
67
|
|
|
*/ |
68
|
|
|
public function setPaginationParamsFactory(PaginationParamsFactory $factory): self |
69
|
|
|
{ |
70
|
|
|
$this->paginationParamsFactory = $factory; |
71
|
|
|
|
72
|
|
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Gets the pagination params from the request |
77
|
|
|
* |
78
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $serverRequest Server Request |
79
|
|
|
* @return \Phauthentic\Pagination\PaginationParamsInterface |
80
|
|
|
*/ |
81
|
|
|
public function getPagingParamsFromRequest( |
82
|
|
|
ServerRequestInterface $serverRequest |
83
|
|
|
): PaginationParamsInterface { |
84
|
|
|
return $this->paginationParamsFactory->build($serverRequest); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Sets the pagination data to a request attribute |
89
|
|
|
* |
90
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $serverRequest Server Request |
91
|
|
|
* @param \Phauthentic\Pagination\PaginationParamsInterface $paginationParams Pagination Params |
92
|
|
|
* @return \Psr\Http\Message\ServerRequestInterface |
93
|
|
|
*/ |
94
|
|
|
public function setPagingRequestAttribute( |
95
|
|
|
ServerRequestInterface $serverRequest, |
96
|
|
|
PaginationParamsInterface $paginationParams, |
97
|
|
|
string $attributeName = 'paging' |
98
|
|
|
): ServerRequestInterface { |
99
|
|
|
return $serverRequest->setAttribute($attributeName, $paginationParams); |
|
|
|
|
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Triggers the pagination on an object |
104
|
|
|
* |
105
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface |
106
|
|
|
* @param mixed $object The object to paginate on |
107
|
|
|
* @param callable $callable Optional callable to do whatever you want instead of using a mapper |
108
|
|
|
* @return mixed |
109
|
|
|
*/ |
110
|
|
|
public function paginateFromRequest(ServerRequestInterface $request, $repository, ?callable $callable = null) |
111
|
|
|
{ |
112
|
|
|
$params = $this->getPagingParamsFromRequest($request); |
113
|
|
|
|
114
|
|
|
return $this->paginate($params, $repository, $callable); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Triggers the pagination on an object |
119
|
|
|
* |
120
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface |
121
|
|
|
* @param mixed $object The object to paginate on |
122
|
|
|
* @param callable $callable Optional callable to do whatever you want instead of using a mapper |
123
|
|
|
* @return mixed |
124
|
|
|
*/ |
125
|
|
|
public function paginate(PaginationParamsInterface $paginationParams, $repository, ?callable $callable = null) |
126
|
|
|
{ |
127
|
|
|
if ($callable) { |
128
|
|
|
return $callable($repository, $paginationParams); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return $this->adapter->paginate($paginationParams, $repository); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.