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
|
|
|
use Exception; |
16
|
|
|
use RuntimeException; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* A paginator adapter for doctrine pagination. |
20
|
|
|
* |
21
|
|
|
* @author Fraser Stockley <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class DoctrinePaginatorAdapter implements PaginatorInterface |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* The paginator instance. |
27
|
|
|
* @var Paginator<mixed> |
28
|
|
|
*/ |
29
|
|
|
private $paginator; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* The route generator. |
33
|
|
|
* |
34
|
|
|
* @var callable |
35
|
|
|
*/ |
36
|
|
|
private $routeGenerator; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Create a new DoctrinePaginatorAdapter. |
40
|
|
|
* @param Paginator<mixed> $paginator |
|
|
|
|
41
|
|
|
* @param callable $routeGenerator |
42
|
|
|
* |
43
|
|
|
*/ |
44
|
1 |
|
public function __construct(Paginator $paginator, callable $routeGenerator) |
45
|
|
|
{ |
46
|
1 |
|
$this->paginator = $paginator; |
47
|
1 |
|
$this->routeGenerator = $routeGenerator; |
48
|
1 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Get the current page. |
52
|
|
|
* |
53
|
|
|
* @return int |
54
|
|
|
*/ |
55
|
1 |
|
public function getCurrentPage() |
56
|
|
|
{ |
57
|
1 |
|
return (int) ($this->paginator->getQuery()->getFirstResult() / $this->paginator->getQuery()->getMaxResults()) + 1; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Get the last page. |
62
|
|
|
* |
63
|
|
|
* @return int |
64
|
|
|
*/ |
65
|
1 |
|
public function getLastPage() |
66
|
|
|
{ |
67
|
1 |
|
return (int) ceil($this->getTotal() / $this->paginator->getQuery()->getMaxResults()); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Get the total. |
72
|
|
|
* |
73
|
|
|
* @return int |
74
|
|
|
*/ |
75
|
1 |
|
public function getTotal() |
76
|
|
|
{ |
77
|
1 |
|
return count($this->paginator); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Get the count. |
82
|
|
|
* |
83
|
|
|
* @return int |
84
|
|
|
* |
85
|
|
|
* @throws Exception If the iterator cannot be resolved |
86
|
|
|
* @throws RuntimeException If the returned iterator can't be counted |
87
|
|
|
*/ |
88
|
1 |
|
public function getCount() |
89
|
|
|
{ |
90
|
1 |
|
$iterator = $this->paginator->getIterator(); |
91
|
1 |
|
if (!is_callable([$iterator, 'count'])) { |
92
|
|
|
throw new RuntimeException('Invalid iterator returned by paginator. Must have a callable "count" method.'); |
93
|
|
|
} |
94
|
1 |
|
return $iterator->count(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Get the number per page. |
99
|
|
|
* |
100
|
|
|
* @return int |
101
|
|
|
*/ |
102
|
1 |
|
public function getPerPage() |
103
|
|
|
{ |
104
|
1 |
|
return (int) $this->paginator->getQuery()->getMaxResults(); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Get the url for the given page. |
109
|
|
|
* |
110
|
|
|
* @param int $page |
111
|
|
|
* |
112
|
|
|
* @return string |
113
|
|
|
*/ |
114
|
1 |
|
public function getUrl($page) |
115
|
|
|
{ |
116
|
1 |
|
return call_user_func($this->getRouteGenerator(), $page); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Get the the route generator. |
121
|
|
|
* |
122
|
|
|
* @return callable |
123
|
|
|
*/ |
124
|
1 |
|
private function getRouteGenerator() |
125
|
|
|
{ |
126
|
1 |
|
return $this->routeGenerator; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.