Completed
Pull Request — 3.x (#120)
by
unknown
01:47
created

LimitOffsetTrait::getOffset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 *
4
 * This file is part of Aura for PHP.
5
 *
6
 * @license http://opensource.org/licenses/bsd-license.php BSD
7
 *
8
 */
9
namespace Aura\SqlQuery\Common;
10
11
/**
12
 *
13
 * An interface for LIMIT...OFFSET clauses.
14
 *
15
 * @package Aura.SqlQuery
16
 *
17
 */
18
trait LimitOffsetTrait
19
{
20
    use LimitTrait;
21
22
    private $offset = 0;
23
24
    /**
25
     *
26
     * Sets a limit offset on the query.
27
     *
28
     * @param int $offset Start returning after this many rows.
29
     *
30
     * @return $this
31
     *
32
     */
33 39
    public function offset($offset)
34
    {
35 39
        $this->offset = (int) $offset;
36 39
        return $this;
37
    }
38
39
    /**
40
     *
41
     * Returns the OFFSET value.
42
     *
43
     * @return int
44
     *
45
     */
46 46
    public function getOffset()
47
    {
48 46
        return $this->offset;
49
    }
50
51
    /**
52
     *
53
     * Builds the `LIMIT ... OFFSET` clause of the statement.
54
     *
55
     * @return string
56
     *
57
     */
58 155
    protected function buildLimit()
59
    {
60 155
        $clause = '';
61
62 155
        $limit = $this->getLimit();
63 155
        if (!empty($limit)) {
64 15
            $clause .= "LIMIT {$limit}";
65
        }
66
67 155
        if (!empty($this->offset)) {
68 10
            $clause .= " OFFSET {$this->offset}";
69
        }
70
71 155
        if (!empty($clause)) {
72 15
            $clause = PHP_EOL . trim($clause);
73
        }
74
75 155
        return $clause;
76
    }
77
}
78