exceptions
4 years ago
fields
4 years ago
field-data-parser.php
4 years ago
form-request-router.php
4 years ago
parser-manager.php
4 years ago
request-handler.php
4 years ago
request-router.php
4 years ago
field-data-parser.php
110 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace Jet_Form_Builder\Request; |
| 5 | |
| 6 | use Jet_Form_Builder\Blocks\Modules\Fields_Errors\Error_Handler; |
| 7 | use Jet_Form_Builder\Classes\Repository\Repository_Item_Instance_Trait; |
| 8 | use Jet_Form_Builder\Request\Exceptions\Exclude_Field_Exception; |
| 9 | |
| 10 | abstract class Field_Data_Parser implements Repository_Item_Instance_Trait { |
| 11 | |
| 12 | protected $value; |
| 13 | protected $is_required = false; |
| 14 | protected $name = 'field_name'; |
| 15 | protected $block; |
| 16 | protected $settings; |
| 17 | protected $inner; |
| 18 | protected $request_handler; |
| 19 | protected $inside_conditional; |
| 20 | |
| 21 | abstract public function type(); |
| 22 | |
| 23 | /** |
| 24 | * @return string |
| 25 | */ |
| 26 | public function rep_item_id() { |
| 27 | return $this->type(); |
| 28 | } |
| 29 | |
| 30 | public function get_response() { |
| 31 | return $this->value; |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * @param $value |
| 36 | * @param $block |
| 37 | * @param $inside_conditional |
| 38 | * |
| 39 | * @return mixed |
| 40 | * @throws Exclude_Field_Exception |
| 41 | */ |
| 42 | final public function get_parsed_value( $value, $block, $inside_conditional ) { |
| 43 | $this->init( $value, $block, $inside_conditional )->is_field_visible(); |
| 44 | |
| 45 | return $this->response(); |
| 46 | } |
| 47 | |
| 48 | final public function response() { |
| 49 | if ( $this->has_error() ) { |
| 50 | $this->save_error(); |
| 51 | } |
| 52 | |
| 53 | return $this->get_response(); |
| 54 | } |
| 55 | |
| 56 | public function parse_value( $value ) { |
| 57 | return $value; |
| 58 | } |
| 59 | |
| 60 | public function init( $value, $block, $inside_conditional ): Field_Data_Parser { |
| 61 | $this->block = $block; |
| 62 | $this->inside_conditional = $inside_conditional; |
| 63 | $this->settings = $this->block['attrs']; |
| 64 | $this->inner = $this->block['innerBlocks']; |
| 65 | |
| 66 | if ( isset( $this->settings['required'] ) ) { |
| 67 | $this->is_required = $this->settings['required']; |
| 68 | } |
| 69 | if ( isset( $this->settings['name'] ) ) { |
| 70 | $this->name = $this->settings['name']; |
| 71 | } |
| 72 | $this->value = $this->parse_value( $value ); |
| 73 | |
| 74 | return $this; |
| 75 | } |
| 76 | |
| 77 | protected function has_error(): bool { |
| 78 | return ( ! $this->inside_conditional && $this->is_required && empty( $this->value ) ); |
| 79 | } |
| 80 | |
| 81 | protected function save_error() { |
| 82 | Error_Handler::instance()->add( |
| 83 | $this->type(), |
| 84 | array( |
| 85 | 'name' => $this->name, |
| 86 | 'params' => $this->settings, |
| 87 | ) |
| 88 | ); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * @throws Exclude_Field_Exception |
| 93 | */ |
| 94 | protected function is_field_visible() { |
| 95 | $visibility = $this->settings['visibility'] ?? true; |
| 96 | |
| 97 | // If is visible for logged in users and user is logged in - show field. |
| 98 | if ( 'logged_id' === $visibility && ! is_user_logged_in() ) { |
| 99 | throw new Exclude_Field_Exception(); |
| 100 | } |
| 101 | |
| 102 | // If is visible for not logged in users and user is not logged in - show field. |
| 103 | if ( 'not_logged_in' === $visibility && is_user_logged_in() ) { |
| 104 | throw new Exclude_Field_Exception(); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | |
| 109 | } |
| 110 |