Pager::getCount()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/*
3
 * This file is part of the Pomm's Foundation package.
4
 *
5
 * (c) 2014 - 2017 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 - 2017 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
     * @param ResultIterator $iterator
31
     * @param int            $count        Total number of results.
32
     * @param int            $max_per_page Results per page
33
     * @param int            $page         Page index.
34
     */
35
    public function __construct(ResultIterator $iterator, $count, $max_per_page, $page)
36
    {
37
        $this->iterator     = $iterator;
38
        $this->count        = $count;
39
        $this->max_per_page = $max_per_page;
40
        $this->page         = $page;
41
    }
42
43
    /**
44
     * getIterator
45
     *
46
     * Return the Pager's iterator.
47
     *
48
     * @return ResultIterator
49
     */
50
    public function getIterator()
51
    {
52
        return $this->iterator;
53
    }
54
55
    /**
56
     * getResultCount
57
     *
58
     * Get the number of results in this page.
59
     *
60
     * @return int
61
     */
62
    public function getResultCount()
63
    {
64
        return $this->iterator->count();
65
    }
66
67
    /**
68
     * getResultMin
69
     *
70
     * Get the index of the first element of this page.
71
     *
72
     * @return int
73
     */
74
    public function getResultMin()
75
    {
76
        return min((1 + $this->max_per_page * ($this->page - 1)), $this->count);
77
    }
78
79
    /**
80
     * getResultMax
81
     *
82
     * Get the index of the last element of this page.
83
     *
84
     * @return int
85
     */
86
    public function getResultMax()
87
    {
88
        return
89
            ($this->getPage() - 1) * $this->max_per_page + $this->iterator->count();
90
    }
91
92
    /**
93
     * getLastPage
94
     *
95
     * Get the last page index.
96
     *
97
     * @return int
98
     */
99
    public function getLastPage()
100
    {
101
        return $this->count == 0 ? 1 : (int) ceil($this->count / $this->max_per_page);
102
    }
103
104
    /**
105
     * getPage
106
     *
107
     * @return int
108
     */
109
    public function getPage()
110
    {
111
        return $this->page;
112
    }
113
114
    /**
115
     * isNextPage
116
     *
117
     * True if a next page exists.
118
     *
119
     * @return Boolean
120
     */
121
    public function isNextPage()
122
    {
123
        return (bool) ($this->getPage() < $this->getLastPage());
124
    }
125
126
    /**
127
     * isPreviousPage
128
     *
129
     * True if a previous page exists.
130
     *
131
     * @return Boolean
132
     */
133
    public function isPreviousPage()
134
    {
135
        return (bool) ($this->page > 1);
136
    }
137
138
    /**
139
     * getCount
140
     *
141
     * Get the total number of results in all pages.
142
     *
143
     * @return int
144
     */
145
    public function getCount()
146
    {
147
        return $this->count;
148
    }
149
150
    /**
151
     * getMaxPerPage
152
     *
153
     * Get maximum result per page.
154
     *
155
     * @return int
156
     */
157
    public function getMaxPerPage()
158
    {
159
        return $this->max_per_page;
160
    }
161
}
162