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

LimitOffsetTrait   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 60
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A offset() 0 5 1
A getOffset() 0 4 1
A buildLimit() 0 19 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 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 15
        }
66
67 155
        if (!empty($this->offset)) {
68 10
            $clause .= " OFFSET {$this->offset}";
69 10
        }
70
71 155
        if (!empty($clause)) {
72 15
            $clause = PHP_EOL . trim($clause);
73 15
        }
74
75 155
        return $clause;
76
    }
77
}
78