Completed
Push — master ( 25162d...6615a4 )
by Adam
01:59
created

Pagination::getPaginationClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 2
rs 10
1
<?php
2
namespace Pager;
3
4
class Pagination {
5
    public static $current;
6
    protected static $queryString;
7
8
    protected static $page;
9
    protected static $pageURL;
10
    protected static $lastpage;
11
    
12
    public static $pagerClass = 'pagination';
13
    public static $liActiveClass= 'active';
14
    
15
    /**
16
     * Sets the class assigned to the UL element of the pagination object
17
     * @param string $class This should be the class or classes that you wish to give to the pagination object 
18
     * @return $this
19
     */
20
    public function setPaginationClass($class){
21
        if((!empty(trim($class)))){
22
            $this->pagerClass = $class;
23
        }
24
        return $this;
25
    }
26
    
27
    /**
28
     * Returns the class to give to the pagination object
29
     * @return type
0 ignored issues
show
Bug introduced by
The type Pager\type was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
30
     */
31
    public function getPaginationClass(){
32
        return $this->pagerClass;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->pagerClass returns the type string which is incompatible with the documented return type Pager\type.
Loading history...
33
    }
34
    
35
    /**
36
     * Sets the active class to assign on the li elements
37
     * @param string $class This should be the class to assign on active elements
38
     * @return $this
39
     */
40
    public function setActiveClass($class){
41
        if((!empty(trim($class)))){
42
            $this->liActiveClass = $class;
43
        }
44
        return $this;
45
    }
46
47
    /**
48
     * Returns the class to assign to active li elements
49
     * @return string $class This should be the class to assign on active elements
50
     */
51
    public function getActiveClass(){
52
        return $this->liActiveClass;
53
    }
54
    
55
    /**
56
     * Returns paging buttons for the number of records
57
     * @param int $records The total number of records
58
     * @param string $pageURL The URL of the page you are creating the paging for
59
     * @param int $start The start number for the results
60
     * @param array $additional Any additional get values to include in the URL
61
     * @param int $maxshown The number of records that are shown on each page
62
     * @param int $numpagesshown The number of pagination buttons to display
63
     * @param boolean $arrows If you want arrows to display before and after for next and previous set to true (default) else set to false
64
     * @return string Returns the pagination menu
65
     */
66
    public function paging($records, $pageURL, $start = 0, $additional = array(), $maxshown = 50, $numpagesshown = 11, $arrows = true) {
67
        self::$pageURL = $pageURL;
68
        self::$queryString = $additional;
69
        if ($records > $maxshown) {
70
            self::$current = $start >= 1 ? intval($start) : 1;
71
            self::$lastpage = ceil($records / $maxshown);
72
            $this->getPage($records, $maxshown, $numpagesshown);
73
            
74
            $paging = '<ul class="'.self::getPaginationClass().'">'.$this->preLinks($arrows);
0 ignored issues
show
Bug Best Practice introduced by
The method Pager\Pagination::getPaginationClass() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

74
            $paging = '<ul class="'.self::/** @scrutinizer ignore-call */ getPaginationClass().'">'.$this->preLinks($arrows);
Loading history...
75
            while (self::$page <= self::$lastpage) {
76
                $paging .= $this->buildLink(self::$page, self::$page, (self::$current == self::$page));
77
                self::$page = (self::$page + 1);
78
            }
79
            return $paging.$this->postLinks($arrows).'</ul>';
80
        }
81
        return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type string.
Loading history...
82
    }
83
    
84
    /**
85
     * Build a link item with the given values
86
     * @param string $link This should be any additional items to be included as part of the link
87
     * @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
88
     * @param boolean $current If this is the current link item set this as true so the class is added to the link item
89
     * @return string This will return the paging item as a string
90
     */
91
    protected function buildLink($link, $page, $current = false) {
92
        return '<li'.(($current === true && !empty(self::getActiveClass())) ? ' class="'.self::getActiveClass().'"' : '').'><a href="'.self::$pageURL.'?'.$this->buildQueryString($link).'" title="Page '.$page.'">'.$page.'</a></li>';
0 ignored issues
show
Bug Best Practice introduced by
The method Pager\Pagination::getActiveClass() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

92
        return '<li'.(($current === true && !empty(self::/** @scrutinizer ignore-call */ getActiveClass())) ? ' class="'.self::getActiveClass().'"' : '').'><a href="'.self::$pageURL.'?'.$this->buildQueryString($link).'" title="Page '.$page.'">'.$page.'</a></li>';
Loading history...
93
    }
94
    
95
    /**
96
     * Builds the query string to add to the URL
97
     * @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
98
     * @return string The complete string will be returned to add to the link item
99
     */
100
    protected function buildQueryString($page) {
101
        if (is_numeric($page)) {
102
            $this->queryString['page'] = $page;
103
        }
104
        return http_build_query(array_filter($this->queryString));
105
    }
106
    
107
    /**
108
     * Gets the current page
109
     * @param int $records The total number of records
110
     * @param int $maxshown The number of records that are shown on each page
111
     * @param int $numpages The number of pagination buttons to display
112
     * return void Nothing is returned
113
     */
114
    protected function getPage($records, $maxshown, $numpages) {
115
        $show = floor($numpages / 2);
116
        if (self::$lastpage > $numpages) {
117
            self::$page = (self::$current > $show ? (self::$current - $show) : 1);
118
            if (self::$current < (self::$lastpage - $show)) {
119
                self::$lastpage = ((self::$current <= $show) ? (self::$current + ($numpages - self::$current)) : (self::$current + $show));
120
            }
121
            else { self::$page = self::$current - ($numpages - ((ceil($records / $maxshown) - self::$current)) - 1); }
122
        }
123
        else { self::$page = 1; }
124
    }
125
    
126
    /**
127
     * Returns the previous arrows as long as arrows is set to true and the page is not the first page
128
     * @param boolean $arrows If you want to display previous arrows set to true else set to false
129
     * @return string Any previous link arrows will be returned as a string
130
     */
131
    protected function preLinks($arrows = true) {
132
        $paging = '';
133
        if (self::$current != 1 && $arrows) {
134
            if (self::$current != 2) { $paging .= $this->buildLink('', '&laquo;'); }
135
            $paging .= $this->buildLink((self::$current - 1), '&lt;');
136
        }
137
        return $paging;
138
    }
139
    
140
    /**
141
     * Returns the next arrows as long as arrows is set to true and the page is not the last page
142
     * @param boolean $arrows If you want to display next arrows set to true else set to false
143
     * @return string Any next link arrows will be returned as a string
144
     */
145
    protected function postLinks($arrows = true) {
146
        $paging = '';
147
        if (self::$current != self::$lastpage && $arrows) {
148
            $paging .= $this->buildLink((self::$current + 1), '&gt;');
149
            if (self::$current != (self::$lastpage - 1)) { $paging .= $this->buildLink(self::$lastpage, '&raquo;'); }
150
        }
151
        return $paging;
152
    }
153
}
154