|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Copyright (c) 2017. Este código foi feito por @marciioluucas, sob licença MIT |
|
5
|
|
|
*/ |
|
6
|
|
|
namespace Phiber\ORM\Queries; |
|
7
|
|
|
|
|
8
|
|
|
use Phiber\ORM\Exceptions\PhiberException; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Classe responsável por fazer as restrições das operações do banco |
|
12
|
|
|
* |
|
13
|
|
|
* @package bin |
|
14
|
|
|
*/ |
|
15
|
|
|
class Restrictions |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var array |
|
19
|
|
|
*/ |
|
20
|
|
|
private static $fieldsAndValues = []; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Restrictions constructor. |
|
24
|
|
|
*/ |
|
25
|
|
|
public function __construct() |
|
26
|
|
|
{ |
|
27
|
|
|
self::$fieldsAndValues = []; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @return array |
|
32
|
|
|
*/ |
|
33
|
|
|
public function getFieldsAndValues(): array |
|
34
|
|
|
{ |
|
35
|
|
|
return self::$fieldsAndValues; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Faz a query de comparação IGUAL |
|
40
|
|
|
* |
|
41
|
|
|
* @example Exemplo: |
|
42
|
|
|
* equals("idade",15); |
|
43
|
|
|
* Criará um pedaço da query do banco assim -> idade = :condition_idade |
|
44
|
|
|
* OBS: O ":condition_idade" é o responsável por depois fazer o binding do valor para |
|
45
|
|
|
* evitar SQL Injection. |
|
46
|
|
|
* |
|
47
|
|
|
* @param string $column |
|
48
|
|
|
* @param string $value |
|
49
|
|
|
* @return array |
|
50
|
|
|
*/ |
|
51
|
|
|
public function equals($column, $value) |
|
52
|
|
|
{ |
|
53
|
|
|
self::addFieldsAndValues($column, $value); |
|
54
|
|
|
|
|
55
|
|
|
return [ |
|
56
|
|
|
"where" => "{$column} = :condition_{$value}" |
|
57
|
|
|
]; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Faz a query de comparação DIFERENTE |
|
62
|
|
|
* |
|
63
|
|
|
* @example Exemplo: |
|
64
|
|
|
* different("idade",15); |
|
65
|
|
|
* Criará um pedaço da query do banco assim -> idade != :condition_idade |
|
66
|
|
|
* OBS: O ":condition_idade" é o responsável por depois fazer o binding do valor para |
|
67
|
|
|
* evitar SQL Injection. |
|
68
|
|
|
* @param string $column |
|
69
|
|
|
* @param string $value |
|
70
|
|
|
* @return array |
|
71
|
|
|
*/ |
|
72
|
|
|
public function different($column, $value) |
|
73
|
|
|
{ |
|
74
|
|
|
self::addFieldsAndValues($column, $value); |
|
75
|
|
|
|
|
76
|
|
|
return [ |
|
77
|
|
|
"where" => "{$column} != :condition_$value" |
|
78
|
|
|
]; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Faz a query de comparação MAIOR QUE |
|
83
|
|
|
* |
|
84
|
|
|
* @example Exemplo: |
|
85
|
|
|
* biggerThen("idade",15); |
|
86
|
|
|
* Criará um pedaço da query do banco assim -> idade > :condition_idade |
|
87
|
|
|
* OBS: O ":condition_idade" é o responsável por depois fazer o binding do valor para |
|
88
|
|
|
* evitar SQL Injection. |
|
89
|
|
|
* @param string $column |
|
90
|
|
|
* @param string $value |
|
91
|
|
|
* @return array |
|
92
|
|
|
*/ |
|
93
|
|
|
public function biggerThen($column, $value) |
|
94
|
|
|
{ |
|
95
|
|
|
self::addFieldsAndValues($column, $value); |
|
96
|
|
|
|
|
97
|
|
|
return [ |
|
98
|
|
|
"where" => "{$column} > :condition_{$value}" |
|
99
|
|
|
]; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Faz a query de comparação MAIOR OU IGUAL A |
|
104
|
|
|
* |
|
105
|
|
|
* @example Exemplo: |
|
106
|
|
|
* greaterThan("idade",15); |
|
107
|
|
|
* Criará um pedaço da query do banco assim -> idade >= :condition_idade |
|
108
|
|
|
* OBS: O ":condition_idade" é o responsável por depois fazer o binding do valor para |
|
109
|
|
|
* evitar SQL Injection. |
|
110
|
|
|
* @param string $column |
|
111
|
|
|
* @param string $value |
|
112
|
|
|
* @return array |
|
113
|
|
|
*/ |
|
114
|
|
|
public function greaterThan($column, $value) |
|
115
|
|
|
{ |
|
116
|
|
|
self::addFieldsAndValues($column, $value); |
|
117
|
|
|
|
|
118
|
|
|
return [ |
|
119
|
|
|
"where" => "{$column} >= :condition_{$value}" |
|
120
|
|
|
]; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Faz a query de comparação MENOR QUE |
|
125
|
|
|
* |
|
126
|
|
|
* @example Exemplo: |
|
127
|
|
|
* lessThen("idade",15); |
|
128
|
|
|
* Criará um pedaço da query do banco assim -> idade < :condition_idade |
|
129
|
|
|
* OBS: O ":condition_idade" é o responsável por depois fazer o binding do valor para |
|
130
|
|
|
* evitar SQL Injection. |
|
131
|
|
|
* @param string $column |
|
132
|
|
|
* @param string $param2 |
|
|
|
|
|
|
133
|
|
|
* @return array |
|
134
|
|
|
*/ |
|
135
|
|
|
public function lessThen($column, $value) |
|
136
|
|
|
{ |
|
137
|
|
|
self::addFieldsAndValues($column, $value); |
|
138
|
|
|
|
|
139
|
|
|
return [ |
|
140
|
|
|
"where" => "{$column} < :condition_{$value}" |
|
141
|
|
|
]; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Faz a query de comparação MENOR OU IGUAL A |
|
146
|
|
|
* |
|
147
|
|
|
* @example Exemplo: |
|
148
|
|
|
* lessLike("idade",15); |
|
149
|
|
|
* Criará um pedaço da query do banco assim -> idade <= :condition_idade |
|
150
|
|
|
* OBS: O ":condition_idade" é o responsável por depois fazer o binding do valor para |
|
151
|
|
|
* evitar SQL Injection. |
|
152
|
|
|
* @param string $column |
|
153
|
|
|
* @param string $value |
|
154
|
|
|
* @return array |
|
155
|
|
|
*/ |
|
156
|
|
|
public function lessLike($column, $value) |
|
157
|
|
|
{ |
|
158
|
|
|
self::addFieldsAndValues($column, $value); |
|
159
|
|
|
|
|
160
|
|
|
return [ |
|
161
|
|
|
"where" => "{$column} <= :condition_{$value}" |
|
162
|
|
|
]; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Faz a query de comparação LIKE |
|
167
|
|
|
* |
|
168
|
|
|
* @example Exemplo: |
|
169
|
|
|
* like("nome","Jhon Snow"); |
|
170
|
|
|
* Criará um pedaço da query do banco assim -> idade LIKE %:condition_nome% |
|
171
|
|
|
* OBS: O ":condition_nome" é o responsável por depois fazer o binding do valor para |
|
172
|
|
|
* evitar SQL Injection. |
|
173
|
|
|
* @param $param1 |
|
174
|
|
|
* @param $param2 |
|
175
|
|
|
* @return array |
|
176
|
|
|
*/ |
|
177
|
|
|
public function like($param1, $param2) |
|
178
|
|
|
{ |
|
179
|
|
|
self::addFieldsAndValues($param1, $param2); |
|
180
|
|
|
|
|
181
|
|
|
return [ |
|
182
|
|
|
"where" => $param1 . " LIKE CONCAT('%',:condition_" . $param1 . ",'%')", |
|
183
|
|
|
]; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Faz a query de conjunção OU |
|
188
|
|
|
* Exemplo: |
|
189
|
|
|
* $condicao1 = equals("idade",15); |
|
190
|
|
|
* $condicao2 = like("nome","Jhon"); |
|
191
|
|
|
* either($condicao1,$condicao2); |
|
192
|
|
|
* |
|
193
|
|
|
* Criará um pedaço da query do banco assim -> |
|
194
|
|
|
* (idade = :condition_idade or nome like %:condition_nome%); |
|
195
|
|
|
* OBS: O ":condition_idade, :condition_nome" são responsáveis por depois fazer o |
|
196
|
|
|
* binding do valor para evitar SQL Injection. |
|
197
|
|
|
* @param $condition1 |
|
198
|
|
|
* @param $condition2 |
|
199
|
|
|
* @return array |
|
200
|
|
|
*/ |
|
201
|
|
View Code Duplication |
public function either($condition1, $condition2) |
|
|
|
|
|
|
202
|
|
|
{ |
|
203
|
|
|
|
|
204
|
|
|
return [ |
|
205
|
|
|
"where" => "(" . $condition1['where'] . " OR " . $condition2['where'] . ")", |
|
206
|
|
|
]; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* Faz a query de conjunção E |
|
211
|
|
|
* Exemplo: |
|
212
|
|
|
* $condicao1 = eq("idade",15); |
|
213
|
|
|
* $condicao2 = like("nome","Jhon"); |
|
214
|
|
|
* or($condicao1,$condicao2); |
|
215
|
|
|
* |
|
216
|
|
|
* Criará um pedaço da query do banco assim -> |
|
217
|
|
|
* (idade = :condition_idade and nome like %:condition_nome%); |
|
218
|
|
|
* OBS: O ":condition_idade, :condition_nome" são responsáveis por depois fazer o |
|
219
|
|
|
* binding do valor para evitar SQL Injection. |
|
220
|
|
|
* @param $condition1 |
|
221
|
|
|
* @param $condition2 |
|
222
|
|
|
* @return array |
|
223
|
|
|
*/ |
|
224
|
|
View Code Duplication |
public function and ($condition1, $condition2) |
|
|
|
|
|
|
225
|
|
|
{ |
|
226
|
|
|
|
|
227
|
|
|
return [ |
|
228
|
|
|
"where" => "(" . $condition1['where'] . " AND " . $condition2['where'] . ")" |
|
229
|
|
|
]; |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* Faz a query de limite. |
|
234
|
|
|
* |
|
235
|
|
|
* @example Exemplo: |
|
236
|
|
|
* Retornar os primeiros registros com o limite 15 |
|
237
|
|
|
* LIMIT 15 |
|
238
|
|
|
* @param int $limit |
|
239
|
|
|
* @return array |
|
240
|
|
|
*/ |
|
241
|
|
|
public function limit($limit) |
|
242
|
|
|
{ |
|
243
|
|
|
return [ |
|
244
|
|
|
"limit" => (int)$limit . " " |
|
245
|
|
|
]; |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
/** |
|
249
|
|
|
* Faz a query de offset (a partir de ) |
|
250
|
|
|
* |
|
251
|
|
|
* @example Exemplo: |
|
252
|
|
|
* Retorne os x resultados a partir de 15 |
|
253
|
|
|
* OFFSET 15 |
|
254
|
|
|
* @param $offset |
|
255
|
|
|
* @return array |
|
256
|
|
|
*/ |
|
257
|
|
|
public function offset($offset) |
|
258
|
|
|
{ |
|
259
|
|
|
return [ "offset" => $offset ]; |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
/** |
|
263
|
|
|
* Faz a query de OrderBy |
|
264
|
|
|
* |
|
265
|
|
|
* @example EXEMPLO: |
|
266
|
|
|
* Passará um array com os orderBys e se quer desc ou asc. |
|
267
|
|
|
* Se caso quiser tudo desc ou tudo asc, colocar DESC|ASC somente no ultimo. |
|
268
|
|
|
* orderBy(["nome asc","id desc"]) |
|
269
|
|
|
* @param array $orderBy |
|
270
|
|
|
* @return array |
|
271
|
|
|
*/ |
|
272
|
|
|
public function orderBy(array $orderBy) |
|
273
|
|
|
{ |
|
274
|
|
|
return [ |
|
275
|
|
|
"orderby" => $orderBy |
|
276
|
|
|
]; |
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
|
|
/** |
|
280
|
|
|
* Responsável por montar instruções JOIN. |
|
281
|
|
|
* |
|
282
|
|
|
* @param string $table |
|
283
|
|
|
* @param array $on |
|
284
|
|
|
* @param string $type |
|
285
|
|
|
* @return void |
|
286
|
|
|
*/ |
|
287
|
|
|
public function join(string $table, array $on, string $type = "INNER") |
|
288
|
|
|
{ |
|
289
|
|
|
if (count($on) > 2) { |
|
290
|
|
|
throw new PhiberException("error_on_join"); |
|
291
|
|
|
} |
|
292
|
|
|
|
|
293
|
|
|
try { |
|
294
|
|
|
switch (strtoupper($type)) { |
|
295
|
|
View Code Duplication |
case "INNER": |
|
|
|
|
|
|
296
|
|
|
return ["join" => "INNER JOIN " . $table . " ON " . $on[0] . " = " . $on[1]]; |
|
297
|
|
|
break; |
|
|
|
|
|
|
298
|
|
|
|
|
299
|
|
View Code Duplication |
case "LEFT": |
|
|
|
|
|
|
300
|
|
|
return ["join" => "LEFT JOIN " . $table . " ON " . $on[0] . " = " . $on[1]]; |
|
301
|
|
|
break; |
|
|
|
|
|
|
302
|
|
|
|
|
303
|
|
View Code Duplication |
case "RIGHT": |
|
|
|
|
|
|
304
|
|
|
return ["join" => "RIGHT JOIN " . $table . " ON " . $on[0] . " = " . $on[1]]; |
|
305
|
|
|
break; |
|
|
|
|
|
|
306
|
|
|
|
|
307
|
|
View Code Duplication |
case "FULL OUTER": |
|
|
|
|
|
|
308
|
|
|
return ["join" => "FULL OUTER JOIN " . $table . " ON " . $on[0] . " = " . $on[1]]; |
|
309
|
|
|
break; |
|
|
|
|
|
|
310
|
|
|
|
|
311
|
|
View Code Duplication |
default: |
|
|
|
|
|
|
312
|
|
|
return ["join" => "INNER JOIN " . $table . " ON " . $on[0] . " = " . $on[1]]; |
|
313
|
|
|
} |
|
314
|
|
|
|
|
315
|
|
|
} catch (PhiberException $phiberException) { |
|
316
|
|
|
throw new PhiberException("join_no_exists"); |
|
317
|
|
|
} |
|
318
|
|
|
} |
|
319
|
|
|
|
|
320
|
|
|
/** |
|
321
|
|
|
* Função para determinar os campos que quer buscar no SELECT. |
|
322
|
|
|
* |
|
323
|
|
|
* @example Exemplo: fields("nome, id"); |
|
324
|
|
|
* Gerará: Select nome, id from ... |
|
325
|
|
|
* Caso não informar campos, retornará todos. |
|
326
|
|
|
* @param array $fields |
|
327
|
|
|
* @return array |
|
328
|
|
|
*/ |
|
329
|
|
|
public function fields(array $fields) |
|
330
|
|
|
{ |
|
331
|
|
|
if (!empty($fields)) { |
|
332
|
|
|
return ["fields" => $fields]; |
|
333
|
|
|
} |
|
334
|
|
|
|
|
335
|
|
|
return ["fields" => ["*"]]; |
|
336
|
|
|
} |
|
337
|
|
|
|
|
338
|
|
|
/** |
|
339
|
|
|
* Adiciona os campos e os valores. |
|
340
|
|
|
* |
|
341
|
|
|
* @ignore |
|
342
|
|
|
* @param string $field |
|
343
|
|
|
* @param mixed $value |
|
344
|
|
|
*/ |
|
345
|
|
|
private function addFieldsAndValues($field, $value) |
|
346
|
|
|
{ |
|
347
|
|
|
self::$fieldsAndValues['fields_and_values'][$field] = $value; |
|
348
|
|
|
} |
|
349
|
|
|
} |
|
350
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.