1 | <?php |
||
12 | abstract class AbstractProcessor |
||
13 | { |
||
14 | /** |
||
15 | * @var null|callable |
||
16 | */ |
||
17 | protected static $defaultFormatter; |
||
18 | |||
19 | /** |
||
20 | * @var null|callable |
||
21 | */ |
||
22 | protected $formatter; |
||
23 | |||
24 | |||
25 | /** |
||
26 | * Override static default formatter. |
||
27 | * |
||
28 | * @param callable|Formatter|string $formatter |
||
29 | */ |
||
30 | 1 | public static function setDefaultFormatter($formatter) |
|
34 | |||
35 | /** |
||
36 | * Restore static default formatter. |
||
37 | */ |
||
38 | 1 | public static function restoreDefaultFormatter() |
|
42 | |||
43 | /** |
||
44 | * Use custom formatter. |
||
45 | * |
||
46 | * @param callable|Formatter|string $formatter |
||
47 | * @return $this |
||
48 | */ |
||
49 | 2 | public function useFormatter($formatter) |
|
54 | |||
55 | /** |
||
56 | * Restore default formatter. |
||
57 | * |
||
58 | * @return $this |
||
59 | */ |
||
60 | 1 | public function restoreFormatter() |
|
65 | |||
66 | /** |
||
67 | * Get result. |
||
68 | * |
||
69 | * @param Query $query |
||
70 | * @param mixed $rows |
||
71 | * @return mixed |
||
72 | */ |
||
73 | 2 | public function process(Query $query, $rows) |
|
110 | |||
111 | /** |
||
112 | * Invoke formatter. |
||
113 | * |
||
114 | * @param mixed $rows |
||
115 | * @param array $meta |
||
116 | * @param Query $query |
||
117 | * @return mixed |
||
118 | */ |
||
119 | 2 | protected function invokeFormatter($rows, array $meta, Query $query) |
|
124 | |||
125 | /** |
||
126 | * Validate formatter and return in normalized form. |
||
127 | * |
||
128 | * @param mixed $formatter |
||
129 | * @return callable |
||
130 | */ |
||
131 | 3 | protected static function validateFormatter($formatter) |
|
141 | |||
142 | /** |
||
143 | * Format result with default format. |
||
144 | * |
||
145 | * @param mixed $rows |
||
146 | * @param array $meta |
||
147 | * @param Query $query |
||
148 | * @return PaginationResult |
||
149 | */ |
||
150 | protected function defaultFormat($rows, array $meta, Query $query) |
||
154 | |||
155 | /** |
||
156 | * Determine if the rows should be replaced in reverse order. |
||
157 | * |
||
158 | * @param Query $query |
||
159 | * @return bool |
||
160 | */ |
||
161 | 2 | protected function shouldReverse(Query $query) |
|
165 | |||
166 | /** |
||
167 | * Determine if the first row should be dropped. |
||
168 | * |
||
169 | * @param Query $query |
||
170 | * @param mixed $rows |
||
171 | * @return bool |
||
172 | */ |
||
173 | 2 | protected function shouldLtrim(Query $query, $rows) |
|
226 | |||
227 | /** |
||
228 | * Determine if the last row should be dropped. |
||
229 | * |
||
230 | * @param Query $query |
||
231 | * @param $rows |
||
232 | * @return bool |
||
233 | */ |
||
234 | 2 | protected function shouldRtrim(Query $query, $rows) |
|
238 | |||
239 | /** |
||
240 | * Make a cursor from the specific row. |
||
241 | * |
||
242 | * @param Query $query |
||
243 | * @param mixed $row |
||
244 | * @return int[]|string[] |
||
245 | */ |
||
246 | protected function makeCursor(Query $query, $row) |
||
254 | |||
255 | /** |
||
256 | * Return comparable value from a row. |
||
257 | * |
||
258 | * @param mixed $row |
||
259 | * @param string $column |
||
260 | * @return int|string |
||
261 | */ |
||
262 | abstract protected function field($row, $column); |
||
263 | |||
264 | /** |
||
265 | * Compare the values. |
||
266 | * |
||
267 | * "$field < $cursor" should return -1. |
||
268 | * "$field > $cursor" should return 1. |
||
269 | * "$field == $cursor" should return 0. |
||
270 | * |
||
271 | * @param int|string $field |
||
272 | * @param int|string $cursor |
||
273 | * @return int |
||
274 | */ |
||
275 | protected function compareField($field, $cursor) |
||
279 | |||
280 | /** |
||
281 | * Return the n-th element of collection. |
||
282 | * Must return null if not exists. |
||
283 | * |
||
284 | * @param mixed $rows |
||
285 | * @param int $offset |
||
286 | * @return mixed |
||
287 | */ |
||
288 | abstract protected function offset($rows, $offset); |
||
289 | |||
290 | /** |
||
291 | * Slice rows, like PHP function array_slice(). |
||
292 | * |
||
293 | * @param mixed $rows |
||
294 | * @param int $offset |
||
295 | * @param null|int $length |
||
296 | * @return mixed |
||
297 | */ |
||
298 | abstract protected function slice($rows, $offset, $length = null); |
||
299 | |||
300 | /** |
||
301 | * Count rows, like PHP function count(). |
||
302 | * |
||
303 | * @param mixed $rows |
||
304 | * @return int |
||
305 | */ |
||
306 | abstract protected function count($rows); |
||
307 | |||
308 | /** |
||
309 | * Reverse rows, like PHP function array_reverse(). |
||
310 | * |
||
311 | * @param $rows |
||
312 | * @return mixed |
||
313 | */ |
||
314 | abstract protected function reverse($rows); |
||
315 | } |
||
316 |