Passed
Push — master ( 09a12d...053d37 )
by Rougin
03:08
created

ReturnEntities::query()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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 string
18
     */
19
    protected $alias = '';
20
21
    /**
22
     * @var string[]
23
     */
24
    protected $fields = array('*');
25
26
    /**
27
     * @var integer|null
28
     */
29
    protected $limit = 10;
30
31
    /**
32
     * @var integer|null
33
     */
34
    protected $offset = 0;
35
36
    /**
37
     * @var string
38
     */
39
    protected $table = '';
40
41
    /**
42
     * @var array
43
     */
44
    protected $wheres = array();
45
46
    /**
47 21
     * Initializes the mutator instance.
48
     *
49 21
     * @param integer|null $limit
50
     * @param integer|null $offset
51 21
     */
52 21
    public function __construct($limit = null, $offset = null)
53
    {
54
        $this->limit = $limit;
55
56
        $this->offset = $offset;
57
    }
58
59
    /**
60 3
     * Returns an array of specified fields.
61
     *
62 3
     * @return array
63
     */
64 3
    public function fields()
65
    {
66
        return $this->fields;
67
    }
68
69
    /**
70
     * Returns the limit per result.
71
     *
72
     * @return integer
73
     */
74
    public function limit()
75
    {
76
        return $this->limit;
77
    }
78
79
    /**
80
     * Returns the offset of the current result.
81
     *
82
     * @return integer
83
     */
84
    public function offset()
85
    {
86
        return $this->offset;
87
    }
88
89
    /**
90
     * Mutates the specified query instance.
91
     *
92 21
     * @param \Rougin\Windstorm\QueryInterface $query
93
     */
94 21
    public function set(QueryInterface $query)
95
    {
96 21
        $operations = array('<>', '>=', '<=', '>', '<', '=');
97 14
98 3
        if (! $this->alias && $this->table)
99
        {
100 3
            $this->alias = $this->table[0];
101 2
        }
102
103 21
        $query = $this->query($query);
104 14
105 3
        foreach ($this->wheres as $key => $value)
106
        {
107
            $text = str_replace($operations, '', $value);
108 18
109
            switch ($value[0])
110
            {
111
                case '<':
112
                    $query->where($key)->lessThan($text);
113
114
                    break;
115
                case '<=':
116
                    $query->where($key)->lessThanOrEqualTo($text);
117
118
                    break;
119
                case '<>':
120
                    $query->where($key)->notEqualTo($text);
121
122
                    break;
123
                case '>':
124
                    $query->where($key)->greaterThan($text);
125
126
                    break;
127
                case '>=':
128
                    $query->where($key)->greaterThanOrEqualTo($text);
129
130
                    break;
131
                case '%':
132
                    $query->where($key)->like($value);
133
134
                    break;
135
                default:
136
                    $query->where($key)->equals($value);
137
138
                    break;
139
            }
140
        }
141
142
        if ($this->limit === null)
143
        {
144
            return $query;
145
        }
146
147
        return $query->limit($this->limit, $this->offset);
148
    }
149
150
    /**
151
     * Sets a where like instance to the query.
152
     *
153
     * @param  string $key
154
     * @param  mixed  $value
155
     * @return self
156
     */
157
    public function where($key, $value)
158
    {
159
        $this->wheres[$key] = $value;
160
161
        return $this;
162
    }
163
164
    /**
165
     * Sets an array of where like conditions.
166
     *
167
     * @param  array $wheres
168
     * @return self
169
     */
170
    public function wheres(array $wheres)
171
    {
172
        foreach ($wheres as $key => $value)
173
        {
174
            $this->where($key, $value);
175
        }
176
177
        return $this;
178
    }
179
180
    /**
181
     * Sets a predefined query instance.
182
     *
183
     * @param  \Rougin\Windstorm\QueryInterface $query
184
     * @return \Rougin\Windstorm\QueryInterface
185
     */
186
    protected function query(QueryInterface $query)
187
    {
188
        $query->select((array) $this->fields);
189
190
        return $query->from($this->table, $this->alias);
191
    }
192
}
193