1
|
|
|
<?php |
2
|
|
|
namespace Tuum\Pagination\ToHtml; |
3
|
|
|
|
4
|
|
|
use Tuum\Pagination\Paginate\Page; |
5
|
|
|
use Tuum\Pagination\Paginate\Paginate; |
6
|
|
|
use Tuum\Pagination\Paginate\PaginateInterface; |
7
|
|
|
|
8
|
|
View Code Duplication |
class ToBootstrap3 implements ToHtmlInterface |
|
|
|
|
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var Paginate |
12
|
|
|
*/ |
13
|
|
|
private $paginate; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* ToBootstrap3 constructor. |
17
|
|
|
* |
18
|
|
|
* @param Paginate $paginate |
19
|
|
|
*/ |
20
|
|
|
public function __construct($paginate = null) |
21
|
|
|
{ |
22
|
|
|
$this->paginate = $paginate; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param PaginateInterface $paginate |
27
|
|
|
* @return ToHtmlInterface |
28
|
|
|
*/ |
29
|
|
|
public function setPaginate(PaginateInterface $paginate) |
30
|
|
|
{ |
31
|
|
|
$this->paginate = $paginate; |
|
|
|
|
32
|
|
|
return $this; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @return string |
37
|
|
|
*/ |
38
|
|
|
public function __toString() |
39
|
|
|
{ |
40
|
|
|
return $this->toString(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @return string |
45
|
|
|
*/ |
46
|
|
|
public function toString() |
47
|
|
|
{ |
48
|
|
|
$paginate = $this->paginate; |
49
|
|
|
$html = '<ul class="pagination">'; |
50
|
|
|
$html .= $this->liLabel($paginate->getPrevPage(), '<'); |
51
|
|
|
$html .= $this->li($paginate->getFirstPage()); |
52
|
|
|
foreach ($paginate as $page) { |
53
|
|
|
$html .= $this->li($page); |
54
|
|
|
} |
55
|
|
|
$last = $paginate->getLastPage(); |
56
|
|
|
if ($last->getPage() > 1) { |
57
|
|
|
$html .= $this->li($last); |
58
|
|
|
} |
59
|
|
|
$html .= $this->liLabel($paginate->getNextPage(), '>'); |
60
|
|
|
$html .= '</ul>'; |
61
|
|
|
|
62
|
|
|
return $html; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param Page $p |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
|
|
private function li(Page $p) |
70
|
|
|
{ |
71
|
|
|
$label = $p->isDisabled('...', $p->getPage()); |
72
|
|
|
$href = $p->isCurrent('#', $p->getUrl()); |
73
|
|
|
$only = $p->isCurrent('<span class=\'sr-only\'>current</span>', ''); |
74
|
|
|
if ($p->isDisabled()) { |
75
|
|
|
return "<li class=\"disabled\"><a href=\"{$href}\" >{$label}</a></li>"; |
76
|
|
|
} |
77
|
|
|
if ($p->isCurrent()) { |
78
|
|
|
return "<li class=\"active\"><a href=\"{$href}\" >{$label}{$only}</a></li>\n"; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return "<li><a href=\"{$href}\" >{$label}{$only}</a></li>\n"; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
private function liLabel(Page $p, $label) |
85
|
|
|
{ |
86
|
|
|
return "<li class=\"{$p->isCurrent('disabled')}\"><a href=\"{$p->isCurrent('#', $p->getUrl())}\" >{$label}</a></li>\n"; |
87
|
|
|
} |
88
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.