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-manager.php
80 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace Jet_Form_Builder\Blocks\Conditional_Block; |
| 5 | |
| 6 | use Jet_Form_Builder\Blocks\Exceptions\Condition_Exception; |
| 7 | use Jet_Form_Builder\Blocks\Exceptions\Render_Empty_Field; |
| 8 | use Jet_Form_Builder\Classes\Arrayable\Arrayable; |
| 9 | use Jet_Form_Builder\Classes\Instance_Trait; |
| 10 | use Jet_Form_Builder\Classes\Tools; |
| 11 | use Jet_Form_Builder\Exceptions\Repository_Exception; |
| 12 | |
| 13 | /** |
| 14 | * @method static Condition_Manager instance() |
| 15 | * |
| 16 | * Class Condition_Manager |
| 17 | * @package Jet_Form_Builder\Blocks\Conditional_Block |
| 18 | */ |
| 19 | class Condition_Manager implements Arrayable { |
| 20 | |
| 21 | use Instance_Trait; |
| 22 | |
| 23 | private $operators; |
| 24 | private $functions; |
| 25 | |
| 26 | private function __construct() { |
| 27 | $this->operators = new Operators(); |
| 28 | $this->functions = new Functions(); |
| 29 | } |
| 30 | |
| 31 | public function prepare( array $conditions ): array { |
| 32 | $response = array(); |
| 33 | |
| 34 | foreach ( $conditions as $condition ) { |
| 35 | try { |
| 36 | $item = new Condition_Item( $condition ); |
| 37 | } catch ( Repository_Exception $exception ) { |
| 38 | continue; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Catch Condition_Exception on this level, |
| 43 | * because in future we can implement "OR" operator |
| 44 | * between all conditions. |
| 45 | * |
| 46 | * Here we could access the global option |
| 47 | */ |
| 48 | try { |
| 49 | $response[] = $item->to_array(); |
| 50 | } catch ( Condition_Exception $exception ) { |
| 51 | /** |
| 52 | * This exception catches in |
| 53 | * |
| 54 | * @see \Jet_Form_Builder\Blocks\Types\Base::render_callback_field |
| 55 | */ |
| 56 | throw new Render_Empty_Field( 'This is temporary.' ); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | return $response; |
| 61 | } |
| 62 | |
| 63 | public function to_array(): array { |
| 64 | return array( |
| 65 | 'functions' => Tools::with_placeholder( $this->get_functions()->to_array() ), |
| 66 | 'operators' => Tools::with_placeholder( $this->get_operators()->to_array() ), |
| 67 | 'render_states' => Tools::with_placeholder( Render_State::instance()->to_array() ), |
| 68 | ); |
| 69 | } |
| 70 | |
| 71 | public function get_operators(): Operators { |
| 72 | return $this->operators; |
| 73 | } |
| 74 | |
| 75 | public function get_functions(): Functions { |
| 76 | return $this->functions; |
| 77 | } |
| 78 | |
| 79 | } |
| 80 |