Completed
Push — master ( 1c956a...362a13 )
by WEBEWEB
01:17
created

PaginateHelper::getPagesCount()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 4
nc 3
nop 2
1
<?php
2
3
/*
4
 * This file is part of the core-library package.
5
 *
6
 * (c) 2018 WEBEWEB
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace WBW\Library\Core\Database\Helper;
13
14
/**
15
 * Paginate helper.
16
 *
17
 * @author webeweb <https://github.com/webeweb/>
18
 * @package WBW\Library\Core\Database\Helper
19
 */
20
class PaginateHelper {
21
22
    /**
23
     * Get a page offset and limit.
24
     *
25
     * @param int $pageNumber The page number.
26
     * @param int $divider The divider.
27
     * @param int $total The total.
28
     * @return int[] Returns the page offset and limit in case of success, -1 otherwise.
29
     */
30
    public static function getPageOffsetAndLimit($pageNumber, $divider, $total = -1) {
31
32
        if ($pageNumber < 0 || $divider < 0) {
33
            return -1;
34
        }
35
36
        $offset = $pageNumber * $divider;
37
        $limit  = $divider;
38
39
        if (0 <= $total && ($total < $offset || $total < ($offset + $limit))) {
40
            $offset = (static::getPagesCount($total, $divider) - 1) * $divider;
41
            $limit  = $total - $offset;
42
        }
43
44
        return [$offset, $limit];
45
    }
46
47
    /**
48
     * Get a pages count.
49
     *
50
     * @param int $linesNumber The lines number.
51
     * @param int $divider The divider.
52
     * @return int Returns the pages count in case of success, -1 otherwise.
53
     */
54
    public static function getPagesCount($linesNumber, $divider) {
55
56
        if ($linesNumber < 0 || $divider < 0) {
57
            return -1;
58
        }
59
60
        $pagesCount = intval($linesNumber / $divider);
61
        if (0 < ($linesNumber % $divider)) {
62
            ++$pagesCount;
63
        }
64
65
        return $pagesCount;
66
    }
67
}
68