condition-instance.php
319 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace Jet_Form_Builder\Actions\Conditions; |
| 5 | |
| 6 | use Jet_Form_Builder\Exceptions\Condition_Silence_Exception; |
| 7 | use Jet_Form_Builder\Presets\Types\Dynamic_Preset; |
| 8 | |
| 9 | class Condition_Instance { |
| 10 | |
| 11 | private $type; |
| 12 | private $events; |
| 13 | |
| 14 | private $operator; |
| 15 | private $must_be_true = true; |
| 16 | private $field_name; |
| 17 | private $compare; |
| 18 | private $compare_value_format; |
| 19 | |
| 20 | private function get_manager(): Condition_Manager { |
| 21 | return jet_fb_action_handler()->get_current_condition_manager(); |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * @param $condition |
| 26 | * |
| 27 | * @return $this |
| 28 | */ |
| 29 | public function set_condition( $condition ): Condition_Instance { |
| 30 | $this->set_type( $condition['type'] ?? '' ); |
| 31 | |
| 32 | if ( ! $this->is_field_compare() ) { |
| 33 | $this->set_events( $condition['events'] ?? array() ); |
| 34 | |
| 35 | return $this; |
| 36 | } |
| 37 | |
| 38 | $this->set_operator( $condition['operator'] ?? false ); |
| 39 | $this->set_field( $condition['field'] ?? '' ); |
| 40 | $this->set_compare_value_format( $condition['compare_value_format'] ?? '' ); |
| 41 | $this->set_compare( $this->get_parsed_value( $condition['default'] ?? '' ) ); |
| 42 | $this->set_must_be( $condition['execute'] ?? '' ); |
| 43 | |
| 44 | return $this; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * @return bool |
| 49 | */ |
| 50 | public function is_correct(): bool { |
| 51 | $this->sanitize_operator(); |
| 52 | |
| 53 | $result = $this->check(); |
| 54 | |
| 55 | return $this->get_must_be() ? $result : ! $result; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * @throws Condition_Silence_Exception |
| 60 | */ |
| 61 | public function is_correct_with_throw() { |
| 62 | $this->end_condition( |
| 63 | $this->is_correct(), |
| 64 | 'The condition was not met.', |
| 65 | get_object_vars( $this ) |
| 66 | ); |
| 67 | } |
| 68 | |
| 69 | public function get_compare_value_transformer() { |
| 70 | return $this->compare_value_format; |
| 71 | } |
| 72 | |
| 73 | public function set_compare_value_format( $format ): Condition_Instance { |
| 74 | $this->compare_value_format = (string) $format; |
| 75 | |
| 76 | return $this; |
| 77 | } |
| 78 | |
| 79 | |
| 80 | public function set_operator( string $operator ): Condition_Instance { |
| 81 | $this->operator = $operator; |
| 82 | |
| 83 | return $this; |
| 84 | } |
| 85 | |
| 86 | public function sanitize_operator(): Condition_Instance { |
| 87 | $operators = $this->get_manager()->get_operators_list(); |
| 88 | |
| 89 | if ( ! $this->operator || ! in_array( $this->operator, $operators, true ) ) { |
| 90 | $this->operator = ''; |
| 91 | } |
| 92 | |
| 93 | return $this; |
| 94 | } |
| 95 | |
| 96 | public function get_operator() { |
| 97 | return $this->operator; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * @return mixed |
| 102 | */ |
| 103 | public function get_events() { |
| 104 | return $this->events; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * @param mixed $events |
| 109 | */ |
| 110 | public function set_events( array $events ) { |
| 111 | $this->events = $events; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * @param $field_name |
| 116 | */ |
| 117 | public function set_field( $field_name ) { |
| 118 | $this->field_name = $field_name; |
| 119 | } |
| 120 | |
| 121 | public function get_field_name() { |
| 122 | return $this->field_name; |
| 123 | } |
| 124 | |
| 125 | public function get_field_value() { |
| 126 | return jet_fb_action_handler()->request_data[ $this->get_field_name() ] ?? false; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * @return mixed |
| 131 | */ |
| 132 | public function get_type() { |
| 133 | return $this->type; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * @param mixed $type |
| 138 | */ |
| 139 | public function set_type( string $type ) { |
| 140 | $this->type = empty( $type ) ? 'field' : $type; |
| 141 | } |
| 142 | |
| 143 | public function is_field_compare(): bool { |
| 144 | return 'field' === $this->get_type(); |
| 145 | } |
| 146 | |
| 147 | public function set_compare( $compare ): Condition_Instance { |
| 148 | return $this->set_compare_raw( $compare )->transform_compare_value(); |
| 149 | } |
| 150 | |
| 151 | public function set_compare_raw( $raw_compare ): Condition_Instance { |
| 152 | $this->compare = $raw_compare; |
| 153 | |
| 154 | return $this; |
| 155 | } |
| 156 | |
| 157 | public function transform_compare_value(): Condition_Instance { |
| 158 | $transformer = $this->get_compare_value_transformer(); |
| 159 | |
| 160 | if ( ! $transformer ) { |
| 161 | return $this; |
| 162 | } |
| 163 | |
| 164 | $transformer = $this->get_manager()->get_transformers_find( |
| 165 | 'value', |
| 166 | $transformer |
| 167 | ); |
| 168 | |
| 169 | $callback = $transformer['callback'] ?? false; |
| 170 | |
| 171 | if ( ! is_callable( $callback ) ) { |
| 172 | return $this; |
| 173 | } |
| 174 | |
| 175 | $this->set_compare_value_with_callback( $callback ); |
| 176 | |
| 177 | return $this; |
| 178 | } |
| 179 | |
| 180 | public function set_compare_value_with_callback( $transformer_callback ) { |
| 181 | $operators_need_explode = $this->get_manager()->get_operators_filter( 'need_explode', true ); |
| 182 | |
| 183 | if ( ! in_array( $this->get_operator(), $operators_need_explode, true ) ) { |
| 184 | $this->set_compare_raw( call_user_func( $transformer_callback, $this->get_compare() ) ); |
| 185 | |
| 186 | return; |
| 187 | } |
| 188 | $this->set_compare_raw( array_map( $transformer_callback, $this->get_compare_as_array() ) ); |
| 189 | } |
| 190 | |
| 191 | public function get_compare() { |
| 192 | return $this->compare; |
| 193 | } |
| 194 | |
| 195 | public function set_must_be( $must_be ): Condition_Instance { |
| 196 | $this->must_be_true = (bool) $must_be; |
| 197 | |
| 198 | return $this; |
| 199 | } |
| 200 | |
| 201 | public function get_must_be(): bool { |
| 202 | return $this->must_be_true; |
| 203 | } |
| 204 | |
| 205 | public function get_parsed_value( $maybe_json_string ) { |
| 206 | return ( new Dynamic_Preset() )->parse_json( $maybe_json_string ); |
| 207 | } |
| 208 | |
| 209 | |
| 210 | /** |
| 211 | * @param $field_name |
| 212 | * |
| 213 | * @return mixed |
| 214 | */ |
| 215 | public function get_request_field( $field_name ) { |
| 216 | $handler = jet_fb_action_handler(); |
| 217 | |
| 218 | if ( isset( $handler->request_data[ $field_name ] ) ) { |
| 219 | return $handler->request_data[ $field_name ]; |
| 220 | } |
| 221 | |
| 222 | return false; |
| 223 | } |
| 224 | |
| 225 | |
| 226 | public function get_compare_as_array(): array { |
| 227 | $compare = $this->get_compare(); |
| 228 | |
| 229 | if ( is_array( $compare ) ) { |
| 230 | return $compare; |
| 231 | } |
| 232 | |
| 233 | $this->compare = array_map( 'trim', explode( ',', $compare ) ); |
| 234 | |
| 235 | return $this->compare; |
| 236 | } |
| 237 | |
| 238 | /** |
| 239 | * @return bool |
| 240 | */ |
| 241 | public function check(): bool { |
| 242 | switch ( $this->get_operator() ) { |
| 243 | case 'equal': |
| 244 | return ( (string) $this->get_field_value() ) === ( (string) $this->get_compare() ); |
| 245 | case 'greater': |
| 246 | return $this->get_field_value() > $this->get_compare(); |
| 247 | case 'less': |
| 248 | return $this->get_field_value() < $this->get_compare(); |
| 249 | |
| 250 | case 'between': |
| 251 | $field = $this->get_field_value(); |
| 252 | $compare_values = $this->get_compare_as_array(); |
| 253 | |
| 254 | if ( count( $compare_values ) !== 2 ) { |
| 255 | return false; |
| 256 | } |
| 257 | |
| 258 | return ( $compare_values[0] < $field && $compare_values[1] > $field ); |
| 259 | case 'one_of': |
| 260 | return $this->check_one_of(); |
| 261 | case 'contain': |
| 262 | return ( strpos( $this->get_field_value(), $this->get_compare() ) !== false ); |
| 263 | default: |
| 264 | return apply_filters( 'jet-form-builder/actions/process-condition', false, $this ); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | protected function check_one_of(): bool { |
| 269 | $field = $this->get_field_value(); |
| 270 | $compare_values = $this->get_compare_as_array(); |
| 271 | |
| 272 | if ( ! is_array( $compare_values ) ) { |
| 273 | return false; |
| 274 | } |
| 275 | |
| 276 | // then value is from checkbox field |
| 277 | if ( is_array( $field ) ) { |
| 278 | return $this->check_one_of_list(); |
| 279 | } |
| 280 | |
| 281 | // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict |
| 282 | return in_array( $field, $compare_values ); |
| 283 | } |
| 284 | |
| 285 | public function check_one_of_list(): bool { |
| 286 | $field = $this->get_field_value(); |
| 287 | $compare_values = $this->get_compare_as_array(); |
| 288 | |
| 289 | foreach ( $field as $current ) { |
| 290 | if ( in_array( $current, $compare_values, true ) ) { |
| 291 | return true; |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | return false; |
| 296 | } |
| 297 | |
| 298 | /** |
| 299 | * @param $success |
| 300 | * @param mixed ...$additional |
| 301 | * |
| 302 | * @throws Condition_Silence_Exception |
| 303 | */ |
| 304 | public function end_condition( $success, ...$additional ) { |
| 305 | $this->get_manager()->throw_by_method( $success, ...$additional ); |
| 306 | } |
| 307 | |
| 308 | /** |
| 309 | * @param mixed ...$additional |
| 310 | * |
| 311 | * @throws Condition_Silence_Exception |
| 312 | */ |
| 313 | public function error( ...$additional ) { |
| 314 | $this->end_condition( false, ...$additional ); |
| 315 | } |
| 316 | |
| 317 | |
| 318 | } |
| 319 |