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
|
|
|
/** |
26
|
|
|
* Parameter map |
27
|
|
|
* |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
protected $map = [ |
31
|
|
|
'limit' => 'limit', |
32
|
|
|
'page' => 'page', |
33
|
|
|
'direction' => 'direction', |
34
|
|
|
'sortBy' => 'sort' |
35
|
|
|
]; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Gets the page number value from the server request params |
39
|
|
|
* |
40
|
|
|
* @param array $params Server request query params |
41
|
|
|
* @param string $name of the query param |
42
|
|
|
* @return int |
43
|
|
|
*/ |
44
|
|
|
public function getPage(array $params, string $name = 'page'): int |
45
|
|
|
{ |
46
|
|
|
if (isset($params[$name])) { |
47
|
|
|
return (int)$params[$name]; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return 1; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Gets the limit value from the server request params |
55
|
|
|
* |
56
|
|
|
* @param array $params Server request query params |
57
|
|
|
* @param string $name of the query param |
58
|
|
|
* @return int |
59
|
|
|
*/ |
60
|
|
|
public function getLimit(array $params, string $name = 'limit'): int |
61
|
|
|
{ |
62
|
|
|
if (isset($params[$name])) { |
63
|
|
|
return (int)$params['limit']; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return 20; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Gets the sort value from the server request params |
71
|
|
|
* |
72
|
|
|
* @param array $params Server request query params |
73
|
|
|
* @param string $name of the query param |
74
|
|
|
* @return string|null |
75
|
|
|
*/ |
76
|
2 |
|
public function getSortBy(array $params, string $name = 'sort'): ?string |
77
|
|
|
{ |
78
|
2 |
|
if (!empty($params[$name])) { |
79
|
2 |
|
return (string)$params[$name]; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return null; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Gets the direction value from the server request params |
87
|
|
|
* |
88
|
|
|
* @param array $params Server request query params |
89
|
|
|
* @param string $name of the query param |
90
|
|
|
* @return string Must be desc `desc`or `asc` |
91
|
|
|
*/ |
92
|
2 |
|
public function getDirection(array $params, string $name = 'direction'): string |
93
|
|
|
{ |
94
|
2 |
|
if (isset($params[$name])) { |
95
|
2 |
|
return (string)$params[$name]; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return 'desc'; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Sets the query param mapping |
103
|
|
|
* |
104
|
|
|
* @param array $map Query param map |
105
|
|
|
* @return \Phauthentic\Pagination\PaginationParamsFactoryInterface |
|
|
|
|
106
|
|
|
*/ |
107
|
|
|
public function setQueryParamMapping(array $map = []): PaginationParamsFactoryInterface |
108
|
|
|
{ |
109
|
|
|
$this->map = array_merge($this->map, $map); |
110
|
|
|
|
111
|
|
|
return $this; |
|
|
|
|
112
|
|
|
} |
113
|
|
|
|
114
|
3 |
|
protected function mapRequest(ServerRequestInterface $request) |
115
|
|
|
{ |
116
|
3 |
|
$queryParams = $request->getQueryParams(); |
117
|
|
|
|
118
|
3 |
|
$params = new static::$paginationParamsClass(); |
119
|
|
|
|
120
|
3 |
|
foreach ($this->map as $setter => $value) { |
121
|
3 |
|
$setterMethod = 'set' . $setter; |
122
|
3 |
|
$getterMethod = 'get' . $setter; |
123
|
|
|
|
124
|
3 |
|
if (!is_string($value) && is_callable($value)) { |
125
|
|
|
$value = $value($request); |
126
|
|
|
} |
127
|
|
|
|
128
|
3 |
|
if (isset($queryParams[$value])) { |
129
|
3 |
|
$params->{$setterMethod}($this->{$getterMethod}($queryParams, $value)); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
3 |
|
return $params; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @inheritDoc |
138
|
|
|
*/ |
139
|
3 |
|
public function build($data = null): PaginationParamsInterface |
140
|
|
|
{ |
141
|
3 |
|
if (!$data instanceof ServerRequestInterface) { |
142
|
|
|
return new static::$paginationParamsClass(); |
143
|
|
|
} |
144
|
|
|
|
145
|
3 |
|
return $this->mapRequest($data); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
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