1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of gpupo/search |
5
|
|
|
* |
6
|
|
|
* (c) Gilmar Pupo <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Gpupo\Search\Paginator; |
13
|
|
|
|
14
|
|
|
use Gpupo\Search\Result\CollectionInterface; |
15
|
|
|
|
16
|
|
|
class Paginator extends PaginatorAbstract implements PaginatorInterface |
17
|
|
|
{ |
18
|
|
|
public function paginateResult(CollectionInterface $collection, $page, $limit = 10) |
19
|
|
|
{ |
20
|
|
|
$limit = intval(abs($limit)); |
21
|
|
|
if (!$limit) { |
22
|
|
|
throw new \LogicException('Invalid item per page number, must be a positive number'); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
$this->paginate($collection->getTotalFound(), $page, $limit); |
26
|
|
|
|
27
|
|
|
return $this; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function getPaginationData() |
31
|
|
|
{ |
32
|
|
|
$pages = $this->getPages(); |
33
|
|
|
|
34
|
|
|
$viewData = [ |
35
|
|
|
'last' => $this->getPagesCount(), |
36
|
|
|
'current' => $this->getCurrentPageNumber(), |
37
|
|
|
'numItemsPerPage' => $this->getItemNumberPerPage(), |
38
|
|
|
'first' => 1, |
39
|
|
|
'pageCount' => $this->getPagesCount(), |
40
|
|
|
'totalCount' => $this->getTotalItemCount(), |
41
|
|
|
]; |
42
|
|
|
|
43
|
|
|
if ($this->getCurrentPageNumber() - 1 > 0) { |
44
|
|
|
$viewData['previous'] = $this->getCurrentPageNumber() - 1; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
if ($this->getCurrentPageNumber() + 1 <= $this->getPagesCount()) { |
48
|
|
|
$viewData['next'] = $this->getCurrentPageNumber() + 1; |
49
|
|
|
} |
50
|
|
|
$viewData['pagesInRange'] = $pages; |
51
|
|
|
$viewData['firstPageInRange'] = min($pages); |
52
|
|
|
$viewData['lastPageInRange'] = max($pages); |
53
|
|
|
|
54
|
|
|
return $viewData; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Acesso ao range de paginas para navegacao. |
59
|
|
|
* |
60
|
|
|
* @return array |
61
|
|
|
*/ |
62
|
|
|
public function getPages() |
63
|
|
|
{ |
64
|
|
|
if ($this->getPageRange() > $this->getPagesCount()) { |
65
|
|
|
$this->setPageRange($this->getPagesCount()); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$delta = ceil($this->getPageRange() / 2); |
69
|
|
|
|
70
|
|
|
if ($this->getCurrentPageNumber() - $delta > $this->getPagesCount() - $this->getPageRange()) { |
71
|
|
|
$pages = range($this->getPagesCount() - $this->getPageRange() + 1, $this->getPagesCount()); |
72
|
|
|
} else { |
73
|
|
|
if ($this->getCurrentPageNumber() - $delta < 0) { |
74
|
|
|
$delta = $this->getCurrentPageNumber(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$offset = $this->getCurrentPageNumber() - $delta; |
78
|
|
|
$pages = range($offset + 1, $offset + $this->getPageRange()); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $pages; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|