1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the League\Fractal package. |
5
|
|
|
* |
6
|
|
|
* (c) Phil Sturgeon <[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 League\Fractal\Pagination; |
13
|
|
|
|
14
|
|
|
use Doctrine\ORM\Tools\Pagination\Paginator; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* A paginator adapter for doctrine pagination. |
18
|
|
|
* |
19
|
|
|
* @author Fraser Stockley <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class DoctrinePaginatorAdapter implements PaginatorInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* The paginator instance. |
25
|
|
|
* @var Paginator |
26
|
|
|
*/ |
27
|
|
|
private $paginator; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The route generator. |
31
|
|
|
* |
32
|
|
|
* @var callable |
33
|
|
|
*/ |
34
|
|
|
private $routeGenerator; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Create a new DoctrinePaginatorAdapter. |
38
|
|
|
* @param Paginator $paginator |
39
|
|
|
* @param callable $routeGenerator |
40
|
|
|
* |
41
|
|
|
*/ |
42
|
1 |
|
public function __construct(Paginator $paginator, callable $routeGenerator) |
43
|
|
|
{ |
44
|
1 |
|
$this->paginator = $paginator; |
45
|
1 |
|
$this->routeGenerator = $routeGenerator; |
46
|
1 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Get the current page. |
50
|
|
|
* |
51
|
|
|
* @return int |
52
|
|
|
*/ |
53
|
1 |
|
public function getCurrentPage() |
54
|
|
|
{ |
55
|
1 |
|
return (int) ($this->paginator->getQuery()->getFirstResult() / $this->paginator->getQuery()->getMaxResults()) + 1; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Get the last page. |
60
|
|
|
* |
61
|
|
|
* @return int |
62
|
|
|
*/ |
63
|
1 |
|
public function getLastPage() |
64
|
|
|
{ |
65
|
1 |
|
return (int) ceil($this->getTotal() / $this->paginator->getQuery()->getMaxResults()); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Get the total. |
70
|
|
|
* |
71
|
|
|
* @return int |
72
|
|
|
*/ |
73
|
1 |
|
public function getTotal() |
74
|
|
|
{ |
75
|
1 |
|
return count($this->paginator); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Get the count. |
80
|
|
|
* |
81
|
|
|
* @return int |
82
|
|
|
*/ |
83
|
1 |
|
public function getCount() |
84
|
|
|
{ |
85
|
1 |
|
return $this->paginator->getIterator()->count(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Get the number per page. |
90
|
|
|
* |
91
|
|
|
* @return int |
92
|
|
|
*/ |
93
|
1 |
|
public function getPerPage() |
94
|
|
|
{ |
95
|
1 |
|
return (int) $this->paginator->getQuery()->getMaxResults(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Get the url for the given page. |
100
|
|
|
* |
101
|
|
|
* @param int $page |
102
|
|
|
* |
103
|
|
|
* @return string |
104
|
|
|
*/ |
105
|
1 |
|
public function getUrl($page) |
106
|
|
|
{ |
107
|
1 |
|
return call_user_func($this->getRouteGenerator(), $page); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Get the the route generator. |
112
|
|
|
* |
113
|
|
|
* @return callable |
114
|
|
|
*/ |
115
|
1 |
|
private function getRouteGenerator() |
116
|
|
|
{ |
117
|
1 |
|
return $this->routeGenerator; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|