Completed
Push — master ( b79559...f9ab88 )
by Jared
02:18
created

LimitStatement::getLimit()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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