|
1
|
|
|
<?php namespace Limoncello\Flute\Adapters; |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Copyright 2015-2017 [email protected] |
|
5
|
|
|
* |
|
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
7
|
|
|
* you may not use this file except in compliance with the License. |
|
8
|
|
|
* You may obtain a copy of the License at |
|
9
|
|
|
* |
|
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
11
|
|
|
* |
|
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15
|
|
|
* See the License for the specific language governing permissions and |
|
16
|
|
|
* limitations under the License. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
use Limoncello\Flute\Contracts\Adapters\PaginationStrategyInterface; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @package Limoncello\Flute |
|
23
|
|
|
*/ |
|
24
|
|
|
class PaginationStrategy implements PaginationStrategyInterface |
|
25
|
|
|
{ |
|
26
|
|
|
/**@deprecated Paging constant */ |
|
27
|
|
|
const DEFAULT_LIMIT_SIZE = 20; |
|
28
|
|
|
|
|
29
|
|
|
/**@deprecated Paging constant */ |
|
30
|
|
|
const MAX_LIMIT_SIZE = 100; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var int |
|
34
|
|
|
*/ |
|
35
|
|
|
private $defaultPageSize; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var int |
|
39
|
|
|
*/ |
|
40
|
|
|
private $maxPageSize; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param int $defaultPageSize |
|
44
|
|
|
* @param int $maxPageSize |
|
45
|
|
|
*/ |
|
46
|
54 |
|
public function __construct(int $defaultPageSize, int $maxPageSize) |
|
47
|
|
|
{ |
|
48
|
54 |
|
assert($defaultPageSize > 0 && $maxPageSize > 0); |
|
49
|
|
|
|
|
50
|
54 |
|
$this->defaultPageSize = $defaultPageSize; |
|
51
|
54 |
|
$this->maxPageSize = $maxPageSize; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @inheritdoc |
|
56
|
|
|
*/ |
|
57
|
6 |
|
public function getParameters(string $rootClass, string $class, string $path, string $relationshipName): array |
|
58
|
|
|
{ |
|
59
|
|
|
// input parameters are ignored (same paging params for all) |
|
60
|
|
|
// feel free to change it in child classes |
|
61
|
6 |
|
assert($rootClass || $class || $path || $relationshipName); |
|
62
|
|
|
|
|
63
|
6 |
|
$offset = 0; |
|
64
|
|
|
|
|
65
|
6 |
|
return [$offset, $this->getDefaultPageSize() + 1]; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @inheritdoc |
|
70
|
|
|
*/ |
|
71
|
20 |
|
public function parseParameters(?array $parameters): array |
|
72
|
|
|
{ |
|
73
|
20 |
|
if ($parameters === null) { |
|
74
|
15 |
|
return [0, $this->getDefaultPageSize() + 1]; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
5 |
|
$offset = $this->getValue( |
|
78
|
5 |
|
$parameters, |
|
79
|
5 |
|
static::PARAM_PAGING_SKIP, |
|
80
|
5 |
|
0, |
|
81
|
5 |
|
0, |
|
82
|
5 |
|
PHP_INT_MAX |
|
83
|
|
|
); |
|
84
|
5 |
|
$size = $this->getValue( |
|
85
|
5 |
|
$parameters, |
|
86
|
5 |
|
static::PARAM_PAGING_SIZE, |
|
87
|
5 |
|
$this->getDefaultPageSize(), |
|
88
|
5 |
|
1, |
|
89
|
5 |
|
max($this->getDefaultPageSize(), $this->getMaxPageSize()) |
|
90
|
|
|
); |
|
91
|
|
|
|
|
92
|
5 |
|
return [$offset, $size + 1]; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @param array $parameters |
|
97
|
|
|
* @param string $key |
|
98
|
|
|
* @param int $default |
|
99
|
|
|
* @param int $min |
|
100
|
|
|
* @param int $max |
|
101
|
|
|
* |
|
102
|
|
|
* @return int |
|
103
|
|
|
*/ |
|
104
|
5 |
|
private function getValue(array $parameters, string $key, int $default, int $min, int $max): int |
|
105
|
|
|
{ |
|
106
|
5 |
|
$result = $default; |
|
107
|
5 |
|
if (isset($parameters[$key]) === true) { |
|
108
|
5 |
|
$value = $parameters[$key]; |
|
109
|
5 |
|
if (is_numeric($value) === true) { |
|
110
|
5 |
|
$value = intval($value); |
|
111
|
5 |
|
if ($value < $min) { |
|
112
|
2 |
|
$result = $min; |
|
113
|
5 |
|
} elseif ($value > $max) { |
|
114
|
1 |
|
$result = $max; |
|
115
|
|
|
} else { |
|
116
|
5 |
|
$result = $value; |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
5 |
|
return $result; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @return int |
|
126
|
|
|
*/ |
|
127
|
23 |
|
public function getDefaultPageSize(): int |
|
128
|
|
|
{ |
|
129
|
23 |
|
return $this->defaultPageSize; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @return int |
|
134
|
|
|
*/ |
|
135
|
5 |
|
public function getMaxPageSize(): int |
|
136
|
|
|
{ |
|
137
|
5 |
|
return $this->maxPageSize; |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|