Passed
Push — master ( 3f32b7...aef1c7 )
by Rougin
03:20
created

ReturnEntities::limit()   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 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Rougin\Windstorm\Mutators;
4
5
use Rougin\Windstorm\QueryInterface;
6
use Rougin\Windstorm\MutatorInterface;
7
8
/**
9
 * Return Entities Mutator
10
 *
11
 * @package Windstorm
12
 * @author  Rougin Gutib <[email protected]>
13
 */
14
class ReturnEntities implements MutatorInterface
15
{
16
    /**
17
     * @var callable|null
18
     */
19
    protected $callback = null;
20
21
    /**
22
     * @var string[]
23
     */
24
    protected $fields = array('*');
25
26
    /**
27
     * @var integer
28
     */
29
    protected $offset = 0;
30
31
    /**
32
     * @var integer
33
     */
34
    protected $limit = 10;
35
36
    /**
37
     * @var string
38
     */
39
    protected $table = '';
40
41
    /**
42
     * Initializes the mutator instance.
43
     *
44
     * @param integer|null $limit
45
     * @param integer      $offset
46
     */
47 21
    public function __construct($limit = 10, $offset = 0)
48
    {
49 21
        $this->limit = $limit;
50
51 21
        $this->offset = $offset;
52 21
    }
53
54
    /**
55
     * Sets a where callback to the query instance.
56
     *
57
     * @param  callable $callback
58
     * @return self
59
     */
60 3
    public function callback($callback)
61
    {
62 3
        $this->callback = $callback;
63
64 3
        return $this;
65
    }
66
67
    /**
68
     * Returns the limit per result.
69
     *
70
     * @return integer
71
     */
72
    public function limit()
73
    {
74
        return $this->limit;
75
    }
76
77
    /**
78
     * Returns the offset of the current result.
79
     *
80
     * @return integer
81
     */
82
    public function offset()
83
    {
84
        return $this->offset;
85
    }
86
87
    /**
88
     * Mutates the specified query instance.
89
     *
90
     * @param \Rougin\Windstorm\QueryInterface $query
91
     */
92 21
    public function set(QueryInterface $query)
93
    {
94 21
        $query = $query->select($this->fields)->from($this->table);
95
96 21
        if ($this->callback !== null)
97 14
        {
98 3
            $callback = $this->callback;
99
100 3
            $query = $callback($query);
101 2
        }
102
103 21
        if ($this->limit === null)
104 14
        {
105 3
            return $query;
106
        }
107
108 18
        return $query->limit($this->limit, (integer) $this->offset);
109
    }
110
}
111