summaryrefslogtreecommitdiffstats
path: root/vendor/ipl/sql/src/LimitOffset.php
blob: 99c30a24e532c9dc9556b5adddafbc6c432635bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php

namespace ipl\Sql;

/**
 * Implementation for the {@link LimitOffsetInterface} to allow pagination via {@link limit()} and {@link offset()}
 */
trait LimitOffset
{
    /**
     * The maximum number of how many items to return
     *
     * If unset or lower than 0, no limit will be applied.
     *
     * @var int|null
     */
    protected $limit;

    /**
     * Offset from where to start the result set
     *
     * If unset or lower than 0, the result set will start from the beginning.
     *
     * @var int|null
     */
    protected $offset;

    public function hasLimit()
    {
        return $this->limit !== null;
    }

    public function getLimit()
    {
        return $this->limit;
    }

    public function limit($limit)
    {
        if ($limit !== null) {
            $limit = (int) $limit;
            if ($limit < 0) {
                $limit = null;
            }
        }

        $this->limit = $limit;

        return $this;
    }

    public function resetLimit()
    {
        $this->limit = null;

        return $this;
    }

    public function hasOffset()
    {
        return $this->offset !== null;
    }

    public function getOffset()
    {
        return $this->offset;
    }

    public function offset($offset)
    {
        if ($offset !== null) {
            $offset = (int) $offset;
            if ($offset <= 0) {
                $offset = null;
            }
        }

        $this->offset = $offset;

        return $this;
    }

    public function resetOffset()
    {
        $this->offset = null;

        return $this;
    }
}