Passed
Push — master ( 87d6a7...d49182 )
by Adam
02:27
created

Pagination::setLiClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
ccs 1
cts 1
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Pager;
3
4
class Pagination {
5
    public static $current;
6
    protected $queryString;
7
8
    protected static $page;
9
    protected static $pageURL;
10
    protected static $lastpage;
11
    protected static $totalPages;
12
    
13
    public $pagerClass = 'pagination justify-content-center';
14
    public $liClass = 'page-item';
15
    public $liActiveClass = 'active';
16
    public $aClass = 'page-link';
17
    public $aActiveClass = '';
18
    
19
    /**
20 1
     * Sets the class assigned to the UL element of the pagination object
21 1
     * @param string $class This should be the class or classes that you wish to give to the pagination object 
22 1
     * @return $this
23
     */
24 1
    public function setPaginationClass($class) {
25
        if ((!empty(trim($class)))) {
26
            $this->pagerClass = $class;
27
        }
28
        return $this;
29
    }
30
    
31 2
    /**
32 2
     * Returns the class to give to the pagination object
33
     * @return string The pagination class will be returned
34
     */
35
    public function getPaginationClass() {
36
        return $this->pagerClass;
37
    }
38
    
39
    /**
40 1
     * Sets the default li class
41 1
     * @param string $class This should be the class that you want to all to all li elements
42 1
     * @return $this
43
     */
44 1
    public function setLiClass($class) {
45
        $this->liClass = trim($class);
46
        return $this;
47
    }
48
    
49
    /**
50
     * Gets the current li class
51 2
     * @return string This should be the class to assign on li elements
52 2
     */
53
    public function getLiClass() {
54
        return $this->liClass;
55
    }
56
    
57
    /**
58
     * Sets the active class to assign on the li elements
59
     * @param string $class This should be the class to assign on active elements
60
     * @return $this
61
     */
62
    public function setLiActiveClass($class) {
63
        $this->liActiveClass = trim($class);
64
        return $this;
65
    }
66 2
67 2
    /**
68 2
     * Returns the class to assign to active li elements
69 2
     * @return string This should be the class to assign on active elements
70 1
     */
71 1
    public function getLiActiveClass() {
72 1
        return $this->liActiveClass;
73
    }
74 1
    
75 1
    
76 1
    /**
77 1
     * Sets the default class on a elements
78
     * @param string $class This should be the class to add to a elements
79 1
     * @return $this
80
     */
81 1
    public function setAClass($class) {
82
        $this->aClass = trim($class);
83
        return $this;
84
    }
85
    
86
    /**
87
     * Returns the class assigned to a elements
88
     * @return string Returns the class assigned to a elements
89
     */
90
    public function getAClass() {
91 1
        return $this->aClass;
92 1
    }
93
    
94
    /**
95
     * Sets the class to assign to active a elements
96
     * @param string $class This should be the class to add to active a elements
97
     * @return $this
98
     */
99
    public function setAActiveClass($class) {
100 1
        $this->aActiveClass = trim($class);
101 1
        return $this;
102 1
    }
103
    
104 1
    /**
105
     * Returns the class assigned to active a elements 
106
     * @return string This should be the class to add to active a elements
107
     */
108
    public function getAActiveClass() {
109
        return $this->aActiveClass;
110
    }
111
    
112
    /**
113
     * Returns paging buttons for the number of records
114 1
     * @param int $records The total number of records
115 1
     * @param string $pageURL The URL of the page you are creating the paging for
116 1
     * @param int $start The start number for the results
117
     * @param int $maxshown The number of records that are shown on each page
118
     * @param int $numpagesshown The number of pagination buttons to display
119
     * @param boolean $arrows If you want arrows to display before and after for next and previous set to true (default) else set to false
120
     * @param array $additional Any additional get values to include in the URL
121
     * @return string|false Returns the pagination menu if required else will return false
122
     */
123 1
    public function paging($records, $pageURL, $start = 0, $maxshown = 50, $numpagesshown = 11, $arrows = true, $additional = array()) {
124 1
        self::$pageURL = $pageURL;
125
        $this->queryString = $additional;
126
        if ($records > $maxshown) {
127
            self::$current = $start >= 1 ? intval($start) : 1;
128
            self::$totalPages = ceil(intval($records) / ($maxshown >= 1 ? intval($maxshown) : 1));
129
            self::$lastpage = self::$totalPages;
130
            $this->getPage($records, $maxshown, $numpagesshown);
131 1
            
132 1
            $paging = '<ul class="'.$this->getPaginationClass().'">'.$this->preLinks($arrows);
133 1
            while (self::$page <= self::$lastpage) {
134
                $paging .= $this->buildLink(self::$page, self::$page, (self::$current == self::$page));
135
                self::$page = (self::$page + 1);
136
            }
137 1
            return $paging.$this->postLinks($arrows).'</ul>';
138
        }
139
        return false;
140
    }
141
    
142
    /**
143
     * Build a link item with the given values
144
     * @param string $link This should be any additional items to be included as part of the link
145 1
     * @param mixed $page This should be the link test on the link normally set as numbers but may be anything like arrows or dots etc
146 1
     * @param boolean $current If this is the current link item set this as true so the class is added to the link item
147 1
     * @return string This will return the paging item as a string
148 1
     */
149 1
    protected function buildLink($link, $page, $current = false) {
150
        return '<li'.(!empty($this->getLiClass()) || ($current === true && !empty($this->getLiActiveClass())) ? ' class="'.trim($this->getLiClass().(($current === true && !empty($this->getLiActiveClass())) ? ' '.$this->getLiActiveClass() : '').'"') : '').'><a href="'.self::$pageURL.(!empty($this->buildQueryString($link)) ? '?'.$this->buildQueryString($link) : '').'" title="Page '.$page.'"'.(!empty($this->getAClass()) || ($current === true && !empty($this->getAActiveClass())) ? ' class="'.trim($this->getAClass().(($current === true && !empty($this->getAActiveClass())) ? ' '.$this->getAActiveClass() : '')).'"' : '').'>'.$page.'</a></li>';
151 1
    }
152
    
153
    /**
154
     * Builds the query string to add to the URL
155
     * @param mixed $page If the page variable is set to a number will add the page number to the query string else will not add any additional items
156
     * @return string The complete string will be returned to add to the link item
157
     */
158
    protected function buildQueryString($page) {
159
        $pageInfo = is_numeric($page) ? ['page' => $page] : [];
160
        return http_build_query(array_filter(array_merge($pageInfo, $this->queryString)), '', '&amp;');
161
    }
162
    
163
    /**
164
     * Gets the current page
165
     * @param int $records The total number of records
166
     * @param int $maxshown The number of records that are shown on each page
167
     * @param int $numpages The number of pagination buttons to display
168
     * return void Nothing is returned
169
     */
170
    protected function getPage($records, $maxshown, $numpages) {
171
        $show = floor($numpages / 2);
172
        if (self::$lastpage > $numpages) {
173
            self::$page = (self::$current > $show ? (self::$current - $show) : 1);
174
            if (self::$current < (self::$lastpage - $show)) {
175
                self::$lastpage = ((self::$current <= $show) ? (self::$current + ($numpages - self::$current)) : (self::$current + $show));
176
            }
177
            else { self::$page = self::$current - ($numpages - ((ceil(intval($records) / ($maxshown >= 1 ? intval($maxshown) : 1)) - self::$current)) - 1); }
178
        }
179
        else { self::$page = 1; }
180
    }
181
    
182
    /**
183
     * Returns the previous arrows as long as arrows is set to true and the page is not the first page
184
     * @param boolean $arrows If you want to display previous arrows set to true else set to false
185
     * @return string Any previous link arrows will be returned as a string
186
     */
187
    protected function preLinks($arrows = true) {
188
        $paging = '';
189
        if (self::$current != 1 && $arrows) {
190
            if (self::$current != 2) { $paging .= $this->buildLink('', '&laquo;'); }
191
            $paging .= $this->buildLink((self::$current - 1), '&lt;');
192
        }
193
        return $paging;
194
    }
195
    
196
    /**
197
     * Returns the next arrows as long as arrows is set to true and the page is not the last page
198
     * @param boolean $arrows If you want to display next arrows set to true else set to false
199
     * @return string Any next link arrows will be returned as a string
200
     */
201
    protected function postLinks($arrows = true) {
202
        $paging = '';
203
        if (self::$current != self::$totalPages && $arrows) {
204
            $paging .= $this->buildLink((self::$current + 1), '&gt;');
205
            if (self::$current != (self::$totalPages - 1)) { $paging .= $this->buildLink(self::$totalPages, '&raquo;'); }
206
        }
207
        return $paging;
208
    }
209
}
210