|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Application\Twig; |
|
4
|
|
|
|
|
5
|
|
|
use Silex\Application; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* @author Borut Balažek <[email protected]> |
|
9
|
|
|
*/ |
|
10
|
|
|
class PaginatorExtension extends \Twig_Extension |
|
11
|
|
|
{ |
|
12
|
|
|
private $app; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @param Application $app |
|
16
|
|
|
*/ |
|
17
|
|
|
public function __construct(Application $app) |
|
18
|
|
|
{ |
|
19
|
|
|
$this->app = $app; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @return string |
|
24
|
|
|
*/ |
|
25
|
|
|
public function getName() |
|
26
|
|
|
{ |
|
27
|
|
|
return 'application/paginator'; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @return array |
|
32
|
|
|
*/ |
|
33
|
|
|
public function getFunctions() |
|
34
|
|
|
{ |
|
35
|
|
|
return array( |
|
|
|
|
|
|
36
|
|
|
'paginator_limit_per_page_render' => new \Twig_Function_Method( |
|
37
|
|
|
$this, |
|
38
|
|
|
'paginatorLimitPerPageRender', |
|
39
|
|
|
array( |
|
40
|
|
|
'is_safe' => array('html'), |
|
41
|
|
|
) |
|
42
|
|
|
), |
|
43
|
|
|
'paginator_search_render' => new \Twig_Function_Method( |
|
44
|
|
|
$this, |
|
45
|
|
|
'paginatorSearchRender', |
|
46
|
|
|
array( |
|
47
|
|
|
'is_safe' => array('html'), |
|
48
|
|
|
) |
|
49
|
|
|
), |
|
50
|
|
|
'paginator_top_render' => new \Twig_Function_Method( |
|
51
|
|
|
$this, |
|
52
|
|
|
'paginatorTopRender', |
|
53
|
|
|
array( |
|
54
|
|
|
'is_safe' => array('html'), |
|
55
|
|
|
) |
|
56
|
|
|
), |
|
57
|
|
|
'paginator_pagination_render' => new \Twig_Function_Method( |
|
58
|
|
|
$this, |
|
59
|
|
|
'paginatorPaginationRender', |
|
60
|
|
|
array( |
|
61
|
|
|
'is_safe' => array('html'), |
|
62
|
|
|
) |
|
63
|
|
|
), |
|
64
|
|
|
'paginator_results_text_render' => new \Twig_Function_Method( |
|
65
|
|
|
$this, |
|
66
|
|
|
'paginatorResultsTextRender', |
|
67
|
|
|
array( |
|
68
|
|
|
'is_safe' => array('html'), |
|
69
|
|
|
) |
|
70
|
|
|
), |
|
71
|
|
|
'paginator_bottom_render' => new \Twig_Function_Method( |
|
72
|
|
|
$this, |
|
73
|
|
|
'paginatorBottomRender', |
|
74
|
|
|
array( |
|
75
|
|
|
'is_safe' => array('html'), |
|
76
|
|
|
) |
|
77
|
|
|
), |
|
78
|
|
|
'paginator_sortable' => new \Twig_Function_Method( |
|
79
|
|
|
$this, |
|
80
|
|
|
'paginatorSortable', |
|
81
|
|
|
array( |
|
82
|
|
|
'is_safe' => array('html'), |
|
83
|
|
|
) |
|
84
|
|
|
), |
|
85
|
|
|
); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @param $pagination |
|
90
|
|
|
* |
|
91
|
|
|
* @return string |
|
92
|
|
|
*/ |
|
93
|
|
|
public function paginatorPaginationRender($pagination) |
|
94
|
|
|
{ |
|
95
|
|
|
$output = ''; |
|
96
|
|
|
|
|
97
|
|
|
$paginationData = $pagination->getPaginationData(); |
|
98
|
|
|
$maxPageRange = isset($paginationData['pageRangeLimit']) |
|
99
|
|
|
? intval($paginationData['pageRangeLimit']) |
|
100
|
|
|
: 10 |
|
101
|
|
|
; |
|
102
|
|
|
$route = $paginationData['route']; |
|
103
|
|
|
$routeParameters = $this->app['request']->query->all(); |
|
104
|
|
|
if (isset($paginationData['routeParameters'])) { |
|
105
|
|
|
$routeParameters = array_merge( |
|
106
|
|
|
$routeParameters, |
|
107
|
|
|
$paginationData['routeParameters'] |
|
108
|
|
|
); |
|
109
|
|
|
} |
|
110
|
|
|
$pageCount = ceil( |
|
111
|
|
|
intval($paginationData['totalCount']) / |
|
112
|
|
|
intval($paginationData['numItemsPerPage']) |
|
113
|
|
|
); |
|
114
|
|
|
$currentPage = intval($paginationData['current']); |
|
115
|
|
|
|
|
116
|
|
|
if ($pageCount > 1) { |
|
117
|
|
|
$pageRange = range(1, $pageCount); |
|
118
|
|
|
|
|
119
|
|
|
// Page range by max page numbers |
|
120
|
|
|
$pageRangeTmp = array(); |
|
121
|
|
|
$rangeFrom = $currentPage - ceil($maxPageRange / 2); |
|
122
|
|
|
$rangeTo = $currentPage + ceil($maxPageRange / 2); |
|
123
|
|
|
|
|
124
|
|
|
foreach (range($rangeFrom, $rangeTo) as $singleRangePage) { |
|
125
|
|
|
if (in_array($singleRangePage, $pageRange)) { |
|
126
|
|
|
$pageRangeTmp[] = $singleRangePage; |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
$pageRange = $pageRangeTmp; |
|
131
|
|
|
// Page range by max page numbers /END |
|
132
|
|
|
|
|
133
|
|
|
// Prev |
|
134
|
|
View Code Duplication |
if ($currentPage > 1) { |
|
|
|
|
|
|
135
|
|
|
$routeParameters = array_merge( |
|
136
|
|
|
$routeParameters, |
|
137
|
|
|
array( |
|
138
|
|
|
$pagination->getPaginatorOption('pageParameterName') => $currentPage - 1, |
|
139
|
|
|
) |
|
140
|
|
|
); |
|
141
|
|
|
|
|
142
|
|
|
$prevUrl = $this->app['url_generator']->generate( |
|
143
|
|
|
$route, |
|
144
|
|
|
$routeParameters |
|
145
|
|
|
); |
|
146
|
|
|
} else { |
|
147
|
|
|
$prevUrl = '#'; |
|
148
|
|
|
} |
|
149
|
|
|
// Prev /END |
|
150
|
|
|
|
|
151
|
|
|
// Next |
|
152
|
|
View Code Duplication |
if ($currentPage < $pageCount) { |
|
|
|
|
|
|
153
|
|
|
$routeParameters = array_merge( |
|
154
|
|
|
$routeParameters, |
|
155
|
|
|
array( |
|
156
|
|
|
$pagination->getPaginatorOption('pageParameterName') => $currentPage + 1, |
|
157
|
|
|
) |
|
158
|
|
|
); |
|
159
|
|
|
|
|
160
|
|
|
$nextUrl = $this->app['url_generator']->generate( |
|
161
|
|
|
$route, |
|
162
|
|
|
$routeParameters |
|
163
|
|
|
); |
|
164
|
|
|
} else { |
|
165
|
|
|
$nextUrl = '#'; |
|
166
|
|
|
} |
|
167
|
|
|
// Next /END |
|
168
|
|
|
|
|
169
|
|
|
$output = $this->app['twig']->render( |
|
170
|
|
|
'twig/paginator/pagination.html.twig', |
|
171
|
|
|
array( |
|
172
|
|
|
'app' => $this->app, |
|
173
|
|
|
'prevUrl' => $prevUrl, |
|
174
|
|
|
'nextUrl' => $nextUrl, |
|
175
|
|
|
'pageRange' => $pageRange, |
|
176
|
|
|
'routeParameters' => $routeParameters, |
|
177
|
|
|
'pagination' => $pagination, |
|
178
|
|
|
'route' => $route, |
|
179
|
|
|
'currentPage' => $currentPage, |
|
180
|
|
|
'pageCount' => $pageCount, |
|
181
|
|
|
) |
|
182
|
|
|
); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
return $output; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* @param $pagination |
|
190
|
|
|
* |
|
191
|
|
|
* @return string |
|
192
|
|
|
*/ |
|
193
|
|
|
public function paginatorResultsTextRender($pagination) |
|
194
|
|
|
{ |
|
195
|
|
|
$output = ''; |
|
196
|
|
|
|
|
197
|
|
|
$paginationData = $pagination->getPaginationData(); |
|
198
|
|
|
$pageCount = ceil( |
|
199
|
|
|
intval($paginationData['totalCount']) / |
|
200
|
|
|
intval($paginationData['numItemsPerPage']) |
|
201
|
|
|
); |
|
202
|
|
|
$currentPage = intval($paginationData['current']); |
|
203
|
|
|
$total = $paginationData['totalCount']; |
|
204
|
|
|
|
|
205
|
|
|
if ($total > 0 && $currentPage <= $pageCount) { |
|
206
|
|
|
$from = (($currentPage -1) * $paginationData['numItemsPerPage']) + 1; |
|
207
|
|
|
$to = $currentPage * $paginationData['numItemsPerPage'] > $total |
|
208
|
|
|
? $total |
|
209
|
|
|
: $currentPage * $paginationData['numItemsPerPage'] |
|
210
|
|
|
; |
|
211
|
|
|
|
|
212
|
|
|
$output = $this->app['twig']->render( |
|
213
|
|
|
'twig/paginator/results-text.html.twig', |
|
214
|
|
|
array( |
|
215
|
|
|
'app' => $this->app, |
|
216
|
|
|
'from' => $from, |
|
217
|
|
|
'to' => $to, |
|
218
|
|
|
'total' => $total, |
|
219
|
|
|
) |
|
220
|
|
|
); |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
return $output; |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
|
|
* @param $pagination |
|
228
|
|
|
* |
|
229
|
|
|
* @return string |
|
230
|
|
|
*/ |
|
231
|
|
|
public function paginatorBottomRender($pagination) |
|
232
|
|
|
{ |
|
233
|
|
|
return $this->app['twig']->render( |
|
234
|
|
|
'twig/paginator/_bottom.html.twig', |
|
235
|
|
|
array( |
|
236
|
|
|
'pagination' => $pagination, |
|
237
|
|
|
) |
|
238
|
|
|
); |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
/** |
|
242
|
|
|
* @param $pagination |
|
243
|
|
|
* |
|
244
|
|
|
* @return string |
|
245
|
|
|
*/ |
|
246
|
|
|
public function paginatorLimitPerPageRender($pagination) |
|
247
|
|
|
{ |
|
248
|
|
|
$output = ''; |
|
249
|
|
|
|
|
250
|
|
|
$paginationData = $pagination->getPaginationData(); |
|
251
|
|
|
|
|
252
|
|
|
if ($paginationData['totalCount'] > 0) { |
|
253
|
|
|
$output = $this->app['twig']->render( |
|
254
|
|
|
'twig/paginator/limit-per-page.html.twig', |
|
255
|
|
|
array( |
|
256
|
|
|
'app' => $this->app, |
|
257
|
|
|
'pagination' => $pagination, |
|
258
|
|
|
) |
|
259
|
|
|
); |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
return $output; |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
/** |
|
266
|
|
|
* @param $pagination |
|
267
|
|
|
* |
|
268
|
|
|
* @return string |
|
269
|
|
|
*/ |
|
270
|
|
|
public function paginatorSearchRender($pagination) |
|
271
|
|
|
{ |
|
272
|
|
|
return $this->app['twig']->render( |
|
273
|
|
|
'twig/paginator/search.html.twig', |
|
274
|
|
|
array( |
|
275
|
|
|
'app' => $this->app, |
|
276
|
|
|
'pagination' => $pagination, |
|
277
|
|
|
) |
|
278
|
|
|
); |
|
279
|
|
|
} |
|
280
|
|
|
|
|
281
|
|
|
/** |
|
282
|
|
|
* @param $pagination |
|
283
|
|
|
* |
|
284
|
|
|
* @return string |
|
285
|
|
|
*/ |
|
286
|
|
|
public function paginatorTopRender($pagination) |
|
287
|
|
|
{ |
|
288
|
|
|
return $this->app['twig']->render( |
|
289
|
|
|
'twig/paginator/_top.html.twig', |
|
290
|
|
|
array( |
|
291
|
|
|
'pagination' => $pagination, |
|
292
|
|
|
) |
|
293
|
|
|
); |
|
294
|
|
|
} |
|
295
|
|
|
|
|
296
|
|
|
/** |
|
297
|
|
|
* @param $pagination |
|
298
|
|
|
* @param $text |
|
299
|
|
|
* @param $key |
|
300
|
|
|
* |
|
301
|
|
|
* @return string |
|
302
|
|
|
*/ |
|
303
|
|
|
public function paginatorSortable($pagination, $text = '', $key = '') |
|
304
|
|
|
{ |
|
305
|
|
|
$text = $this->app['translator']->trans($text); |
|
306
|
|
|
|
|
307
|
|
|
$sortDirectionParameterName = $pagination->getPaginatorOption('sortDirectionParameterName'); |
|
308
|
|
|
$direction = isset($sortDirectionParameterName) |
|
309
|
|
|
? $this->app['request']->query->get($sortDirectionParameterName) |
|
310
|
|
|
: 'asc' |
|
311
|
|
|
; |
|
312
|
|
|
|
|
313
|
|
|
$direction = $direction == 'asc' |
|
314
|
|
|
? 'desc' |
|
315
|
|
|
: 'asc' |
|
316
|
|
|
; |
|
317
|
|
|
|
|
318
|
|
|
$paginationData = $pagination->getPaginationData(); |
|
319
|
|
|
$route = $paginationData['route']; |
|
320
|
|
|
$routeParameters = $this->app['request']->query->all(); |
|
321
|
|
|
if (isset($paginationData['routeParameters'])) { |
|
322
|
|
|
$routeParameters = array_merge( |
|
323
|
|
|
$routeParameters, |
|
324
|
|
|
$paginationData['routeParameters'] |
|
325
|
|
|
); |
|
326
|
|
|
} |
|
327
|
|
|
$routeParameters = array_merge( |
|
328
|
|
|
$routeParameters, |
|
329
|
|
|
array( |
|
330
|
|
|
$pagination->getPaginatorOption('pageParameterName') => 1, |
|
331
|
|
|
$pagination->getPaginatorOption('sortFieldParameterName') => $key, |
|
332
|
|
|
$pagination->getPaginatorOption('sortDirectionParameterName') => $direction, |
|
333
|
|
|
) |
|
334
|
|
|
); |
|
335
|
|
|
|
|
336
|
|
|
$url = $this->app['url_generator']->generate( |
|
337
|
|
|
$route, |
|
338
|
|
|
$routeParameters |
|
339
|
|
|
); |
|
340
|
|
|
|
|
341
|
|
|
$icon = $direction == 'asc' |
|
342
|
|
|
? 'down' |
|
343
|
|
|
: 'up' |
|
344
|
|
|
; |
|
345
|
|
|
|
|
346
|
|
|
$showIcon = $this->app['request']->query->get( |
|
347
|
|
|
$pagination->getPaginatorOption('sortFieldParameterName'), |
|
348
|
|
|
$paginationData['defaultSortFieldName'] |
|
349
|
|
|
) == $key |
|
350
|
|
|
? true |
|
351
|
|
|
: false |
|
352
|
|
|
; |
|
353
|
|
|
|
|
354
|
|
|
return '<a href="'.$url.'">'. |
|
355
|
|
|
$text. |
|
356
|
|
|
($showIcon ? ' <i class="fa fa-chevron-'.$icon.'"></i>' : ''). |
|
357
|
|
|
'</a>'; |
|
358
|
|
|
} |
|
359
|
|
|
} |
|
360
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.