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-manager.php
151 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\Classes\Instance_Trait; |
| 8 | use Jet_Form_Builder\Classes\Repository\Repository_Pattern_Trait; |
| 9 | use Jet_Form_Builder\Exceptions\Parse_Exception; |
| 10 | use Jet_Form_Builder\Exceptions\Repository_Exception; |
| 11 | use Jet_Form_Builder\Request\Exceptions\Exclude_Field_Exception; |
| 12 | use Jet_Form_Builder\Request\Fields; |
| 13 | |
| 14 | /** |
| 15 | * @method static Parser_Manager instance() |
| 16 | * |
| 17 | * Class Parser_Manager |
| 18 | * @package Jet_Form_Builder\Request |
| 19 | */ |
| 20 | class Parser_Manager { |
| 21 | |
| 22 | const EMPTY_BLOCK_ERROR = '0'; |
| 23 | const NOT_FIELD_HAS_INNER = '1'; |
| 24 | const FIELD_HAS_INNER = '2'; |
| 25 | const IS_CONDITIONAL = '3'; |
| 26 | |
| 27 | use Instance_Trait; |
| 28 | use Repository_Pattern_Trait; |
| 29 | |
| 30 | private function __construct() { |
| 31 | $this->rep_install(); |
| 32 | } |
| 33 | |
| 34 | public function rep_instances(): array { |
| 35 | return apply_filters( |
| 36 | 'jet-form-builder/parsers-request/register', |
| 37 | array( |
| 38 | new Fields\Date_Field_Parser(), |
| 39 | new Fields\Repeater_Field_Parser(), |
| 40 | new Fields\Wysiwyg_Field_Parser(), |
| 41 | new Fields\Text_Field_Parser(), |
| 42 | new Fields\Repeater_Field_Parser(), |
| 43 | new Fields\Media_Field_Parser(), |
| 44 | new Fields\Datetime_Field_Parser(), |
| 45 | new Fields\Hidden_Field_Parser(), |
| 46 | ) |
| 47 | ); |
| 48 | } |
| 49 | |
| 50 | public function get_values_fields( $fields, Parser_Context $context ) { |
| 51 | $response = array(); |
| 52 | $this->get_values_fields_recursive( $response, $fields, $context ); |
| 53 | |
| 54 | return $response; |
| 55 | } |
| 56 | |
| 57 | public function get_values_fields_recursive( &$output, $fields, Parser_Context $context ) { |
| 58 | foreach ( $fields as $field ) { |
| 59 | try { |
| 60 | $context->set_field( $field ); |
| 61 | |
| 62 | $this->get_value_from_field( $output, $context ); |
| 63 | |
| 64 | } catch ( Parse_Exception $exception ) { |
| 65 | switch ( $exception->getMessage() ) { |
| 66 | |
| 67 | case self::IS_CONDITIONAL: |
| 68 | $this->get_values_fields_recursive( |
| 69 | $output, |
| 70 | $exception->get_inner(), |
| 71 | $context->set_inside_conditional( true ) |
| 72 | ); |
| 73 | break; |
| 74 | |
| 75 | case self::NOT_FIELD_HAS_INNER: |
| 76 | $this->get_values_fields_recursive( |
| 77 | $output, |
| 78 | $exception->get_inner(), |
| 79 | $context |
| 80 | ); |
| 81 | break; |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | |
| 88 | /** |
| 89 | * @param $output |
| 90 | * @param Parser_Context $context |
| 91 | */ |
| 92 | public function get_value_from_field( &$output, Parser_Context $context ) { |
| 93 | try { |
| 94 | $parser = $context->get_parser(); |
| 95 | } catch ( Repository_Exception $exception ) { |
| 96 | $context->save_to_request(); |
| 97 | |
| 98 | $output[ $context->get_name() ] = $context->get_value(); |
| 99 | |
| 100 | return; |
| 101 | } |
| 102 | |
| 103 | try { |
| 104 | $output[ $context->get_name() ] = $parser->get_parsed_value( $context ); |
| 105 | } catch ( Parse_Exception $exception ) { |
| 106 | $output = array_merge( $output, $exception->get_inner() ); |
| 107 | return; |
| 108 | |
| 109 | } catch ( Exclude_Field_Exception $exception ) { |
| 110 | return; |
| 111 | } |
| 112 | |
| 113 | $context->save_to_request(); |
| 114 | } |
| 115 | |
| 116 | public function save_to_request( $name, $type, $settings ) { |
| 117 | jet_fb_request_handler()->set_request_type( |
| 118 | array( |
| 119 | $name => $type, |
| 120 | ) |
| 121 | ); |
| 122 | jet_fb_request_handler()->set_request_attrs( |
| 123 | array( |
| 124 | $name => $settings, |
| 125 | ) |
| 126 | ); |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * @param $block_name |
| 131 | * |
| 132 | * @return bool |
| 133 | */ |
| 134 | public function isset_parser( $block_name ): bool { |
| 135 | $type = Block_Helper::delete_namespace( $block_name ); |
| 136 | |
| 137 | return $this->rep_isset_item( $type ); |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * @param $slug |
| 142 | * |
| 143 | * @return mixed |
| 144 | * @throws Repository_Exception |
| 145 | */ |
| 146 | public function get_parser( $slug ): Field_Data_Parser { |
| 147 | return $this->rep_clone_item( $slug ); |
| 148 | } |
| 149 | |
| 150 | } |
| 151 |