Completed
Push — master ( 1e376d...b0060a )
by Changwan
05: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 83.33%

Importance

Changes 0
Metric Value
dl 0
loc 73
ccs 25
cts 30
cp 0.8333
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 5
    public function take($take = null)
19
    {
20 5
        $this->take = $take;
21 5
        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 5
    public function toSql()
50
    {
51 5
        if (!isset($this->take) && !isset($this->offset)) {
52
            return '';
53
        }
54 5
        $sqlParts = [];
55 5
        if (isset($this->take)) {
56 5
            $sqlParts[] = "LIMIT ?";
57 5
        }
58 5
        if (isset($this->offset)) {
59 5
            $sqlParts[] = "OFFSET ?";
60 5
        }
61 5
        return implode(" ", $sqlParts);
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67 5
    public function getBindings()
68
    {
69 5
        $bindings = [];
70 5
        if (isset($this->take)) {
71 5
            $bindings[] = $this->take;
72 5
        }
73 5
        if (isset($this->offset)) {
74 5
            $bindings[] = $this->offset;
75 5
        }
76 5
        return $bindings;
77
    }
78
}
79