Completed
Push — master ( 5df011...747e00 )
by Changwan
03:50
created

LimitExpression   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 80.77%

Importance

Changes 0
Metric Value
dl 0
loc 73
ccs 21
cts 26
cp 0.8077
rs 10
c 0
b 0
f 0
wmc 11
lcom 1
cbo 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A take() 0 5 1
A offset() 0 5 1
A limit() 0 6 1
B toSql() 0 14 5
A getBindings() 0 11 3
1
<?php
2
namespace Wandu\Database\Query\Expression;
3
4
use Wandu\Database\Contracts\ExpressionInterface;
5
6
class LimitExpression implements ExpressionInterface
7
{
8
    /** @var int */
9
    protected $take;
10
11
    /** @var int */
12
    protected $offset;
13
14
    /**
15
     * @param int $take
16
     * @return static
17
     */
18 7
    public function take($take = null)
19
    {
20 7
        $this->take = $take;
21 7
        return $this;
22
    }
23
24
    /**
25
     * @param int $offset
26
     * @return static
27
     */
28 5
    public function offset($offset = null)
29
    {
30 5
        $this->offset = $offset;
31 5
        return $this;
32
    }
33
34
    /**
35
     * @param int $offset
36
     * @param int $take
37
     * @return static
38
     */
39
    public function limit($offset, $take)
40
    {
41
        $this->offset = $offset;
42
        $this->take = $take;
43
        return $this;
44
    }
45
    
46
    /**
47
     * {@inheritdoc}
48
     */
49 9
    public function toSql()
50
    {
51 9
        if (!isset($this->take) && !isset($this->offset)) {
52
            return '';
53
        }
54 9
        $sqlParts = [];
55 9
        if (isset($this->take)) {
56 9
            $sqlParts[] = "LIMIT ?";
57
        }
58 9
        if (isset($this->offset)) {
59 5
            $sqlParts[] = "OFFSET ?";
60
        }
61 9
        return implode(" ", $sqlParts);
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67 9
    public function getBindings()
68
    {
69 9
        $bindings = [];
70 9
        if (isset($this->take)) {
71 9
            $bindings[] = $this->take;
72
        }
73 9
        if (isset($this->offset)) {
74 5
            $bindings[] = $this->offset;
75
        }
76 9
        return $bindings;
77
    }
78
}
79