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