1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ClickHouseDB\Query\Degeneration; |
4
|
|
|
|
5
|
|
|
use ClickHouseDB\Query\Degeneration; |
6
|
|
|
|
7
|
|
|
class Conditions implements Degeneration |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var array |
11
|
|
|
*/ |
12
|
|
|
protected $bindings = []; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @param array $bindings |
16
|
|
|
*/ |
17
|
1 |
|
public function bindParams(array $bindings) |
18
|
|
|
{ |
19
|
1 |
|
foreach ($bindings as $column => $value) { |
20
|
1 |
|
$this->bindings[$column] = $value; |
21
|
|
|
} |
22
|
1 |
|
} |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
static function __ifsets($matches, $markers, $else = false) |
|
|
|
|
26
|
|
|
{ |
27
|
|
|
$content_false = ''; |
28
|
|
|
|
29
|
|
|
if ($else) |
30
|
|
|
{ |
31
|
|
|
list($condition, $preset, $variable, $content_true, $content_false) = $matches; |
32
|
|
|
} else |
33
|
|
|
{ |
34
|
|
|
list($condition, $preset, $variable, $content_true) = $matches; |
35
|
|
|
} |
36
|
|
|
$preset = strtolower($preset); |
37
|
|
|
|
38
|
|
|
if ($preset == 'set') |
39
|
|
|
{ |
40
|
|
|
return (isset($markers[$variable]) && !empty($markers[$variable])) ? $content_true : $content_false; |
41
|
|
|
} |
42
|
|
|
if ($preset == 'bool') |
43
|
|
|
{ |
44
|
|
|
return (isset($markers[$variable]) && is_bool($markers[$variable]) && $markers[$variable] == true) |
45
|
|
|
? $content_true |
46
|
|
|
: $content_false; |
47
|
|
|
} |
48
|
|
|
if ($preset == 'string') |
49
|
|
|
{ |
50
|
|
|
return (isset($markers[$variable]) && is_string($markers[$variable]) && strlen($markers[$variable])) |
51
|
|
|
? $content_true |
52
|
|
|
: $content_false; |
53
|
|
|
} |
54
|
|
|
if ($preset == 'int') |
55
|
|
|
{ |
56
|
|
|
return (isset($markers[$variable]) && intval($markers[$variable]) <> 0) |
57
|
|
|
? $content_true |
58
|
|
|
: $content_false; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return ''; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param string $sql |
66
|
|
|
* @return mixed |
67
|
|
|
*/ |
68
|
1 |
|
public function process($sql) |
69
|
|
|
{ |
70
|
1 |
|
$markers = $this->bindings; |
71
|
|
|
|
72
|
|
|
// 2. process if/else conditions |
73
|
|
|
$sql = preg_replace_callback('#\{if\s(.+?)}(.+?)\{else}(.+?)\{/if}#sui', function($matches) use ($markers) { |
74
|
1 |
|
list($condition, $variable, $content_true, $content_false) = $matches; |
75
|
|
|
|
76
|
1 |
|
return (isset($markers[$variable]) && ($markers[$variable] || is_numeric($markers[$variable]))) |
77
|
|
|
? $content_true |
78
|
1 |
|
: $content_false; |
79
|
1 |
|
}, $sql); |
80
|
|
|
|
81
|
|
|
// 3. process if conditions |
82
|
|
|
$sql = preg_replace_callback('#\{if\s(.+?)}(.+?)\{/if}#sui', function($matches) use ($markers) { |
83
|
1 |
|
list($condition, $variable, $content) = $matches; |
84
|
|
|
|
85
|
1 |
|
if (isset($markers[$variable]) && ($markers[$variable] || is_numeric($markers[$variable]))) { |
86
|
1 |
|
return $content; |
87
|
|
|
} |
88
|
1 |
|
}, $sql); |
89
|
|
|
|
90
|
|
|
// 1. process if[set|int]/else conditions |
91
|
|
|
$sql = preg_replace_callback('#\{if(.{1,}?)\s(.+?)}(.+?)\{else}(.+?)\{/if}#sui', function($matches) use ($markers) {return self::__ifsets($matches, $markers, true); }, $sql); |
92
|
|
|
$sql = preg_replace_callback('#\{if(.{1,}?)\s(.+?)}(.+?)\{/if}#sui', function($matches) use ($markers) { return self::__ifsets($matches, $markers, false); }, $sql); |
93
|
|
|
|
94
|
1 |
|
return $sql; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
} |
98
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.