|
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\ParamsFactory; |
|
15
|
|
|
|
|
16
|
|
|
use Phauthentic\Pagination\PaginationParamsInterface; |
|
17
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Extracts the pagination information from a server requests query string |
|
21
|
|
|
*/ |
|
22
|
|
|
class ServerRequestQueryParamsFactory extends AbstractFactory { |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Parameter map |
|
26
|
|
|
* |
|
27
|
|
|
* @var array |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $map = [ |
|
30
|
|
|
'limit' => 'limit', |
|
31
|
|
|
'page' => 'page', |
|
32
|
|
|
'direction' => 'direction', |
|
33
|
|
|
'sortBy' => 'sort' |
|
34
|
|
|
]; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Gets the page number value from the server request params |
|
38
|
|
|
* |
|
39
|
|
|
* @param array $params Server request query params |
|
40
|
|
|
* @param string $name of the query param |
|
41
|
|
|
* @return int |
|
42
|
|
|
*/ |
|
43
|
|
|
public function getPage(array $params, string $name = 'page'): int |
|
44
|
|
|
{ |
|
45
|
|
|
if (isset($params[$name])) { |
|
46
|
|
|
return (int)$params[$name]; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
return 1; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Gets the limit value from the server request params |
|
54
|
|
|
* |
|
55
|
|
|
* @param array $params Server request query params |
|
56
|
|
|
* @param string $name of the query param |
|
57
|
|
|
* @return int |
|
58
|
|
|
*/ |
|
59
|
|
|
public function getLimit(array $params, string $name = 'limit'): int |
|
60
|
|
|
{ |
|
61
|
|
|
if (isset($params[$name])) { |
|
62
|
|
|
return (int)$params['limit']; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return 20; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Gets the sort value from the server request params |
|
70
|
|
|
* |
|
71
|
|
|
* @param array $params Server request query params |
|
72
|
|
|
* @param string $name of the query param |
|
73
|
|
|
* @return string|null |
|
74
|
|
|
*/ |
|
75
|
1 |
|
public function getSortBy(array $params, string $name = 'sort'): ?string |
|
76
|
|
|
{ |
|
77
|
1 |
|
if (!empty($params[$name])) { |
|
78
|
1 |
|
return (string)$params[$name]; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return null; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Gets the direction value from the server request params |
|
86
|
|
|
* |
|
87
|
|
|
* @param array $params Server request query params |
|
88
|
|
|
* @param string $name of the query param |
|
89
|
|
|
* @return string Must be desc `desc`or `asc` |
|
90
|
|
|
*/ |
|
91
|
1 |
|
public function getDirection(array $params, string $name = 'direction'): string |
|
92
|
|
|
{ |
|
93
|
1 |
|
if (isset($params[$name])) { |
|
94
|
1 |
|
return (string)$params[$name]; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
return 'desc'; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Sets the query param mapping |
|
102
|
|
|
* |
|
103
|
|
|
* @param array $map Query param map |
|
104
|
|
|
* @return \Phauthentic\Pagination\PaginationParamsFactoryInterface |
|
|
|
|
|
|
105
|
|
|
*/ |
|
106
|
|
|
public function setQueryParamMapping(array $map = []): PaginationParamsFactoryInterface |
|
107
|
|
|
{ |
|
108
|
|
|
$this->map = array_merge($this->map, $map); |
|
109
|
|
|
|
|
110
|
|
|
return $this; |
|
|
|
|
|
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
2 |
|
protected function mapRequest(ServerRequestInterface $request) |
|
114
|
|
|
{ |
|
115
|
2 |
|
$queryParams = $request->getQueryParams(); |
|
116
|
|
|
|
|
117
|
2 |
|
$params = new static::$paginationParamsClass(); |
|
118
|
|
|
|
|
119
|
2 |
|
foreach ($this->map as $setter => $value) { |
|
120
|
2 |
|
$setterMethod = 'set' . $setter; |
|
121
|
2 |
|
$getterMethod = 'get' . $setter; |
|
122
|
|
|
|
|
123
|
2 |
|
if (!is_string($value) && is_callable($value)) { |
|
124
|
|
|
$value = $value($request); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
2 |
|
if (isset($queryParams[$value])) { |
|
128
|
2 |
|
$params->{$setterMethod}($this->{$getterMethod}($queryParams, $value)); |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
2 |
|
return $params; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @inheritDoc |
|
137
|
|
|
*/ |
|
138
|
2 |
|
public function build($data): PaginationParamsInterface |
|
139
|
|
|
{ |
|
140
|
2 |
|
if (!$data instanceof ServerRequestInterface) { |
|
141
|
|
|
return new static::$paginationParamsClass(); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
2 |
|
return $this->mapRequest($data); |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths