PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 2.1.4
JetFormBuilder — Dynamic Blocks Form Builder v2.1.4
3.6.3.1 3.6.3 3.6.2.2 3.6.2.1 3.6.2 3.6.1.1 3.6.1 3.6.0.1 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.1.0 2.1.1 2.1.10 2.1.11 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 3.0.0 3.0.0.1 3.0.0.2 3.0.0.3 3.0.1 3.0.1.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.0.1 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 3.2.0 3.2.1 3.2.2 3.2.3 3.3.0 3.3.1 3.3.2 3.3.3 3.3.3.1 3.3.4 3.3.4.1 3.3.4.2 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.5.1 3.4.5.2 3.4.6 3.4.7 3.4.7.1 3.5.0 3.5.1 3.5.1.1 3.5.1.2 3.5.2 3.5.2.1 3.5.3 3.5.4 3.5.5 3.5.6 3.5.6.1 3.5.6.2 3.5.6.3 3.6.0
jetformbuilder / includes / db-queries / query-conditions-builder.php
jetformbuilder / includes / db-queries Last commit date
constraints 3 years ago exceptions 3 years ago models 3 years ago traits 3 years ago views 3 years ago base-db-constraint.php 3 years ago base-db-model.php 3 years ago execution-builder.php 3 years ago query-builder.php 3 years ago query-cache-builder.php 3 years ago query-conditions-builder.php 3 years ago
query-conditions-builder.php
270 lines
1 <?php
2
3
4 namespace Jet_Form_Builder\Db_Queries;
5
6 use Jet_Form_Builder\Db_Queries\Traits\With_View;
7 use Jet_Form_Builder\Exceptions\Query_Builder_Exception;
8
9 /**
10 * @method Query_Conditions_Builder set_view( Views\View_Base $view )
11 *
12 * Class Query_Conditions_Builder
13 * @package Jet_Form_Builder\Db_Queries
14 */
15 class Query_Conditions_Builder {
16
17 const TYPE_EQUAL_STATIC = 'equal_static';
18 const TYPE_EQUAL = 'equal_column';
19 const TYPE_EQUAL_COLUMNS = 'equal_two_columns';
20 const TYPE_LIKE = 'like';
21 const TYPE_NOT_LIKE = 'not_like';
22 const TYPE_MORE_STATIC = 'more_static';
23 const TYPE_LESS_STATIC = 'less_static';
24 const TYPE_IN = 'in';
25
26 use With_View;
27
28 private $conditions = array(
29 array(
30 'type' => self::TYPE_EQUAL_STATIC,
31 'values' => array( 1, 1 ),
32 ),
33 );
34
35 /**
36 * @var array
37 */
38 private $current_condition;
39
40 public function get_types(): array {
41 return array(
42 self::TYPE_EQUAL_STATIC => array(
43 'callback' => array( $this, 'build_equal_static' ),
44 ),
45 self::TYPE_EQUAL => array(
46 'callback' => array( $this, 'build_equal_column' ),
47 ),
48 self::TYPE_EQUAL_COLUMNS => array(
49 'callback' => array( $this, 'build_equal_two_columns' ),
50 ),
51 self::TYPE_LIKE => array(
52 'callback' => array( $this, 'build_like' ),
53 ),
54 self::TYPE_NOT_LIKE => array(
55 'callback' => array( $this, 'build_not_like' ),
56 ),
57 self::TYPE_MORE_STATIC => array(
58 'callback' => array( $this, 'build_more_static' ),
59 ),
60 self::TYPE_LESS_STATIC => array(
61 'callback' => array( $this, 'build_less_static' ),
62 ),
63 self::TYPE_IN => array(
64 'callback' => array( $this, 'build_in' ),
65 ),
66 );
67 }
68
69 public function set_condition( array $condition ): Query_Conditions_Builder {
70 $this->conditions[] = $condition;
71
72 return $this;
73 }
74
75 public function set_conditions( array $conditions ): Query_Conditions_Builder {
76 foreach ( $conditions as $condition ) {
77 $this->set_condition( $condition );
78 }
79
80 return $this;
81 }
82
83 /**
84 * @throws Query_Builder_Exception
85 */
86 public function after_set_view() {
87 $this->set_conditions( $this->view()->conditions() );
88 }
89
90 /**
91 * @return string
92 */
93 public function result(): string {
94 if ( ! $this->conditions ) {
95 return '';
96 }
97
98 return 'WHERE ' . $this->prepare_conditions();
99 }
100
101 /**
102 * @param $conditions
103 *
104 * @return array
105 */
106 public function build_conditions_raw( array $conditions ): array {
107 $prepared = array();
108
109 foreach ( $conditions as $condition ) {
110 $this->current_condition = $condition;
111
112 $prepared[] = $this->prepare();
113 }
114
115 $this->current_condition = array();
116
117 return $prepared;
118 }
119
120 /**
121 * @return string
122 */
123 public function prepare_conditions(): string {
124 $prepared = $this->build_conditions_raw( $this->conditions );
125
126 return implode( "\r\n\tAND ", $prepared );
127 }
128
129 /**
130 * @return mixed
131 */
132 public function prepare() {
133 $type = $this->get_condition_type();
134
135 list ( $first, $second ) = $this->get_condition_values();
136
137 $callback = $this->get_types()[ $type ]['callback'];
138
139 return call_user_func( $callback, $first, $second );
140 }
141
142
143 /**
144 * @return array
145 */
146 public function current_condition(): array {
147 if ( empty( $this->current_condition ) ) {
148 _doing_it_wrong( __METHOD__, 'Current condition is empty', '2.0.0' );
149 wp_die();
150 }
151
152 return $this->current_condition;
153 }
154
155 /**
156 * @return string
157 */
158 private function get_condition_type(): string {
159 $condition = $this->current_condition();
160 $type = $condition['type'] ?? false;
161
162 if ( ! $type || ! in_array( $type, array_keys( $this->get_types() ), true ) ) {
163 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
164 _doing_it_wrong( __METHOD__, "Undefined condition type: $type", '2.0.0' );
165 wp_die();
166 }
167
168 return $type;
169 }
170
171 public function build_equal_static( $first, $second ): string {
172 return "{$first} = {$second}";
173 }
174
175 /**
176 * @param $column_name
177 * @param $second
178 *
179 * @return string
180 * @throws Query_Builder_Exception
181 */
182 public function build_equal_column( $column_name, $second ): string {
183 $second = esc_sql( $second );
184
185 return "{$this->view()->column( $column_name )} = '{$second}'";
186 }
187
188 /**
189 * @param $column_name
190 * @param $second
191 *
192 * @return string
193 * @throws Query_Builder_Exception
194 */
195 public function build_equal_two_columns( $column_name, $second ): string {
196 return "{$this->view()->column( $column_name )} = {$this->view()->column( $second )}";
197 }
198
199 /**
200 * @param $column_name
201 * @param $second
202 *
203 * @return string
204 * @throws Query_Builder_Exception
205 */
206 public function build_like( $column_name, $second ): string {
207 $second = esc_sql( $second );
208
209 return "{$this->view()->column( $column_name )} LIKE '%{$second}%'";
210 }
211
212 /**
213 * @param $column_name
214 * @param $second
215 *
216 * @return string
217 * @throws Query_Builder_Exception
218 */
219 public function build_not_like( $column_name, $second ): string {
220 $second = esc_sql( $second );
221
222 return "{$this->view()->column( $column_name )} LIKE '%{$second}%'";
223 }
224
225 /**
226 * @param $column_name
227 * @param $second
228 *
229 * @return string
230 * @throws Query_Builder_Exception
231 */
232 public function build_more_static( $column_name, $second ): string {
233 return "{$this->view()->column( $column_name )} > {$second}";
234 }
235
236 /**
237 * @param $column_name
238 * @param $second
239 *
240 * @return string
241 * @throws Query_Builder_Exception
242 */
243 public function build_less_static( $column_name, $second ): string {
244 return "{$this->view()->column( $column_name )} < {$second}";
245 }
246
247 /**
248 * @param $column_name
249 * @param $second
250 *
251 * @return string
252 * @throws Query_Builder_Exception
253 */
254 public function build_in( $column_name, $second ): string {
255 $right_part = implode( ', ', $second );
256
257 return "{$this->view()->column( $column_name )} IN ({$right_part})";
258 }
259
260 /**
261 * @return array
262 */
263 public function get_condition_values(): array {
264 $condition = $this->current_condition();
265
266 return $condition['values'] ?? array();
267 }
268
269 }
270