Completed
Push — master ( 6d16be...447129 )
by Adam
01:55
created

Pagination::postLinks()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 5
nc 3
nop 1
dl 0
loc 7
ccs 6
cts 6
cp 1
crap 4
rs 9.2
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
    
12
    public $pagerClass = 'pagination';
13
    public $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 1
    public function setPaginationClass($class){
21 1
        if((!empty(trim($class)))){
22 1
            $this->pagerClass = $class;
23
        }
24 1
        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 2
    public function getPaginationClass(){
32 2
        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 1
    public function setActiveClass($class){
41 1
        if((!empty(trim($class)))){
42 1
            $this->liActiveClass = $class;
43
        }
44 1
        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 2
    public function getActiveClass(){
52 2
        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 int $maxshown The number of records that are shown on each page
61
     * @param int $numpagesshown The number of pagination buttons to display
62
     * @param boolean $arrows If you want arrows to display before and after for next and previous set to true (default) else set to false
63
     * @param array $additional Any additional get values to include in the URL
64
     * @return string Returns the pagination menu
65
     */
66 2
    public function paging($records, $pageURL, $start = 0, $maxshown = 50, $numpagesshown = 11, $arrows = true, $additional = array()) {
67 2
        self::$pageURL = $pageURL;
68 2
        $this->queryString = $additional;
69 2
        if ($records > $maxshown) {
70 1
            self::$current = $start >= 1 ? intval($start) : 1;
71 1
            self::$lastpage = ceil($records / $maxshown);
72 1
            $this->getPage($records, $maxshown, $numpagesshown);
73
            
74 1
            $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 1
            while (self::$page <= self::$lastpage) {
76 1
                $paging .= $this->buildLink(self::$page, self::$page, (self::$current == self::$page));
77 1
                self::$page = (self::$page + 1);
78
            }
79 1
            return $paging.$this->postLinks($arrows).'</ul>';
80
        }
81 1
        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 1
    protected function buildLink($link, $page, $current = false) {
92 1
        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 1
    protected function buildQueryString($page) {
101 1
        if (is_numeric($page)) {
102 1
            $this->queryString['page'] = $page;
103
        }
104 1
        return http_build_query(array_filter($this->queryString), '', '&amp;');
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 1
    protected function getPage($records, $maxshown, $numpages) {
115 1
        $show = floor($numpages / 2);
116 1
        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 1
        else { self::$page = 1; }
124 1
    }
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 1
    protected function preLinks($arrows = true) {
132 1
        $paging = '';
133 1
        if (self::$current != 1 && $arrows) {
134
            if (self::$current != 2) { $paging .= $this->buildLink('', '&laquo;'); }
135
            $paging .= $this->buildLink((self::$current - 1), '&lt;');
136
        }
137 1
        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 1
    protected function postLinks($arrows = true) {
146 1
        $paging = '';
147 1
        if (self::$current != self::$lastpage && $arrows) {
148 1
            $paging .= $this->buildLink((self::$current + 1), '&gt;');
149 1
            if (self::$current != (self::$lastpage - 1)) { $paging .= $this->buildLink(self::$lastpage, '&raquo;'); }
150
        }
151 1
        return $paging;
152
    }
153
}
154