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