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
request-tools.php
3 years ago
parser-context.php
202 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 | * @var bool |
| 27 | * |
| 28 | * @since 3.0.4 |
| 29 | */ |
| 30 | protected $guest_allow = false; |
| 31 | |
| 32 | /** |
| 33 | * @return mixed |
| 34 | */ |
| 35 | public function get_file() { |
| 36 | return $this->files[ $this->name ] ?? false; |
| 37 | } |
| 38 | |
| 39 | public function get_value() { |
| 40 | return $this->request[ $this->name ] ?? ''; |
| 41 | } |
| 42 | |
| 43 | public function isset_parser(): bool { |
| 44 | return Parser_Manager::instance()->isset_parser( $this->field_type ); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * @return Field_Data_Parser |
| 49 | * @throws Repository_Exception |
| 50 | */ |
| 51 | public function get_parser(): Field_Data_Parser { |
| 52 | return Parser_Manager::instance()->get_parser( $this->field_type ); |
| 53 | } |
| 54 | |
| 55 | public function save_to_request() { |
| 56 | Parser_Manager::instance()->save_to_request( $this->name, $this->field_type, $this->settings ); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * @param array $field |
| 61 | * |
| 62 | * @return $this |
| 63 | * @throws Parse_Exception |
| 64 | */ |
| 65 | public function set_field( array $field ): Parser_Context { |
| 66 | $this->validate_field( $field ); |
| 67 | |
| 68 | return $this->set_raw_field( $field ); |
| 69 | } |
| 70 | |
| 71 | public function set_raw_field( array $field ): Parser_Context { |
| 72 | $this->block = $field; |
| 73 | $this->settings = $field['attrs']; |
| 74 | $this->name = $field['attrs']['name'] ?? 'field_name'; |
| 75 | $this->field_type = Block_Helper::delete_namespace( $field['blockName'] ); |
| 76 | |
| 77 | // reset |
| 78 | $this->guest_allow = false; |
| 79 | |
| 80 | return $this; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * @param array $field |
| 85 | * |
| 86 | * @throws Parse_Exception |
| 87 | */ |
| 88 | protected function validate_field( array $field ) { |
| 89 | if ( empty( $field['blockName'] ) ) { |
| 90 | throw new Parse_Exception( Parser_Manager::EMPTY_BLOCK_ERROR ); |
| 91 | } |
| 92 | |
| 93 | if ( empty( $field['innerBlocks'] ) ) { |
| 94 | return; |
| 95 | } |
| 96 | |
| 97 | if ( strpos( $field['blockName'], 'conditional-block' ) ) { |
| 98 | throw new Parse_Exception( Parser_Manager::IS_CONDITIONAL, $field['innerBlocks'] ); |
| 99 | } |
| 100 | if ( ! Parser_Manager::instance()->isset_parser( $field['blockName'] ) ) { |
| 101 | throw new Parse_Exception( Parser_Manager::NOT_FIELD_HAS_INNER, $field['innerBlocks'] ); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | public function get_default_value() { |
| 106 | /** @var Base $block */ |
| 107 | $block = jet_form_builder()->blocks->get_field_by_name( $this->field_type ); |
| 108 | |
| 109 | // the exception will never be thrown |
| 110 | $block->set_block_data( $this->settings ); |
| 111 | $block->set_context( $this->context ); |
| 112 | $block->set_preset(); |
| 113 | |
| 114 | return $block->block_attrs['default']; |
| 115 | } |
| 116 | |
| 117 | public function set_request_context( array $request ): Parser_Context { |
| 118 | $this->request = $request; |
| 119 | |
| 120 | return $this; |
| 121 | } |
| 122 | |
| 123 | |
| 124 | public function set_files_context( $files ): Parser_Context { |
| 125 | $this->files = $files; |
| 126 | |
| 127 | return $this; |
| 128 | } |
| 129 | |
| 130 | public function get_request(): array { |
| 131 | return $this->request; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * @return mixed |
| 136 | */ |
| 137 | public function get_files() { |
| 138 | return $this->files; |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * @return mixed |
| 143 | */ |
| 144 | public function get_block() { |
| 145 | return $this->block; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * @return mixed |
| 150 | */ |
| 151 | public function get_settings() { |
| 152 | return $this->settings; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * @return string |
| 157 | */ |
| 158 | public function get_name(): string { |
| 159 | return $this->name; |
| 160 | } |
| 161 | |
| 162 | public function is_inside_conditional(): bool { |
| 163 | return $this->inside_conditional; |
| 164 | } |
| 165 | |
| 166 | public function set_inside_conditional( bool $inside_conditional ): Parser_Context { |
| 167 | $this->inside_conditional = $inside_conditional; |
| 168 | |
| 169 | return $this; |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * @since 3.0.4 |
| 174 | */ |
| 175 | public function get_class_name(): string { |
| 176 | return $this->settings['class_name'] ?? ''; |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * @since 3.0.4 |
| 181 | */ |
| 182 | public function update_setting( string $name, $value ) { |
| 183 | $this->settings[ $name ] = $value; |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * @since 3.0.4 |
| 188 | */ |
| 189 | public function allow_for_guest() { |
| 190 | $this->guest_allow = true; |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * @since 3.0.4 |
| 195 | */ |
| 196 | public function is_allowed_for_guest(): bool { |
| 197 | return $this->guest_allow; |
| 198 | } |
| 199 | |
| 200 | |
| 201 | } |
| 202 |