Cancelled
Pull Request — master (#22)
by Teye
06:15 queued 01:50
created

Limit::setOffset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
declare(strict_types=1);
3
4
namespace Level23\Druid\Limits;
5
6
use Level23\Druid\OrderBy\OrderByInterface;
7
use Level23\Druid\Collections\OrderByCollection;
8
9
class Limit implements LimitInterface
10
{
11
    /**
12
     * @var int|null
13
     */
14
    protected $limit;
15
16
    /**
17
     * @var \Level23\Druid\Collections\OrderByCollection|null
18
     */
19
    protected $orderBy;
20
21 34
    /**
22
     * @var int|null
23 34
     */
24 34
    protected $offset;
25 34
26
    public function __construct(int $limit = null, OrderByCollection $orderBy = null, int $offset = null)
27
    {
28
        $this->limit   = $limit;
29
        $this->orderBy = $orderBy;
30
        $this->offset  = $offset;
31
    }
32 2
33
    /**
34
     * Return the limit in array format so that it can be used in a druid query.
35 2
     *
36 2
     * @return array
37 2
     */
38
    public function toArray(): array
39
    {
40 2
        $result = [
41
            'type'    => 'default',
42
            'columns' => ($this->orderBy ? $this->orderBy->toArray() : []),
43
        ];
44
        if ($this->limit !== null) {
45
            $result['limit'] = $this->limit;
46 2
        }
47
        if ($this->offset !== null) {
48 2
            $result['offset'] = $this->offset;
49 2
        }
50
51
        return $result;
52
    }
53
54
    /**
55
     * @param int $limit
56 16
     */
57
    public function setLimit(int $limit): void
58 16
    {
59 16
        $this->limit = $limit;
60
    }
61 16
62 16
    /**
63
     * Add an order by field to the collection.
64
     *
65
     * @param \Level23\Druid\OrderBy\OrderByInterface $orderBy
66
     */
67
    public function addOrderBy(OrderByInterface $orderBy)
68
    {
69 26
        if ($this->orderBy === null) {
70
            $this->orderBy = new OrderByCollection();
71 26
        }
72
        $this->orderBy->add($orderBy);
73
    }
74
75
    /**
76
     * Get the limit which is currently configured.
77
     *
78
     * @return int|null
79 16
     */
80
    public function getLimit(): ?int
81 16
    {
82 5
        return $this->limit;
83
    }
84
85 11
    /**
86
     * Return the fields which are used to sort the result by.
87
     *
88
     * @return \Level23\Druid\Collections\OrderByCollection
89
     */
90
    public function getOrderByCollection(): OrderByCollection
91
    {
92
        if (is_null($this->orderBy)) {
93
            return new OrderByCollection();
94
        }
95
96
        return $this->orderBy;
97
    }
98
99
    /**
100
     * @param int $offset
101
     */
102
    public function setOffset(int $offset): void
103
    {
104
        $this->offset = $offset;
105
    }
106
107
    /**
108
     * @return int|null
109
     */
110
    public function getOffset(): ?int
111
    {
112
        return $this->offset;
113
    }
114
}