1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Pagination\Util; |
4
|
|
|
|
5
|
|
|
use Silex\Application; |
6
|
|
|
use Symfony\Component\Routing\Router; |
7
|
|
|
use Symfony\Component\HttpFoundation\Request; |
8
|
|
|
|
9
|
|
|
class Paginator |
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* |
14
|
|
|
* @var Application $app |
15
|
|
|
*/ |
16
|
|
|
private $app; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* |
20
|
|
|
* @var Router $router |
21
|
|
|
*/ |
22
|
|
|
private $router; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* |
26
|
|
|
* @var Request $request |
27
|
|
|
*/ |
28
|
|
|
private $request; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* |
32
|
|
|
* @var array $options |
33
|
|
|
*/ |
34
|
|
|
private $options; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* |
38
|
|
|
* @param Application $app |
39
|
|
|
*/ |
40
|
3 |
|
public function __construct(Application $app) |
41
|
|
|
{ |
42
|
3 |
|
$this->app = $app; |
43
|
3 |
|
$this->router = $app['url_generator']; |
44
|
3 |
|
$this->request = $app['request']; |
45
|
3 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* |
49
|
|
|
* @param \Doctrine\ORM\QueryBuilder $query |
50
|
|
|
* @param array $options |
51
|
|
|
* |
52
|
|
|
* @return array |
53
|
|
|
* |
54
|
|
|
* @throws \Pagination\Exception\UnknownAdapterException |
55
|
|
|
*/ |
56
|
2 |
|
public function pagination($query, $options = array()) |
57
|
|
|
{ |
58
|
|
|
|
59
|
2 |
|
$this->options = array_replace($this->app['paginator.options'], $options); |
60
|
|
|
|
61
|
2 |
|
switch (true) { |
62
|
2 |
|
case $query instanceof \Doctrine\ORM\QueryBuilder : |
|
|
|
|
63
|
|
|
$adapter = new \Pagination\Adapter\PaginationORMAdapter(); |
64
|
|
|
break; |
65
|
2 |
|
case is_array($query) : |
|
|
|
|
66
|
1 |
|
$adapter = new \Pagination\Adapter\PaginationArrayAdapter(); |
67
|
1 |
|
break; |
68
|
1 |
|
default : |
|
|
|
|
69
|
1 |
|
throw new \Pagination\Exception\UnknownAdapterException(); |
70
|
1 |
|
} |
71
|
|
|
|
72
|
1 |
|
$current = $this->request->get('page', 1); |
73
|
1 |
|
$counter = $adapter->getCounter($query); |
|
|
|
|
74
|
|
|
|
75
|
1 |
|
$items = ceil($counter/$this->options['items_per_page']); |
76
|
|
|
|
77
|
|
|
return array( |
78
|
1 |
|
'first' => $this->createPage(1), |
79
|
1 |
|
'prev' => $current > 1 ? $this->createPage($current - 1) : null, |
80
|
1 |
|
'current' => $current, |
81
|
1 |
|
'items' => $adapter->getItems($query, $current, $this->options['items_per_page']), |
|
|
|
|
82
|
1 |
|
'pages' => $this->getPages($current, $items), |
83
|
1 |
|
'next' => $current < $items ? $this->createPage($current + 1) : null, |
84
|
1 |
|
'last' => $this->createPage($items), |
85
|
1 |
|
'total' => $counter, |
86
|
1 |
|
'total_page' => $items, |
87
|
1 |
|
'options' => $this->options |
88
|
1 |
|
); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* |
93
|
|
|
* @param integer $current |
94
|
|
|
* @param integer $items |
95
|
|
|
* |
96
|
|
|
* @return array |
97
|
|
|
*/ |
98
|
1 |
|
public function getPages($current, $items) |
99
|
|
|
{ |
100
|
|
|
|
101
|
1 |
|
$start = $current - $this->options['offset_page'] > 1 ? $current - $this->options['offset_page'] : 1; |
102
|
1 |
|
$end = $current + $this->options['offset_page'] < $items ? $current + $this->options['offset_page'] : $items; |
103
|
|
|
|
104
|
1 |
|
$pages = array(); |
105
|
1 |
|
for ($i = $start; $i <= $end; $i++) |
106
|
|
|
{ |
107
|
1 |
|
$pages[] = $this->createPage($i); |
108
|
1 |
|
} |
109
|
1 |
|
if ($start > 1) { |
110
|
|
|
$end_min = 1 + $this->options['offset_page'] >= $start ? $start - 1 : 1 + $this->options['offset_page']; |
111
|
|
|
if ($start - $end_min > 1) { |
112
|
|
|
array_unshift($pages, null); |
113
|
|
|
} |
114
|
|
|
for ($i = $end_min; $i >= 1; $i--) |
115
|
|
|
{ |
116
|
|
|
array_unshift($pages, $this->createPage($i)); |
117
|
|
|
} |
118
|
|
|
} |
119
|
1 |
|
if ($items - $end >= 1) { |
120
|
1 |
|
if ($items - $this->options['offset_page'] > $end + 1) { |
121
|
1 |
|
$pages[] = null; |
122
|
1 |
|
} |
123
|
1 |
|
$end_max = $items - $this->options['offset_page'] <= $end ? $end + 1 : $items - $this->options['offset_page']; |
124
|
1 |
|
for ($i = $end_max; $i <= $items; $i++) |
125
|
|
|
{ |
126
|
1 |
|
$pages[] = $this->createPage($i); |
127
|
1 |
|
} |
128
|
1 |
|
} |
129
|
|
|
|
130
|
1 |
|
return $pages; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* |
135
|
|
|
* @param integer $page |
136
|
|
|
* |
137
|
|
|
* @return array |
138
|
|
|
*/ |
139
|
1 |
|
public function createPage($page) |
140
|
|
|
{ |
141
|
|
|
return array( |
142
|
1 |
|
'page' => $page, |
143
|
1 |
|
'url' => $this->router->generate($this->request->get('_route'), array_merge( |
144
|
1 |
|
$this->request->query->all(), |
145
|
1 |
|
$this->request->get('_route_params'), |
146
|
1 |
|
array('page' => $page) |
147
|
1 |
|
)) |
148
|
1 |
|
); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
} |
152
|
|
|
|
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.