LimitStatement::getLimit()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
/**
4
 * @author Jared King <[email protected]>
5
 *
6
 * @see http://jaredtking.com
7
 *
8
 * @copyright 2015 Jared King
9
 * @license MIT
10
 */
11
12
namespace JAQB\Statement;
13
14
class LimitStatement extends Statement
15
{
16
    /**
17
     * @var int
18
     */
19
    protected $limit;
20
21
    /**
22
     * @var int
23
     */
24
    protected $offset = 0;
25
26
    /**
27
     * @var int
28
     */
29
    protected $max;
30
31
    /**
32
     * @param int $max maximum # the limit can be set to, 0=infinity
33
     */
34
    public function __construct($max = 0)
35
    {
36
        $this->max = $max;
37
    }
38
39
    /**
40
     * Sets the limit and start offset.
41
     *
42
     * @param int $limit
43
     * @param int $offset
44
     *
45
     * @return self
46
     */
47
    public function setLimit($limit, $offset = 0)
48
    {
49
        if (is_numeric($limit) && is_numeric($offset)) {
50
            $this->limit = (int) $limit;
51
            $this->offset = (int) $offset;
52
        }
53
54
        return $this;
55
    }
56
57
    /**
58
     * Gets the start offset.
59
     *
60
     * @return int
61
     */
62
    public function getStart()
63
    {
64
        return $this->offset;
65
    }
66
67
    /**
68
     * Gets the limit.
69
     *
70
     * @return int
71
     */
72
    public function getLimit()
73
    {
74
        if ($this->max > 0) {
75
            return min($this->max, $this->limit);
76
        }
77
78
        return $this->limit;
79
    }
80
81
    public function build()
82
    {
83
        if (!$this->limit) {
84
            return '';
85
        }
86
87
        if (!$this->offset) {
88
            return 'LIMIT '.$this->limit;
89
        }
90
91
        return 'LIMIT '.(string) $this->offset.','.$this->limit;
92
    }
93
}
94