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 Interface |
20
|
|
|
*/ |
21
|
|
|
class PaginationParamsFactory implements PaginationParamsFactoryInterface { |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
protected $map = [ |
27
|
|
|
'limit' => 'limit', |
28
|
|
|
'page' => 'page', |
29
|
|
|
'direction' => 'direction', |
30
|
|
|
'sortBy' => 'sort' |
31
|
|
|
]; |
32
|
|
|
|
33
|
|
|
public function getPage(array $params, string $name = 'page'): int |
34
|
|
|
{ |
35
|
|
|
if (isset($params[$name])) { |
36
|
|
|
return (int)$params[$name]; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
return 1; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function getLimit(array $params, string $name = 'limit'): int |
43
|
|
|
{ |
44
|
|
|
if (isset($params[$name])) { |
45
|
|
|
return (int)$params['limit']; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
return 20; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function getSortBy(array $params, string $name = 'sort'): string |
52
|
|
|
{ |
53
|
|
|
if (!empty($params[$name])) { |
54
|
|
|
return (string)$params[$name]; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return null; |
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function getDirection(array $params, string $name = 'direction'): string |
61
|
|
|
{ |
62
|
|
|
if (isset($params[$name])) { |
63
|
|
|
return (string)$params[$name]; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return 'desc'; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Sets the query param mapping |
71
|
|
|
* |
72
|
|
|
* @param array $map Query param map |
73
|
|
|
* @return \Phauthentic\Pagination\PaginationParamsFactoryInterface |
74
|
|
|
*/ |
75
|
|
|
public function setQueryParamMapping(array $map = []): PaginationParamsFactoryInterface |
76
|
|
|
{ |
77
|
|
|
$this->map = array_merge($this->map, $map); |
78
|
|
|
|
79
|
|
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
protected function mapRequest(ServerRequestInterface $request) |
83
|
|
|
{ |
84
|
|
|
$queryParams = $request->getQueryParams(); |
85
|
|
|
|
86
|
|
|
$params = new PaginationParams(); |
87
|
|
|
|
88
|
|
|
foreach ($this->map as $setter => $value) { |
89
|
|
|
$setterMethod = 'set' . $setter; |
90
|
|
|
$getterMethod = 'get' . $setter; |
91
|
|
|
|
92
|
|
|
if (!is_string($value) && is_callable($value)) { |
93
|
|
|
$value = $value($request); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
if (isset($queryParams[$value])) { |
97
|
|
|
$params->{$setterMethod}($this->{$getterMethod}($queryParams, $value)); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $params; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @inheritDoc |
106
|
|
|
*/ |
107
|
|
|
public function build(ServerRequestInterface $request): PaginationParamsInterface |
108
|
|
|
{ |
109
|
|
|
return $this->mapRequest($request); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|