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