1 | <?php |
||
29 | class PaginationFactory |
||
30 | { |
||
31 | const DESC = "desc"; |
||
32 | const SORT = "sort"; |
||
33 | const ORDER = "order"; |
||
34 | const PAGE = "page"; |
||
35 | const START_PAGE = "startPage"; |
||
36 | const START_INDEX = "startIndex"; |
||
37 | const START = "start"; |
||
38 | const COUNT = "count"; |
||
39 | const MAX = "max"; |
||
40 | const LIMIT = "limit"; |
||
41 | const OFFSET = "offset"; |
||
42 | const RANGE = "Range"; |
||
43 | const REGEX_RANGE = '/^items=(\\d+)-(\\d+)$/'; |
||
44 | const REGEX_DOJO_SORT = '/^sort\\(.*\\)$/'; |
||
45 | |||
46 | private static $maxAlias = [self::MAX => null, self::LIMIT => null, self::COUNT => null]; |
||
47 | private static $offsetAlias = [self::START => null, self::OFFSET => null]; |
||
48 | private static $pageAlias = [self::PAGE => null, self::START_PAGE => null]; |
||
49 | |||
50 | /** |
||
51 | * Checks the request query parameters and headers for pagination info. |
||
52 | * |
||
53 | * This class supports a good size sampling of different ways to provide |
||
54 | * pagination info. |
||
55 | * |
||
56 | * #### Range |
||
57 | * - `max` + `offset` (e.g. Grails): `&max=25&offset=0` |
||
58 | * - `count` + `start` (e.g. `dojox.data.QueryReadStore`): `&count=25&start=0` |
||
59 | * - `Range` header (e.g. `dojo.store.JsonRest`): `Range: items=0-24` |
||
60 | * - `count` + `startIndex` (e.g. OpenSearch): `&count=25&startIndex=1` |
||
61 | * - `count` + `startPage` (e.g. OpenSearch): `&count=25&startPage=1` |
||
62 | * - `limit` + `page` (e.g. Spring Data REST): `&limit=25&page=1` |
||
63 | * - `limit` + `start` (e.g. ExtJS): `&limit=25&start=0` |
||
64 | * |
||
65 | * Dojo, Grails, and ExtJS are all zero-based. OpenSearch and Spring Data |
||
66 | * are one-based. |
||
67 | * |
||
68 | * #### Order |
||
69 | * - OpenSearchServer: `&sort=foo&sort=-bar` |
||
70 | * - OpenSearch extension: `&sort=foo:ascending&sort=bar:descending` |
||
71 | * - Grails: `&sort=foo&order=asc` |
||
72 | * - Spring Data REST: `&sort=foo,asc&sort=bar,desc` |
||
73 | * - Dojo: `&sort(+foo,-bar)` |
||
74 | * - Dojo w/field: `&[field]=+foo,-bar` |
||
75 | * - ExtJS JSON: `&sort=[{"property":"foo","direction":"asc"},{"property":"bar","direction":"desc"}]` |
||
76 | * |
||
77 | * Because of the fact that many order syntaxes use multiple query string |
||
78 | * parameters with the same name, it is absolutely *vital* that you do not |
||
79 | * use a `ServerRequestInterface` that has been constructed with the `$_GET` |
||
80 | * superglobal. |
||
81 | * |
||
82 | * The problem here is that PHP will overwrite entries in the `$_GET` |
||
83 | * superglobal if they share the same name. With PHP, a request to |
||
84 | * `file.php?foobar=foo&foobar=bar` will result in `$_GET` set to |
||
85 | * `['foobar' => 'bar']`. |
||
86 | * |
||
87 | * Other platforms, like the Java Servlet specification, allow list-like |
||
88 | * access to these parameters. Make sure the object you pass for `$request` |
||
89 | * has a `queryParams` property that has been created to account for |
||
90 | * multiple query parameters with the same name. The `QueryParams` class |
||
91 | * will produce an array that accounts for this case. |
||
92 | * |
||
93 | * @param \Psr\Http\Message\ServerRequestInterface $request The server request. Please read important docs above. |
||
94 | * @param string $sortParameter The name of the sort parameter |
||
95 | * @param array<string,bool> $defaultSort The default sort if request lacks it |
||
96 | * @return \Caridea\Http\Pagination The pagination details |
||
97 | */ |
||
98 | 8 | public function create(\Psr\Http\Message\ServerRequestInterface $request, string $sortParameter = self::SORT, array $defaultSort = []): Pagination |
|
133 | |||
134 | /** |
||
135 | * Parses the order array. |
||
136 | * |
||
137 | * Because of the fact that many order syntaxes use multiple query string |
||
138 | * parameters with the same name, it is absolutely *vital* that you do not |
||
139 | * use a `ServerRequestInterface` that has been constructed with the `$_GET` |
||
140 | * superglobal. |
||
141 | * |
||
142 | * The problem here is that PHP will overwrite entries in the `$_GET` |
||
143 | * superglobal if they share the same name. With PHP, a request to |
||
144 | * `file.php?foobar=foo&foobar=bar` will result in `$_GET` set to |
||
145 | * `['foobar' => 'bar']`. |
||
146 | * |
||
147 | * Other platforms, like the Java Servlet specification, allow list-like |
||
148 | * access to these parameters. Make sure the object you pass for `$request` |
||
149 | * has a `queryParams` property that has been created to account for |
||
150 | * multiple query parameters with the same name. The `QueryParams` class |
||
151 | * will produce an array that accounts for this case. |
||
152 | * |
||
153 | * @param \Psr\Http\Message\ServerRequestInterface $request The request |
||
154 | * @param string $sortParameter The sort parameter |
||
155 | * @param array<string,bool> $default The default sort order |
||
156 | * @return array<string,bool> String keys to boolean values |
||
157 | */ |
||
158 | 8 | protected function getOrder(\Psr\Http\Message\ServerRequestInterface $request, string $sortParameter, array $default = []): array |
|
180 | |||
181 | /** |
||
182 | * Attempts to parse a single sort value. |
||
183 | * |
||
184 | * @param string $sort The sort value |
||
185 | * @param array<string,bool> $sorts String keys to boolean values |
||
186 | */ |
||
187 | 7 | protected function parseSort(string $sort, array &$sorts) |
|
231 | |||
232 | /** |
||
233 | * Gets the first numeric value from `$params`, otherwise `$defaultValue`. |
||
234 | * |
||
235 | * @param array $names |
||
236 | * @param array $params |
||
237 | * @param int $defaultValue |
||
238 | * @return int |
||
239 | */ |
||
240 | protected function parse(array &$names, array &$params, int $defaultValue) : int |
||
247 | } |
||
248 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.