Completed
Push — master ( 44b4d6...c54f73 )
by WEBEWEB
06:15
created

PaginateHelper   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 10
lcom 0
cbo 0
dl 0
loc 42
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getPageOffsetAndLimit() 0 12 6
A getPagesCount() 0 10 4
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\Helper\Database;
13
14
/**
15
 * Paginate helper.
16
 *
17
 * @author webeweb <https://github.com/webeweb/>
18
 * @package WBW\Library\Core\Helper\Database
19
 */
20
class PaginateHelper {
21
22
    /**
23
     * Get a page offset and limit.
24
     *
25
     * @param integer $pageNumber The page number.
26
     * @param integer $divider The divider.
27
     * @param integer $total The total.
28
     * @return integer[] Returns the page offset and limit in case of success, -1 otherwise.
29
     */
30
    public static function getPageOffsetAndLimit($pageNumber, $divider, $total = -1) {
31
        if ($pageNumber < 0 || $divider < 0) {
32
            return -1;
33
        }
34
        $offset = $pageNumber * $divider;
35
        $limit  = $divider;
36
        if (0 <= $total && ($total < $offset || $total < ($offset + $limit))) {
37
            $offset = (self::getPagesCount($total, $divider) - 1) * $divider;
38
            $limit  = $total - $offset;
39
        }
40
        return [$offset, $limit];
41
    }
42
43
    /**
44
     * Get a pages count.
45
     *
46
     * @param integer $linesNumber The lines number.
47
     * @param integer $divider The divider.
48
     * @return integer Returns the pages count in case of success, -1 otherwise.
49
     */
50
    public static function getPagesCount($linesNumber, $divider) {
51
        if ($linesNumber < 0 || $divider < 0) {
52
            return -1;
53
        }
54
        $pagesCount = intval($linesNumber / $divider);
55
        if (0 < ($linesNumber % $divider)) {
56
            ++$pagesCount;
57
        }
58
        return $pagesCount;
59
    }
60
61
}
62