functions
3 years ago
operators
3 years ago
render-states
3 years ago
condition-item.php
3 years ago
condition-manager.php
3 years ago
condition-response-object.php
3 years ago
functions.php
3 years ago
operators.php
3 years ago
render-state.php
3 years ago
render-states-collection.php
3 years ago
condition-item.php
128 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace Jet_Form_Builder\Blocks\Conditional_Block; |
| 5 | |
| 6 | use Jet_Form_Builder\Blocks\Conditional_Block\Functions\Base_Function; |
| 7 | use Jet_Form_Builder\Blocks\Conditional_Block\Operators\Base_Operator; |
| 8 | use Jet_Form_Builder\Blocks\Exceptions\Condition_Exception; |
| 9 | use Jet_Form_Builder\Blocks\Exceptions\Render_Empty_Field; |
| 10 | use Jet_Form_Builder\Classes\Arrayable\Arrayable; |
| 11 | use Jet_Form_Builder\Exceptions\Repository_Exception; |
| 12 | use Jet_Form_Builder\Presets\Types\Dynamic_Preset; |
| 13 | |
| 14 | class Condition_Item implements Arrayable { |
| 15 | |
| 16 | private $type; |
| 17 | private $value; |
| 18 | private $field; |
| 19 | private $operator; |
| 20 | |
| 21 | private $parsed_value; |
| 22 | |
| 23 | /** |
| 24 | * All condition options |
| 25 | * |
| 26 | * @var array |
| 27 | */ |
| 28 | public $base; |
| 29 | |
| 30 | /** |
| 31 | * Condition_Item constructor. |
| 32 | * |
| 33 | * @param array $condition |
| 34 | * |
| 35 | * @throws Repository_Exception |
| 36 | */ |
| 37 | public function __construct( array $condition ) { |
| 38 | $this->base = $condition; |
| 39 | $this->value = $condition['value'] ?? ''; |
| 40 | $this->field = $condition['field'] ?? ''; |
| 41 | $this->type = $condition['type'] ?? ''; |
| 42 | $this->operator = $condition['operator'] ?? ''; |
| 43 | |
| 44 | Condition_Manager::instance()->get_functions()->isset_function( $this->type ); |
| 45 | Condition_Manager::instance()->get_operators()->isset_operator( $this->operator ); |
| 46 | } |
| 47 | |
| 48 | public function to_array(): array { |
| 49 | $base = array( |
| 50 | 'value' => $this->get_parsed_value(), |
| 51 | 'type' => $this->get_type(), |
| 52 | 'field' => $this->get_field(), |
| 53 | 'operator' => $this->get_operator(), |
| 54 | ); |
| 55 | $base = array_merge( |
| 56 | $base, |
| 57 | $this->get_operator_object()->to_response( $base, $this ) |
| 58 | ); |
| 59 | |
| 60 | return array_merge( |
| 61 | $base, |
| 62 | $this->get_function_object()->to_response( $base, $this ) |
| 63 | ); |
| 64 | } |
| 65 | |
| 66 | public function get_function_object(): Base_Function { |
| 67 | return Condition_Manager::instance()->get_functions()->rep_get_item_or_die( $this->type ); |
| 68 | } |
| 69 | |
| 70 | public function get_operator_object(): Base_Operator { |
| 71 | return Condition_Manager::instance()->get_operators()->rep_get_item_or_die( $this->operator ); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * @return array|string |
| 76 | */ |
| 77 | public function get_parsed_value() { |
| 78 | if ( ! is_null( $this->parsed_value ) ) { |
| 79 | return $this->parsed_value; |
| 80 | } |
| 81 | |
| 82 | $value = ( new Dynamic_Preset() )->parse_json( $this->value ); |
| 83 | |
| 84 | $this->parsed_value = $this->parse_string( $value ); |
| 85 | |
| 86 | return $this->parsed_value; |
| 87 | } |
| 88 | |
| 89 | private function parse_string( string $value ) { |
| 90 | $value_in_array = explode( ',', $value ); |
| 91 | |
| 92 | if ( 1 === count( $value_in_array ) ) { |
| 93 | return $value; |
| 94 | } |
| 95 | |
| 96 | return array_map( 'trim', $value_in_array ); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * @return string |
| 101 | */ |
| 102 | public function get_type(): string { |
| 103 | return $this->type; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * @return string |
| 108 | */ |
| 109 | public function get_value(): string { |
| 110 | return $this->value; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * @return string |
| 115 | */ |
| 116 | public function get_field(): string { |
| 117 | return $this->field; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * @return string |
| 122 | */ |
| 123 | public function get_operator(): string { |
| 124 | return $this->operator; |
| 125 | } |
| 126 | |
| 127 | } |
| 128 |