DatabasePagination   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 54
rs 10
c 0
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A limit() 0 3 1
A hasLimit() 0 3 1
A offset() 0 10 2
A sql() 0 9 3
1
<?php
2
3
namespace Charcoal\Source\Database;
4
5
use UnexpectedValueException;
6
7
// From 'charcoal-core'
8
use Charcoal\Source\Database\DatabaseExpressionInterface;
9
use Charcoal\Source\Pagination;
10
11
/**
12
 * SQL Pagination Clause
13
 */
14
class DatabasePagination extends Pagination implements
15
    DatabaseExpressionInterface
16
{
17
    /**
18
     * Converts the pagination into a SQL expression for the LIMIT clause.
19
     *
20
     * @return string A SQL string fragment.
21
     */
22
    public function sql()
23
    {
24
        if ($this->active() && $this->hasLimit()) {
25
            $limit  = $this->limit();
26
            $offset = $this->offset();
27
            return 'LIMIT '.$offset.', '.$limit;
28
        }
29
30
        return '';
31
    }
32
33
    /**
34
     * Determine if the expression has a number per page.
35
     *
36
     * @return boolean
37
     */
38
    public function hasLimit()
39
    {
40
        return ($this->limit() > 0);
41
    }
42
43
    /**
44
     * Alias of {@see self::numPerPage()}
45
     *
46
     * @return integer
47
     */
48
    public function limit()
49
    {
50
        return $this->numPerPage();
51
    }
52
53
    /**
54
     * Retrieve the offset from the page number and count.
55
     *
56
     * @return integer
57
     */
58
    public function offset()
59
    {
60
        $page   = $this->page();
61
        $limit  = $this->numPerPage();
62
        $offset = (($page - 1) * $limit);
63
        if (PHP_INT_MAX <= $offset) {
64
            $offset = PHP_INT_MAX;
65
        }
66
67
        return max(0, $offset);
68
    }
69
}
70