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