|
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 |
|
foreach ($this->wheres as $key => $value) |
|
106
|
|
|
{ |
|
107
|
3 |
|
$text = str_replace($operations, '', $value); |
|
108
|
|
|
|
|
109
|
3 |
|
$operator = str_replace($text, '', $value); |
|
110
|
|
|
|
|
111
|
|
|
switch ($operator) |
|
112
|
|
|
{ |
|
113
|
3 |
|
case '<': |
|
114
|
|
|
$query->where($key)->lessThan($text); |
|
115
|
|
|
|
|
116
|
|
|
break; |
|
117
|
3 |
|
case '<=': |
|
118
|
|
|
$query->where($key)->lessThanOrEqualTo($text); |
|
119
|
|
|
|
|
120
|
|
|
break; |
|
121
|
3 |
|
case '<>': |
|
122
|
|
|
$query->where($key)->notEqualTo($text); |
|
123
|
|
|
|
|
124
|
|
|
break; |
|
125
|
3 |
|
case '>': |
|
126
|
|
|
$query->where($key)->greaterThan($text); |
|
127
|
|
|
|
|
128
|
|
|
break; |
|
129
|
3 |
|
case '>=': |
|
130
|
|
|
$query->where($key)->greaterThanOrEqualTo($text); |
|
131
|
|
|
|
|
132
|
|
|
break; |
|
133
|
2 |
|
default: |
|
134
|
3 |
|
if ($value[0] === '%' || $value[strlen($value) - 1] === '%') |
|
135
|
2 |
|
{ |
|
136
|
|
|
break; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
3 |
|
$query->where($key)->equals($value); |
|
140
|
|
|
|
|
141
|
3 |
|
break; |
|
142
|
2 |
|
} |
|
143
|
|
|
|
|
144
|
3 |
|
if ($value[0] === '%' || $value[strlen($value) - 1] === '%') |
|
145
|
2 |
|
{ |
|
146
|
1 |
|
$query->where($key)->like($value); |
|
147
|
|
|
} |
|
148
|
14 |
|
} |
|
149
|
|
|
|
|
150
|
|
|
// echo $query . '<br>'; |
|
151
|
|
|
|
|
152
|
21 |
|
if ($this->limit === null) |
|
153
|
14 |
|
{ |
|
154
|
21 |
|
return $query; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
return $query->limit($this->limit, $this->offset); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Sets a where like instance to the query. |
|
162
|
|
|
* |
|
163
|
|
|
* @param string $key |
|
164
|
|
|
* @param mixed $value |
|
165
|
|
|
* @return self |
|
166
|
|
|
*/ |
|
167
|
3 |
|
public function where($key, $value) |
|
168
|
|
|
{ |
|
169
|
3 |
|
$this->wheres[$key] = $value; |
|
170
|
|
|
|
|
171
|
3 |
|
return $this; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Sets an array of where like conditions. |
|
176
|
|
|
* |
|
177
|
|
|
* @param array $wheres |
|
178
|
|
|
* @return self |
|
179
|
|
|
*/ |
|
180
|
|
|
public function wheres(array $wheres) |
|
181
|
|
|
{ |
|
182
|
|
|
foreach ($wheres as $key => $value) |
|
183
|
|
|
{ |
|
184
|
|
|
$this->where($key, $value); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
return $this; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* Sets a predefined query instance. |
|
192
|
|
|
* |
|
193
|
|
|
* @param \Rougin\Windstorm\QueryInterface $query |
|
194
|
|
|
* @return \Rougin\Windstorm\QueryInterface |
|
195
|
|
|
*/ |
|
196
|
21 |
|
protected function query(QueryInterface $query) |
|
197
|
|
|
{ |
|
198
|
21 |
|
$query->select((array) $this->fields); |
|
199
|
|
|
|
|
200
|
21 |
|
return $query->from($this->table, $this->alias); |
|
201
|
|
|
} |
|
202
|
|
|
} |
|
203
|
|
|
|