exceptions
3 years ago
fields
3 years ago
field-data-parser.php
3 years ago
file-uploader.php
3 years ago
form-request-router.php
3 years ago
parser-context.php
3 years ago
parser-manager.php
3 years ago
request-handler.php
3 years ago
request-router.php
3 years ago
parser-context.php
160 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace Jet_Form_Builder\Request; |
| 5 | |
| 6 | use Jet_Form_Builder\Blocks\Block_Helper; |
| 7 | use Jet_Form_Builder\Blocks\Types\Base; |
| 8 | use Jet_Form_Builder\Exceptions\Parse_Exception; |
| 9 | use Jet_Form_Builder\Exceptions\Repository_Exception; |
| 10 | |
| 11 | class Parser_Context { |
| 12 | |
| 13 | protected $block; |
| 14 | protected $settings; |
| 15 | protected $name = ''; |
| 16 | protected $field_type; |
| 17 | |
| 18 | protected $request; |
| 19 | protected $files; |
| 20 | |
| 21 | protected $inside_conditional = false; |
| 22 | |
| 23 | protected $context = array(); |
| 24 | |
| 25 | /** |
| 26 | * @return mixed |
| 27 | */ |
| 28 | public function get_file() { |
| 29 | return $this->files[ $this->name ] ?? false; |
| 30 | } |
| 31 | |
| 32 | public function get_value() { |
| 33 | return $this->request[ $this->name ] ?? ''; |
| 34 | } |
| 35 | |
| 36 | public function isset_parser(): bool { |
| 37 | return Parser_Manager::instance()->isset_parser( $this->field_type ); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * @return Field_Data_Parser |
| 42 | * @throws Repository_Exception |
| 43 | */ |
| 44 | public function get_parser(): Field_Data_Parser { |
| 45 | return Parser_Manager::instance()->get_parser( $this->field_type ); |
| 46 | } |
| 47 | |
| 48 | public function save_to_request() { |
| 49 | Parser_Manager::instance()->save_to_request( $this->name, $this->field_type, $this->settings ); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * @param array $field |
| 54 | * |
| 55 | * @return $this |
| 56 | * @throws Parse_Exception |
| 57 | */ |
| 58 | public function set_field( array $field ): Parser_Context { |
| 59 | $this->validate_field( $field ); |
| 60 | |
| 61 | $this->block = $field; |
| 62 | $this->settings = $field['attrs']; |
| 63 | $this->name = $field['attrs']['name'] ?? 'field_name'; |
| 64 | $this->field_type = Block_Helper::delete_namespace( $field['blockName'] ); |
| 65 | |
| 66 | return $this; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * @param array $field |
| 71 | * |
| 72 | * @throws Parse_Exception |
| 73 | */ |
| 74 | protected function validate_field( array $field ) { |
| 75 | if ( empty( $field['blockName'] ) ) { |
| 76 | throw new Parse_Exception( Parser_Manager::EMPTY_BLOCK_ERROR ); |
| 77 | } |
| 78 | |
| 79 | if ( empty( $field['innerBlocks'] ) ) { |
| 80 | return; |
| 81 | } |
| 82 | |
| 83 | if ( strpos( $field['blockName'], 'conditional-block' ) ) { |
| 84 | throw new Parse_Exception( Parser_Manager::IS_CONDITIONAL, $field['innerBlocks'] ); |
| 85 | } |
| 86 | if ( ! Parser_Manager::instance()->isset_parser( $field['blockName'] ) ) { |
| 87 | throw new Parse_Exception( Parser_Manager::NOT_FIELD_HAS_INNER, $field['innerBlocks'] ); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | public function get_default_value() { |
| 92 | /** @var Base $block */ |
| 93 | $block = jet_form_builder()->blocks->get_field_by_name( $this->field_type ); |
| 94 | |
| 95 | // the exception will never be thrown |
| 96 | $block->set_block_data( $this->settings ); |
| 97 | $block->set_context( $this->context ); |
| 98 | $block->set_preset(); |
| 99 | |
| 100 | return $block->block_attrs['default']; |
| 101 | } |
| 102 | |
| 103 | public function set_request_context( array $request ): Parser_Context { |
| 104 | $this->request = $request; |
| 105 | |
| 106 | return $this; |
| 107 | } |
| 108 | |
| 109 | |
| 110 | public function set_files_context( $files ): Parser_Context { |
| 111 | $this->files = $files; |
| 112 | |
| 113 | return $this; |
| 114 | } |
| 115 | |
| 116 | public function get_request(): array { |
| 117 | return $this->request; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * @return mixed |
| 122 | */ |
| 123 | public function get_files() { |
| 124 | return $this->files; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * @return mixed |
| 129 | */ |
| 130 | public function get_block() { |
| 131 | return $this->block; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * @return mixed |
| 136 | */ |
| 137 | public function get_settings() { |
| 138 | return $this->settings; |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * @return string |
| 143 | */ |
| 144 | public function get_name(): string { |
| 145 | return $this->name; |
| 146 | } |
| 147 | |
| 148 | public function is_inside_conditional(): bool { |
| 149 | return $this->inside_conditional; |
| 150 | } |
| 151 | |
| 152 | public function set_inside_conditional( bool $inside_conditional ): Parser_Context { |
| 153 | $this->inside_conditional = $inside_conditional; |
| 154 | |
| 155 | return $this; |
| 156 | } |
| 157 | |
| 158 | |
| 159 | } |
| 160 |