fields
5 years ago
field-data-parser.php
5 years ago
parser-manager.php
5 years ago
request-handler.php
5 years ago
field-data-parser.php
68 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace Jet_Form_Builder\Request; |
| 5 | |
| 6 | |
| 7 | use Jet_Form_Builder\Blocks\Modules\Fields_Errors\Error_Handler; |
| 8 | |
| 9 | abstract class Field_Data_Parser { |
| 10 | |
| 11 | protected $value; |
| 12 | protected $is_required = false; |
| 13 | protected $name = 'field_name'; |
| 14 | protected $block; |
| 15 | protected $settings; |
| 16 | protected $inner; |
| 17 | protected $request_handler; |
| 18 | |
| 19 | abstract public function type(); |
| 20 | |
| 21 | public function get_response() { |
| 22 | return $this->value; |
| 23 | } |
| 24 | |
| 25 | public function _is_custom_check() { |
| 26 | return false; |
| 27 | } |
| 28 | |
| 29 | final public function response() { |
| 30 | if ( $this->_is_required() || $this->_is_custom_check() ) { |
| 31 | $this->save_error(); |
| 32 | } |
| 33 | |
| 34 | return $this->get_response(); |
| 35 | } |
| 36 | |
| 37 | public function init( $value, $block ) { |
| 38 | $this->value = $value; |
| 39 | $this->block = $block; |
| 40 | |
| 41 | $this->settings = $this->block['attrs']; |
| 42 | $this->inner = $this->block['innerBlocks']; |
| 43 | |
| 44 | if ( isset( $this->settings['required'] ) ) { |
| 45 | $this->is_required = $this->settings['required']; |
| 46 | } |
| 47 | if ( isset( $this->settings['name'] ) ) { |
| 48 | $this->name = $this->settings['name']; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | |
| 53 | private function _is_required() { |
| 54 | return ( $this->is_required && empty( $this->value ) ); |
| 55 | } |
| 56 | |
| 57 | private function save_error() { |
| 58 | Error_Handler::instance()->add( |
| 59 | $this->type(), array( 'name' => $this->name, 'params' => $this->settings ) |
| 60 | ); |
| 61 | } |
| 62 | |
| 63 | public function set_request_handler( $request_handler ) { |
| 64 | $this->request_handler = $request_handler; |
| 65 | } |
| 66 | |
| 67 | |
| 68 | } |