1 | <?php |
||
2 | |||
3 | namespace ClickHouseDB\Query\Degeneration; |
||
4 | |||
5 | use ClickHouseDB\Exception\QueryException; |
||
6 | use ClickHouseDB\Query\Degeneration; |
||
7 | |||
8 | class Conditions implements Degeneration |
||
9 | { |
||
10 | /** |
||
11 | * @var array |
||
12 | */ |
||
13 | protected $bindings = []; |
||
14 | |||
15 | /** |
||
16 | * @param array $bindings |
||
17 | */ |
||
18 | 3 | public function bindParams(array $bindings) |
|
19 | { |
||
20 | 3 | foreach ($bindings as $column => $value) { |
|
21 | 3 | $this->bindings[$column] = $value; |
|
22 | } |
||
23 | 3 | } |
|
24 | |||
25 | |||
26 | 3 | static function __ifsets($matches, $markers) |
|
0 ignored issues
–
show
|
|||
27 | { |
||
28 | 3 | $content_false = ''; |
|
29 | 3 | $condition = ''; |
|
30 | 3 | $flag_else = ''; |
|
31 | //print_r($matches); |
||
32 | 3 | if (sizeof($matches) == 4) { |
|
33 | 3 | list($condition, $preset, $variable, $content_true) = $matches; |
|
34 | 3 | } elseif (sizeof($matches) == 6) { |
|
35 | 3 | list($condition, $preset, $variable, $content_true, $flag_else, $content_false) = $matches; |
|
36 | } else { |
||
37 | throw new QueryException('Error in parse Conditions' . json_encode($matches)); |
||
38 | } |
||
39 | 3 | $variable = trim($variable); |
|
40 | 3 | $preset = strtolower(trim($preset)); |
|
41 | |||
42 | 3 | if ($preset == '') { |
|
43 | 3 | return (isset($markers[$variable]) && ($markers[$variable] || is_numeric($markers[$variable]))) |
|
44 | 3 | ? $content_true |
|
45 | 3 | : $content_false; |
|
46 | } |
||
47 | 2 | if ($preset == 'set') { |
|
48 | 2 | return (isset($markers[$variable]) && !empty($markers[$variable])) ? $content_true : $content_false; |
|
49 | } |
||
50 | 2 | if ($preset == 'bool') { |
|
51 | 2 | return (isset($markers[$variable]) && is_bool($markers[$variable]) && $markers[$variable] == true) |
|
52 | 2 | ? $content_true |
|
53 | 2 | : $content_false; |
|
54 | } |
||
55 | 2 | if ($preset == 'string') { |
|
56 | 2 | return (isset($markers[$variable]) && is_string($markers[$variable]) && strlen($markers[$variable])) |
|
57 | 2 | ? $content_true |
|
58 | 2 | : $content_false; |
|
59 | } |
||
60 | 2 | if ($preset == 'int') { |
|
61 | 2 | return (isset($markers[$variable]) && intval($markers[$variable]) <> 0) |
|
62 | 2 | ? $content_true |
|
63 | 2 | : $content_false; |
|
64 | } |
||
65 | |||
66 | return ''; |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * @param string $sql |
||
71 | * @return mixed |
||
72 | */ |
||
73 | 3 | public function process($sql) |
|
74 | { |
||
75 | 3 | $markers = $this->bindings; |
|
76 | |||
77 | // ------ if/else conditions & if[set|int]/else conditions ----- |
||
78 | $sql = preg_replace_callback('#\{if(.{0,}?)\s+([^\}]+?)\}(.+?)(\{else\}([^\{]+?)?)?\s*\{\/if}#sui', function ($matches) use ($markers) { |
||
79 | 3 | return self::__ifsets($matches, $markers); |
|
80 | 3 | } |
|
81 | 3 | , $sql); |
|
82 | |||
83 | 3 | return $sql; |
|
84 | |||
85 | /* |
||
86 | * $ifint var ELSE {ENDIF} |
||
87 | * |
||
88 | */ |
||
89 | |||
90 | // stackoverflow |
||
91 | // if(whatever) { } else { adsffdsa } else if() { } |
||
92 | // /^if\s*\((.*?)\)\s*{(.*?)}(\s*(else|else\s+if\s*\((.*?)\))\s*{(.*?)})*/ |
||
93 | // if (condition_function(params)) { |
||
94 | // statements; |
||
95 | //} |
||
96 | // if\s*\(((?:(?:(?:"(?:(?:\\")|[^"])*")|(?:'(?:(?:\\')|[^'])*'))|[^\(\)]|\((?1)\))*+)\)\s*{((?:(?:(?:"(?:(?:\\")|[^"])*")|(?:'(?:(?:\\')|[^'])*'))|[^{}]|{(?2)})*+)}\s*(?:(?:else\s*{((?:(?:(?:"(?:(?:\\")|[^"])*")|(?:'(?:(?:\\')|[^'])*'))|[^{}]|{(?3)})*+)}\s*)|(?:else\s*if\s*\(((?:(?:(?:"(?:(?:\\")|[^"])*")|(?:'(?:(?:\\')|[^'])*'))|[^\(\)]|\((?4)\))*+)\)\s*{((?:(?:(?:"(?:(?:\\")|[^"])*")|(?:'(?:(?:\\')|[^'])*'))|[^{}]|{(?5)})*+)}\s*))*; |
||
97 | // @if\s*\(\s*([^)]*)\s*\)\s*(((?!@if|@endif).)*)\s*(?:@else\s*(((?!@if|@endif).)*))?\s*@endif |
||
98 | // @if \s* \( \s* ([^)]*)\s*\)\s*(((?!@if|@endif).)*)\s*(?:@else\s*(((?!@if|@endif).)*))?\s*@endif |
||
99 | // [^}] |
||
100 | |||
101 | // // 3. process if conditions |
||
102 | // $sql = preg_replace_callback('#\{if\s(.+?)}(.+?)\{/if}#sui', function($matches) use ($markers) { |
||
103 | // list($condition, $variable, $content) = $matches; |
||
104 | // if (isset($markers[$variable]) && ($markers[$variable] || is_numeric($markers[$variable]))) { |
||
105 | // return $content; |
||
106 | // } |
||
107 | // }, $sql); |
||
108 | |||
109 | // 1. process if[set|int]/else conditions |
||
110 | // $sql = preg_replace_callback('#\{if(.{1,}?)\s(.+?)}(.+?)\{else}(.+?)\{/if}#sui', function($matches) use ($markers) {return self::__ifsets($matches, $markers, true); }, $sql); |
||
111 | // $sql = preg_replace_callback('#\{if(.{1,}?)\s(.+?)}(.+?)\{/if}#sui', function($matches) use ($markers) { return self::__ifsets($matches, $markers, false); }, $sql); |
||
112 | } |
||
113 | |||
114 | } |
||
115 |
Generally, we recommend to declare visibility for all methods in your source code. This has the advantage of clearly communication to other developers, and also yourself, how this method should be consumed.
If you are not sure which visibility to choose, it is a good idea to start with the most restrictive visibility, and then raise visibility as needed, i.e. start with
private
, and only raise it toprotected
if a sub-class needs to have access, orpublic
if an external class needs access.