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
|
|
|
* Initializes the mutator instance. |
48
|
|
|
* |
49
|
|
|
* @param integer|null $limit |
50
|
|
|
* @param integer|null $offset |
51
|
|
|
*/ |
52
|
21 |
|
public function __construct($limit = null, $offset = null) |
53
|
|
|
{ |
54
|
21 |
|
$this->limit = $limit; |
55
|
|
|
|
56
|
21 |
|
$this->offset = $offset; |
57
|
21 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Returns an array of specified fields. |
61
|
|
|
* |
62
|
|
|
* @return array |
63
|
|
|
*/ |
64
|
|
|
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
|
|
|
* @param \Rougin\Windstorm\QueryInterface $query |
93
|
|
|
*/ |
94
|
21 |
|
public function set(QueryInterface $query) |
95
|
|
|
{ |
96
|
21 |
|
$operations = array('<>', '>=', '<=', '>', '<', '='); |
97
|
|
|
|
98
|
21 |
|
if (! $this->alias && $this->table) |
99
|
14 |
|
{ |
100
|
21 |
|
$this->alias = $this->table[0]; |
101
|
14 |
|
} |
102
|
|
|
|
103
|
21 |
|
$query = $this->query($query); |
104
|
|
|
|
105
|
21 |
|
$multiple = false; |
106
|
|
|
|
107
|
21 |
|
foreach ($this->wheres as $key => $value) |
108
|
|
|
{ |
109
|
3 |
|
$text = str_replace($operations, '', $value); |
110
|
|
|
|
111
|
3 |
|
$operator = str_replace($text, '', $value); |
112
|
|
|
|
113
|
|
|
switch ($operator) |
114
|
|
|
{ |
115
|
3 |
|
case '<': |
116
|
|
|
if ($multiple) |
117
|
|
|
{ |
118
|
|
|
$query->andWhere($key)->lessThan($text); |
119
|
|
|
} |
120
|
|
|
else |
121
|
|
|
{ |
122
|
|
|
$query->where($key)->lessThan($text); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
break; |
126
|
3 |
|
case '<=': |
127
|
|
|
if ($multiple) |
128
|
|
|
{ |
129
|
|
|
$query->andWhere($key)->lessThanOrEqualTo($text); |
130
|
|
|
} |
131
|
|
|
else |
132
|
|
|
{ |
133
|
|
|
$query->where($key)->lessThanOrEqualTo($text); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
break; |
137
|
3 |
|
case '<>': |
138
|
|
|
if ($multiple) |
139
|
|
|
{ |
140
|
|
|
$query->andWhere($key)->notEqualTo($text); |
141
|
|
|
} |
142
|
|
|
else |
143
|
|
|
{ |
144
|
|
|
$query->where($key)->notEqualTo($text); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
break; |
148
|
3 |
|
case '>': |
149
|
|
|
if ($multiple) |
150
|
|
|
{ |
151
|
|
|
$query->andWhere($key)->greaterThan($text); |
152
|
|
|
} |
153
|
|
|
else |
154
|
|
|
{ |
155
|
|
|
$query->where($key)->greaterThan($text); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
break; |
159
|
3 |
|
case '>=': |
160
|
|
|
if ($multiple) |
161
|
|
|
{ |
162
|
|
|
$query->andWhere($key)->greaterThanOrEqualTo($text); |
163
|
|
|
} |
164
|
|
|
else |
165
|
|
|
{ |
166
|
|
|
$query->where($key)->greaterThanOrEqualTo($text); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
break; |
170
|
2 |
|
default: |
171
|
3 |
|
if ($value[0] === '%' || $value[strlen($value) - 1] === '%') |
172
|
2 |
|
{ |
173
|
|
|
break; |
174
|
|
|
} |
175
|
|
|
|
176
|
1 |
|
if ($multiple) |
177
|
2 |
|
{ |
178
|
|
|
$query->andWhere($key)->equals($value); |
179
|
|
|
} |
180
|
|
|
else |
181
|
|
|
{ |
182
|
3 |
|
$query->where($key)->equals($value); |
183
|
|
|
} |
184
|
|
|
|
185
|
3 |
|
break; |
186
|
2 |
|
} |
187
|
|
|
|
188
|
3 |
|
if ($value[0] === '%' || $value[strlen($value) - 1] === '%') |
189
|
2 |
|
{ |
190
|
|
|
if ($multiple) |
191
|
|
|
{ |
192
|
|
|
$query->andWhere($key)->like($value); |
193
|
|
|
} |
194
|
|
|
else |
195
|
|
|
{ |
196
|
|
|
$query->where($key)->like($value); |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|
200
|
3 |
|
$multiple = true; |
201
|
14 |
|
} |
202
|
|
|
|
203
|
21 |
|
$multiple = false; |
|
|
|
|
204
|
|
|
|
205
|
21 |
|
if ($this->limit === null) |
206
|
14 |
|
{ |
207
|
21 |
|
return $query; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
return $query->limit($this->limit, $this->offset); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Sets a where like instance to the query. |
215
|
|
|
* |
216
|
|
|
* @param string $key |
217
|
|
|
* @param mixed $value |
218
|
|
|
* @return self |
219
|
|
|
*/ |
220
|
3 |
|
public function where($key, $value) |
221
|
|
|
{ |
222
|
3 |
|
$this->wheres[$key] = $value; |
223
|
|
|
|
224
|
3 |
|
return $this; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Sets an array of where like conditions. |
229
|
|
|
* |
230
|
|
|
* @param array $wheres |
231
|
|
|
* @return self |
232
|
|
|
*/ |
233
|
|
|
public function wheres(array $wheres) |
234
|
|
|
{ |
235
|
|
|
foreach ($wheres as $key => $value) |
236
|
|
|
{ |
237
|
|
|
$this->where($key, $value); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
return $this; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Sets a predefined query instance. |
245
|
|
|
* |
246
|
|
|
* @param \Rougin\Windstorm\QueryInterface $query |
247
|
|
|
* @return \Rougin\Windstorm\QueryInterface |
248
|
|
|
*/ |
249
|
21 |
|
protected function query(QueryInterface $query) |
250
|
|
|
{ |
251
|
21 |
|
$query->select((array) $this->fields); |
252
|
|
|
|
253
|
21 |
|
return $query->from($this->table, $this->alias); |
254
|
|
|
} |
255
|
|
|
} |
256
|
|
|
|