|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright (c) Phauthentic (https://github.com/Phauthentic) |
|
4
|
|
|
* |
|
5
|
|
|
* Licensed under The MIT License |
|
6
|
|
|
* For full copyright and license information, please see the LICENSE.txt |
|
7
|
|
|
* Redistributions of files must retain the above copyright notice. |
|
8
|
|
|
* |
|
9
|
|
|
* @copyright Copyright (c) Phauthentic (https://github.com/Phauthentic) |
|
10
|
|
|
* @link https://github.com/Phauthentic |
|
11
|
|
|
* @license https://opensource.org/licenses/mit-license.php MIT License |
|
12
|
|
|
*/ |
|
13
|
|
|
declare(strict_types = 1); |
|
14
|
|
|
|
|
15
|
|
|
namespace Phauthentic\Pagination\Paginator; |
|
16
|
|
|
|
|
17
|
|
|
use Phauthentic\Pagination\PaginationParams; |
|
18
|
|
|
use Phauthentic\Pagination\PaginationParamsInterface; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Paginates arrays |
|
22
|
|
|
*/ |
|
23
|
|
|
class ArrayPaginator implements PaginatorInterface |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* Sort Handler callback |
|
27
|
|
|
* |
|
28
|
|
|
* @var callable |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $sortHandler; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Maps the params to the repository |
|
34
|
|
|
* |
|
35
|
|
|
* @param \Phauthentic\Pagination\PaginationParamsInterface $paginationParams Pagination params |
|
36
|
|
|
* @param array $array |
|
37
|
|
|
* @return array |
|
38
|
|
|
*/ |
|
39
|
1 |
|
public function paginate($array, PaginationParamsInterface $paginationParams) |
|
40
|
|
|
{ |
|
41
|
1 |
|
$paginationParams->setCount(count($array)); |
|
42
|
|
|
|
|
43
|
1 |
|
$count = 1; |
|
44
|
1 |
|
$data = []; |
|
45
|
1 |
|
$offset = $paginationParams->getOffset(); |
|
46
|
1 |
|
$stopAt = $offset + $paginationParams->getLimit(); |
|
47
|
|
|
|
|
48
|
1 |
|
$array = $this->sort($array, $paginationParams); |
|
49
|
|
|
|
|
50
|
1 |
|
foreach ($array as $key => $value) { |
|
51
|
1 |
|
if ($count > $stopAt) { |
|
52
|
1 |
|
break; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
1 |
|
if ($count > $paginationParams->getOffset()) { |
|
56
|
1 |
|
$data[$key] = $value; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
1 |
|
$count++; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
1 |
|
return $data; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Sorts the array by a callback |
|
67
|
|
|
* |
|
68
|
|
|
* @param array $array Array |
|
69
|
|
|
* @param \Phauthentic\Pagination\PaginationParamsInterface $paginationParams Pagination params |
|
70
|
|
|
* @return array |
|
71
|
|
|
*/ |
|
72
|
1 |
|
protected function sort($array, PaginationParams $paginationParams) |
|
73
|
|
|
{ |
|
74
|
1 |
|
if (empty($this->setSortHandler)) { |
|
|
|
|
|
|
75
|
1 |
|
return $array; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
$handler = $this->sortHandler; |
|
79
|
|
|
|
|
80
|
|
|
return $handler($array, $paginationParams); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Sets the sort handler callback |
|
85
|
|
|
* |
|
86
|
|
|
* @return $this |
|
87
|
|
|
*/ |
|
88
|
|
|
public function setSortHandler(callable $handler): self |
|
89
|
|
|
{ |
|
90
|
|
|
$this->sortHandler = $handler; |
|
91
|
|
|
|
|
92
|
|
|
return $this; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|