1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Compolomus\LSQLQueryBuilder\System\Traits; |
4
|
|
|
|
5
|
|
|
use Compolomus\LSQLQueryBuilder\BuilderException; |
6
|
|
|
|
7
|
|
|
trait Helper |
8
|
|
|
{ |
9
|
2 |
|
public function concatWhere(array $conditions, string $separator = 'and'): string |
10
|
|
|
{ |
11
|
2 |
|
if (!\in_array($separator, ['and', 'or'], true)) { |
12
|
1 |
|
throw new BuilderException('Передан неверный тип |WHERE concate|'); |
13
|
|
|
} |
14
|
2 |
|
return implode(' ' . strtoupper($separator) . ' ', $conditions); |
15
|
|
|
} |
16
|
|
|
|
17
|
7 |
|
public function concat(array $fields): string |
18
|
|
|
{ |
19
|
7 |
|
return implode(',', $fields); |
20
|
|
|
} |
21
|
|
|
|
22
|
1 |
|
public function concatOrder(array $order, string $type = 'asc'): string |
23
|
|
|
{ |
24
|
1 |
View Code Duplication |
if (!\in_array($type, ['asc', 'desc'], true)) { |
|
|
|
|
25
|
1 |
|
throw new BuilderException('Передан неверный тип |ORDER concate|'); |
26
|
|
|
} |
27
|
1 |
|
$result = ''; |
28
|
1 |
|
if (\count($order) > 0) { |
29
|
1 |
|
$result = implode(',', $this->escapeField($order)) . ' ' . strtoupper($type); |
30
|
|
|
} |
31
|
1 |
|
return $result; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param $field |
36
|
|
|
* @return array|string |
37
|
|
|
*/ |
38
|
12 |
|
public function escapeField($field) |
39
|
|
|
{ |
40
|
12 |
|
return \is_array($field) ? array_map([$this, 'escapeField'], $field) : str_replace('`*`', '*', |
41
|
12 |
|
'`' . str_replace('.', '`.`', $field) . '`'); |
42
|
|
|
} |
43
|
|
|
|
44
|
1 |
|
public function map(string $field, $value): string |
45
|
|
|
{ |
46
|
1 |
|
return $field . ' = ' . $value; |
47
|
|
|
} |
48
|
|
|
|
49
|
3 |
|
public function uid(string $prefix): string |
50
|
|
|
{ |
51
|
3 |
|
$prefix = strtoupper($prefix); |
52
|
3 |
|
$str = uniqid($prefix . '-', true); |
53
|
3 |
|
return str_replace('.', '', substr($str, 2, 2) . substr($str, 12, -4)) . $prefix; |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.