1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Compolomus\LSQLQueryBuilder\System\Traits; |
4
|
|
|
|
5
|
|
|
trait Helper |
6
|
|
|
{ |
7
|
|
|
public function concatWhere(array $conditions, string $separator = 'and') |
8
|
|
|
{ |
9
|
|
|
if (!in_array($separator, ['and', 'or'])) { |
10
|
|
|
throw new \InvalidArgumentException('Передан неверный тип |WHERE concate|'); |
11
|
|
|
} |
12
|
|
|
return implode(' ' . strtoupper($separator) . ' ', $conditions); |
13
|
|
|
} |
14
|
|
|
|
15
|
|
|
public function concat(array $fields): string |
16
|
|
|
{ |
17
|
|
|
return implode(',', $fields); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
public function concatOrder(array $order, string $type = 'asc') |
21
|
|
|
{ |
22
|
|
|
if (!in_array($type, ['asc', 'desc'])) { |
23
|
|
|
throw new \InvalidArgumentException('Передан неверный тип |ORDER concate|'); |
24
|
|
|
} |
25
|
|
|
$result = ''; |
26
|
|
|
if (count($order) > 0) { |
27
|
|
|
$result = implode(',', $this->escapeField($order)) . ' ' . strtoupper($type); |
28
|
|
|
} |
29
|
|
|
return $result; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param $field |
34
|
|
|
* @return array|string |
35
|
|
|
*/ |
36
|
|
|
public function escapeField($field) |
37
|
|
|
{ |
38
|
|
|
return is_array($field) ? array_map([$this, 'escapeField'], $field) : str_replace('`*`', '*', '`' . str_replace('.', '`.`', $field) . '`'); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function map(string $field, $value): string |
42
|
|
|
{ |
43
|
|
|
return $field . ' = ' . $value; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function uid(string $prefix): string |
47
|
|
|
{ |
48
|
|
|
$prefix = strtoupper($prefix); |
49
|
|
|
$str = uniqid($prefix . '-', true); |
50
|
|
|
return str_replace('.', '', substr($str, 2, 2) . substr($str, 12, -4)) . $prefix; |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|