Limit   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 61
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setOffset() 0 4 1
A setLimit() 0 4 1
A limit() 0 5 1
A prepareLimit() 0 3 2
1
<?php
2
3
/**
4
 * Bluz Framework Component
5
 *
6
 * @copyright Bluz PHP Team
7
 * @link      https://github.com/bluzphp/framework
8
 */
9
10
declare(strict_types=1);
11
12
namespace Bluz\Db\Query\Traits;
13
14
/**
15
 * Limit Trait
16
 *
17
 * Required for:
18
 *  - Select Builder
19
 *  - Update Builder
20
 *  - Delete Builder
21
 *
22
 * @package  Bluz\Db\Query\Traits
23
 * @author   Anton Shevchuk
24
 */
25
trait Limit
26
{
27
    /**
28
     * @var integer the maximum number of results to retrieve/update/delete
29
     */
30
    protected $limit;
31
32
    /**
33
     * @var integer the index of the first result to retrieve.
34
     */
35
    protected $offset = 0;
36
37
    /**
38
     * Sets the maximum number of results to retrieve/update/delete
39
     *
40
     * @param  integer $limit  The maximum number of results to retrieve
41
     * @param  integer $offset The offset of the query
42
     *
43
     * @return $this
44
     */
45 2
    public function limit(int $limit, int $offset = 0): self
46
    {
47 2
        $this->setLimit($limit);
48 2
        $this->setOffset($offset);
49 2
        return $this;
50
    }
51
52
    /**
53
     * Setup limit for the query
54
     *
55
     * @param  integer $limit
56
     *
57
     * @return $this
58
     */
59 5
    public function setLimit(int $limit): self
60
    {
61 5
        $this->limit = $limit;
62 5
        return $this;
63
    }
64
65
    /**
66
     * Setup offset for the query
67
     *
68
     * @param  integer $offset
69
     *
70
     * @return $this
71
     */
72 4
    public function setOffset(int $offset): self
73
    {
74 4
        $this->offset = $offset;
75 4
        return $this;
76
    }
77
78
    /**
79
     * Prepare string to apply limit inside SQL query
80
     *
81
     * @return string
82
     */
83 9
    protected function prepareLimit(): string
84
    {
85 9
        return $this->limit ? ' LIMIT ' . $this->limit . ' OFFSET ' . $this->offset : '';
86
    }
87
}
88