1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Zemit Framework. |
5
|
|
|
* |
6
|
|
|
* (c) Zemit Team <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE.txt |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Zemit\Mvc\Controller\Traits\Query; |
13
|
|
|
|
14
|
|
|
use Phalcon\Filter\Exception; |
15
|
|
|
use Phalcon\Filter\Filter; |
16
|
|
|
use Zemit\Mvc\Controller\Traits\Abstracts\AbstractParams; |
17
|
|
|
use Zemit\Mvc\Controller\Traits\Abstracts\Query\AbstractOffset; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* This trait provides functionality to set and get an offset value for a query. |
21
|
|
|
*/ |
22
|
|
|
trait Offset |
23
|
|
|
{ |
24
|
|
|
use AbstractOffset; |
25
|
|
|
|
26
|
|
|
use AbstractParams; |
27
|
|
|
|
28
|
|
|
protected ?int $offset; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Initializes the offset value. |
32
|
|
|
* |
33
|
|
|
* Sets the offset value using the provided parameter's value, after filtering it |
34
|
|
|
* through the specified filter, or sets it to the default offset value if no |
35
|
|
|
* offset parameter is provided. |
36
|
|
|
* |
37
|
|
|
* @return void |
38
|
|
|
* @throws Exception |
39
|
|
|
* @throws \Exception |
40
|
|
|
*/ |
41
|
|
|
public function initializeOffset(): void |
42
|
|
|
{ |
43
|
|
|
$this->setOffset($this->getParam('offset', [Filter::FILTER_ABSINT], $this->defaultOffset())); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Sets the offset value for the query. |
48
|
|
|
* |
49
|
|
|
* @param int|null $offset The offset value to set for the query. Specify an integer representing the offset value or null if no offset is required. |
50
|
|
|
* @return void |
51
|
|
|
* @throws \Exception If the specified offset value is less than 0. |
52
|
|
|
*/ |
53
|
|
|
public function setOffset(?int $offset): void |
54
|
|
|
{ |
55
|
|
|
if ($this->limit < 0) { |
56
|
|
|
throw new \Exception("Query offset ({$this->limit}) must be higher than or equal to 0", 400); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$this->offset = $offset; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Returns the offset value. |
64
|
|
|
* |
65
|
|
|
* @return int|null The offset value. Returns either an integer representing the offset value or null if no offset is set. |
66
|
|
|
*/ |
67
|
|
|
public function getOffset(): ?int |
68
|
|
|
{ |
69
|
|
|
return $this->offset; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Returns the default offset value. |
74
|
|
|
* |
75
|
|
|
* @return int|null The default offset value. Returns either an integer representing the offset value or null if no offset is set. |
76
|
|
|
*/ |
77
|
|
|
public function defaultOffset(): ?int |
78
|
|
|
{ |
79
|
|
|
return 0; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|