1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the League\Fractal package. |
4
|
|
|
* |
5
|
|
|
* (c) Phil Sturgeon <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace League\Fractal\Pagination; |
12
|
|
|
|
13
|
|
|
use Doctrine\ORM\Tools\Pagination\Paginator; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* A paginator adapter for doctrine pagination. |
17
|
|
|
* |
18
|
|
|
* @author Fraser Stockley <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
class DoctrinePaginatorAdapter implements PaginatorInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* The paginator instance. |
24
|
|
|
* @var Paginator |
25
|
|
|
*/ |
26
|
|
|
private $paginator; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The route generator. |
30
|
|
|
* |
31
|
|
|
* @var callable |
32
|
|
|
*/ |
33
|
|
|
private $routeGenerator; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Create a new DoctrinePaginatorAdapter. |
37
|
|
|
* @param Paginator $paginator |
38
|
|
|
* @param callable $routeGenerator |
39
|
|
|
* |
40
|
|
|
*/ |
41
|
1 |
|
public function __construct(Paginator $paginator, callable $routeGenerator) |
42
|
|
|
{ |
43
|
1 |
|
$this->paginator = $paginator; |
44
|
1 |
|
$this->routeGenerator = $routeGenerator; |
45
|
1 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Get the current page. |
49
|
|
|
* |
50
|
|
|
* @return int |
51
|
|
|
*/ |
52
|
1 |
|
public function getCurrentPage() |
53
|
|
|
{ |
54
|
1 |
|
return ($this->paginator->getQuery()->getFirstResult() / $this->paginator->getQuery()->getMaxResults()) + 1; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Get the last page. |
59
|
|
|
* |
60
|
|
|
* @return int |
61
|
|
|
*/ |
62
|
1 |
|
public function getLastPage() |
63
|
|
|
{ |
64
|
1 |
|
return (int) ceil($this->getTotal() / $this->paginator->getQuery()->getMaxResults()); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get the total. |
69
|
|
|
* |
70
|
|
|
* @return int |
71
|
|
|
*/ |
72
|
1 |
|
public function getTotal() |
73
|
|
|
{ |
74
|
1 |
|
return count($this->paginator); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Get the count. |
79
|
|
|
* |
80
|
|
|
* @return int |
81
|
|
|
*/ |
82
|
1 |
|
public function getCount() |
83
|
|
|
{ |
84
|
1 |
|
return $this->paginator->getIterator()->count(); |
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Get the number per page. |
89
|
|
|
* |
90
|
|
|
* @return int |
91
|
|
|
*/ |
92
|
1 |
|
public function getPerPage() |
93
|
|
|
{ |
94
|
1 |
|
return $this->paginator->getQuery()->getMaxResults(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Get the url for the given page. |
99
|
|
|
* |
100
|
|
|
* @param int $page |
101
|
|
|
* |
102
|
|
|
* @return string |
103
|
|
|
*/ |
104
|
1 |
|
public function getUrl($page) |
105
|
|
|
{ |
106
|
1 |
|
return call_user_func($this->getRouteGenerator(), $page); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Get the the route generator. |
111
|
|
|
* |
112
|
|
|
* @return callable |
113
|
|
|
*/ |
114
|
1 |
|
private function getRouteGenerator() |
115
|
|
|
{ |
116
|
1 |
|
return $this->routeGenerator; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: