1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* AnimeDb package. |
4
|
|
|
* |
5
|
|
|
* @author Peter Gribanov <[email protected]> |
6
|
|
|
* @copyright Copyright (c) 2011, Peter Gribanov |
7
|
|
|
* @license http://opensource.org/licenses/MIT |
8
|
|
|
*/ |
9
|
|
|
namespace AnimeDb\Bundle\PaginationBundle\Service; |
10
|
|
|
|
11
|
|
|
use AnimeDb\Bundle\PaginationBundle\Entity\Node; |
12
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @author Peter Gribanov <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class View implements \IteratorAggregate |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var Configuration |
21
|
|
|
*/ |
22
|
|
|
protected $config; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var NavigateRange |
26
|
|
|
*/ |
27
|
|
|
protected $range; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var Node|null |
31
|
|
|
*/ |
32
|
|
|
protected $first; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var Node|null |
36
|
|
|
*/ |
37
|
|
|
protected $prev; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var Node |
41
|
|
|
*/ |
42
|
|
|
protected $current; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var Node|null |
46
|
|
|
*/ |
47
|
|
|
protected $next; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var Node|null |
51
|
|
|
*/ |
52
|
|
|
protected $last; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var ArrayCollection|null |
56
|
|
|
*/ |
57
|
|
|
protected $list; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param Configuration $config |
61
|
|
|
* @param NavigateRange $range |
62
|
|
|
*/ |
63
|
27 |
|
public function __construct(Configuration $config, NavigateRange $range) |
64
|
|
|
{ |
65
|
27 |
|
$this->config = $config; |
66
|
27 |
|
$this->range = $range; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return int |
71
|
|
|
*/ |
72
|
|
|
public function getTotal() |
73
|
|
|
{ |
74
|
|
|
return $this->config->getTotalPages(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return Node|null |
79
|
|
|
*/ |
80
|
5 |
|
public function getFirst() |
81
|
|
|
{ |
82
|
|
|
if (!$this->first && $this->config->getCurrentPage() > 1) { |
83
|
|
|
$this->first = new Node(1, $this->buildLink(1)); |
84
|
|
|
} |
85
|
|
|
|
86
|
5 |
|
return $this->first; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return Node|null |
91
|
|
|
*/ |
92
|
3 |
|
public function getPrev() |
93
|
|
|
{ |
94
|
|
|
if (!$this->prev && $this->config->getCurrentPage() > 1) { |
95
|
2 |
|
$this->prev = new Node( |
96
|
|
|
$this->config->getCurrentPage() - 1, |
97
|
|
|
$this->buildLink($this->config->getCurrentPage() - 1) |
98
|
|
|
); |
99
|
|
|
} |
100
|
|
|
|
101
|
3 |
|
return $this->prev; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return Node |
106
|
|
|
*/ |
107
|
10 |
|
public function getCurrent() |
108
|
|
|
{ |
109
|
10 |
|
if (!$this->current) { |
110
|
10 |
|
$this->current = new Node( |
111
|
10 |
|
$this->config->getCurrentPage(), |
112
|
|
|
$this->buildLink($this->config->getCurrentPage()), |
113
|
10 |
|
true |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
|
117
|
10 |
|
return $this->current; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return Node|null |
122
|
|
|
*/ |
123
|
3 |
|
public function getNext() |
124
|
|
|
{ |
125
|
|
|
if (!$this->next && $this->config->getCurrentPage() < $this->getTotal()) { |
126
|
2 |
|
$this->next = new Node( |
127
|
|
|
$this->config->getCurrentPage() + 1, |
128
|
|
|
$this->buildLink($this->config->getCurrentPage() + 1) |
129
|
|
|
); |
130
|
|
|
} |
131
|
|
|
|
132
|
3 |
|
return $this->next; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @return Node|null |
137
|
|
|
*/ |
138
|
3 |
|
public function getLast() |
139
|
|
|
{ |
140
|
|
|
if (!$this->last && $this->config->getCurrentPage() < $this->getTotal()) { |
141
|
|
|
$this->last = new Node($this->getTotal(), $this->buildLink($this->getTotal())); |
142
|
|
|
} |
143
|
|
|
|
144
|
3 |
|
return $this->last; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @return ArrayCollection |
149
|
|
|
*/ |
150
|
7 |
|
public function getIterator() |
151
|
|
|
{ |
152
|
7 |
|
if (!($this->list instanceof ArrayCollection)) { |
153
|
|
|
$this->list = new ArrayCollection(); |
154
|
|
|
|
155
|
|
|
if ($this->getTotal() > 1) { |
156
|
|
|
// determining the first and last pages in paging based on the current page and offset |
157
|
|
|
$page = $this->config->getCurrentPage() - $this->range->getLeftOffset(); |
158
|
|
|
$page_to = $this->config->getCurrentPage() + $this->range->getRightOffset(); |
159
|
|
|
|
160
|
|
|
while ($page <= $page_to) { |
161
|
|
|
if ($page == $this->config->getCurrentPage()) { |
162
|
|
|
$this->list->add($this->getCurrent()); |
163
|
|
|
} else { |
164
|
|
|
$this->list->add(new Node($page, $this->buildLink($page))); |
165
|
6 |
|
} |
166
|
|
|
$page++; |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
171
|
7 |
|
return $this->list; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @param int $page |
176
|
|
|
* |
177
|
|
|
* @return string |
178
|
|
|
*/ |
179
|
6 |
|
protected function buildLink($page) |
180
|
|
|
{ |
181
|
6 |
|
if ($page == 1 && $this->config->getFirstPageLink()) { |
182
|
|
|
return $this->config->getFirstPageLink(); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
if (is_callable($this->config->getPageLink())) { |
186
|
|
|
return call_user_func($this->config->getPageLink(), $page); |
187
|
|
|
} else { |
188
|
|
|
return sprintf($this->config->getPageLink(), $page); |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|