Completed
Push — 3.x ( 9be43c...53c3d5 )
by Paul
03:10 queued 01:24
created

LimitOffsetTrait::buildLimit()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 13
cts 13
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 10
nc 8
nop 0
crap 4
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 44
    public function offset($offset)
34
    {
35 44
        $this->offset = (int) $offset;
36 44
        return $this;
37
    }
38
39
    /**
40
     *
41
     * Returns the OFFSET value.
42
     *
43
     * @return int
44
     *
45
     */
46 47
    public function getOffset()
47
    {
48 47
        return $this->offset;
49
    }
50
51
    /**
52
     *
53
     * Builds the `LIMIT ... OFFSET` clause of the statement.
54
     *
55
     * @return string
56
     *
57
     */
58 159
    protected function buildLimit()
59
    {
60 159
        $clause = '';
61
62 159
        $limit = $this->getLimit();
63 159
        if (!empty($limit)) {
64 15
            $clause .= "LIMIT {$limit}";
65 15
        }
66
67 159
        if (!empty($this->offset)) {
68 10
            $clause .= " OFFSET {$this->offset}";
69 10
        }
70
71 159
        if (!empty($clause)) {
72 15
            $clause = PHP_EOL . trim($clause);
73 15
        }
74
75 159
        return $clause;
76
    }
77
}
78