Completed
Pull Request — 2.0 (#75)
by Julien
04:02
created

Pager::isNextPage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/*
3
 * This file is part of the Pomm's Foundation package.
4
 *
5
 * (c) 2014 - 2015 Grégoire HUBERT <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace PommProject\Foundation;
11
12
/**
13
 * Pager
14
 *
15
 * @package   Foundation
16
 * @copyright 2014 - 2015 Grégoire HUBERT
17
 * @author    Grégoire HUBERT <[email protected]>
18
 * @license   MIT/X11 {@link http://opensource.org/licenses/mit-license.php}
19
 */
20
class Pager
21
{
22
    protected $iterator;
23
    protected $count;
24
    protected $max_per_page;
25
    protected $page;
26
27
    /**
28
     * __construct
29
     *
30
     * @access public
31
     * @param ResultIterator $iterator
32
     * @param int            $count        Total number of results.
33
     * @param int            $max_per_page Results per page
34
     * @param int            $page         Page index.
35
     */
36
    public function __construct(ResultIterator $iterator, $count, $max_per_page, $page)
37
    {
38
        $this->iterator     = $iterator;
39
        $this->count        = $count;
40
        $this->max_per_page = $max_per_page;
41
        $this->page         = $page;
42
    }
43
44
    /**
45
     * getIterator
46
     *
47
     * Return the Pager's iterator.
48
     *
49
     * @access public
50
     * @return ResultIterator
51
     */
52
    public function getIterator()
53
    {
54
        return $this->iterator;
55
    }
56
57
    /**
58
     * getResultCount
59
     *
60
     * Get the number of results in this page.
61
     *
62
     * @access public
63
     * @return int
64
     */
65
    public function getResultCount()
66
    {
67
        return $this->count;
68
    }
69
70
    /**
71
     * getResultMin
72
     *
73
     * Get the index of the first element of this page.
74
     *
75
     * @access public
76
     * @return int
77
     */
78
    public function getResultMin()
79
    {
80
        return min((1 + $this->max_per_page * ($this->page - 1)), $this->count);
81
    }
82
83
    /**
84
     * getResultMax
85
     *
86
     * Get the index of the last element of this page.
87
     *
88
     * @access public
89
     * @return int
90
     */
91
    public function getResultMax()
92
    {
93
        return max(($this->getResultMin() + $this->iterator->count() - 1), 0);
94
    }
95
96
    /**
97
     * getLastPage
98
     *
99
     * Get the last page index.
100
     *
101
     * @access public
102
     * @return int
103
     */
104
    public function getLastPage()
105
    {
106
        return $this->count == 0 ? 1 : ceil($this->count / $this->max_per_page);
107
    }
108
109
    /**
110
     * getPage
111
     *
112
     * @access public
113
     * @return int
114
     */
115
    public function getPage()
116
    {
117
        return $this->page;
118
    }
119
120
    /**
121
     * isNextPage
122
     *
123
     * True if a next page exists.
124
     *
125
     * @access public
126
     * @return Boolean
127
     */
128
    public function isNextPage()
129
    {
130
        return (bool) ($this->getPage() < $this->getLastPage());
131
    }
132
133
    /**
134
     * isPreviousPage
135
     *
136
     * True if a previous page exists.
137
     *
138
     * @access public
139
     * @return Boolean
140
     */
141
    public function isPreviousPage()
142
    {
143
        return (bool) ($this->page > 1);
144
    }
145
146
    /**
147
     * getCount
148
     *
149
     * Get the total number of results in all pages.
150
     *
151
     * @access public
152
     * @return int
153
     */
154
    public function getCount()
155
    {
156
        return $this->count;
157
    }
158
159
    /**
160
     * getMaxPerPage
161
     *
162
     * Get maximum result per page.
163
     *
164
     * @access public
165
     * @return int
166
     */
167
    public function getMaxPerPage()
168
    {
169
        return $this->max_per_page;
170
    }
171
}
172