exceptions
3 years ago
form-record
3 years ago
post-modification
3 years ago
update-user
3 years ago
abstract-modifier.php
3 years ago
base-modifier-action.php
3 years ago
base-object-property.php
3 years ago
object-dynamic-property.php
3 years ago
object-properties-collection.php
3 years ago
abstract-modifier.php
204 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Jet_Form_Builder\Actions\Methods; |
| 4 | |
| 5 | use Jet_Form_Builder\Actions\Methods\Exceptions\Modifier_Exclude_Property; |
| 6 | use Jet_Form_Builder\Actions\Types\Insert_Post; |
| 7 | use Jet_Form_Builder\Classes\Arrayable\Array_Tools; |
| 8 | use Jet_Form_Builder\Classes\Arrayable\Collection; |
| 9 | use Jet_Form_Builder\Classes\Repository\Repository_Pattern_Trait; |
| 10 | use Jet_Form_Builder\Classes\Tools; |
| 11 | use Jet_Form_Builder\Exceptions\Action_Exception; |
| 12 | use Jet_Form_Builder\Exceptions\Silence_Exception; |
| 13 | use Jet_Theme_Core\Template_Conditions\Page_404; |
| 14 | |
| 15 | abstract class Abstract_Modifier { |
| 16 | |
| 17 | public $source_arr = array(); |
| 18 | public $fields_map = array(); |
| 19 | protected $request = array(); |
| 20 | |
| 21 | /** @var Object_Properties_Collection */ |
| 22 | public $properties; |
| 23 | |
| 24 | /** @var Collection */ |
| 25 | public $actions; |
| 26 | |
| 27 | /** @var Base_Modifier_Action */ |
| 28 | protected $action; |
| 29 | |
| 30 | abstract protected function get_actions(): Collection; |
| 31 | |
| 32 | abstract protected function get_properties(): Object_Properties_Collection; |
| 33 | |
| 34 | public function __construct() { |
| 35 | $this->properties = $this->get_properties(); |
| 36 | // install actions |
| 37 | $this->actions = $this->get_actions(); |
| 38 | } |
| 39 | |
| 40 | public function run() { |
| 41 | foreach ( $this->request as $key => $value ) { |
| 42 | $this->attach_item( $key, $value ); |
| 43 | } |
| 44 | |
| 45 | $this->attach_properties(); |
| 46 | $this->do_action(); |
| 47 | |
| 48 | /** @var Base_Object_Property $property */ |
| 49 | foreach ( $this->properties as $property ) { |
| 50 | $property->do_after( $this ); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | protected function attach_item( $field_name, $value ) { |
| 55 | $key = $this->get_field_key( $field_name ); |
| 56 | |
| 57 | if ( ! $key ) { |
| 58 | return; |
| 59 | } |
| 60 | |
| 61 | /** @var Base_Object_Property $property */ |
| 62 | foreach ( $this->properties->get_by_id( $key ) as $property ) { |
| 63 | $property->set_value( $key, $value, $this ); |
| 64 | } |
| 65 | |
| 66 | if ( $this->properties->has_by_id( $key ) ) { |
| 67 | return; |
| 68 | } |
| 69 | |
| 70 | /** @var Base_Object_Property $item */ |
| 71 | foreach ( $this->properties->get_dynamic() as $item ) { |
| 72 | $item->set_value( $key, $value, $this ); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | protected function attach_properties() { |
| 77 | /** @var Base_Object_Property $property */ |
| 78 | foreach ( $this->properties as $property ) { |
| 79 | try { |
| 80 | $value = $property->get_value( $this ); |
| 81 | } catch ( Silence_Exception $exception ) { |
| 82 | continue; |
| 83 | } |
| 84 | |
| 85 | if ( ! is_array( $value ) || ! $property->is_merge_value() ) { |
| 86 | $this->source_arr[ $property->get_attach_id() ] = $value; |
| 87 | |
| 88 | continue; |
| 89 | } |
| 90 | |
| 91 | $this->source_arr = array_merge( |
| 92 | $this->source_arr, |
| 93 | $value |
| 94 | ); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | protected function do_action() { |
| 99 | $this->action = $this->get_action(); |
| 100 | |
| 101 | $this->action->set_modifier( $this ); |
| 102 | $this->action->do_action(); |
| 103 | $this->action->do_after(); |
| 104 | } |
| 105 | |
| 106 | protected function get_supported_action(): Base_Modifier_Action { |
| 107 | /** @var Base_Modifier_Action $current */ |
| 108 | foreach ( Array_Tools::reverse( $this->actions ) as $current ) { |
| 109 | if ( ! $current::is_supported( $this ) ) { |
| 110 | continue; |
| 111 | } |
| 112 | |
| 113 | return $current; |
| 114 | } |
| 115 | |
| 116 | wp_die( 'Something went wrong' ); |
| 117 | } |
| 118 | |
| 119 | public function set( string $property, $value, $key = null ): Abstract_Modifier { |
| 120 | /** @var Base_Object_Property $prop_item */ |
| 121 | $prop_item = $this->properties->get_by_id( $property )->current(); |
| 122 | $key = $key ?? $prop_item->get_id(); |
| 123 | |
| 124 | $prop_item->set_value( $key, $value, $this ); |
| 125 | |
| 126 | return $this; |
| 127 | } |
| 128 | |
| 129 | public function get( string $property ): Base_Object_Property { |
| 130 | return $this->properties->get_by_id( $property )->current(); |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * @param string $field_name |
| 135 | * |
| 136 | * @return string |
| 137 | */ |
| 138 | public function get_field_key( string $field_name ): string { |
| 139 | return Tools::sanitize_text_field( $this->fields_map[ $field_name ] ?? '' ); |
| 140 | } |
| 141 | |
| 142 | public function get_value( string $field_name ) { |
| 143 | return $this->request[ $field_name ] ?? false; |
| 144 | } |
| 145 | |
| 146 | public function get_field_name_by_prop( string $search_prop ) { |
| 147 | foreach ( $this->fields_map as $field_name => $prop ) { |
| 148 | if ( $search_prop === $prop ) { |
| 149 | return $field_name; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | return false; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * [field_name] => [property] |
| 158 | * |
| 159 | * @param array $fields_map |
| 160 | * |
| 161 | * Pass TRUE, if your fields_map in such format: |
| 162 | * [property] => [field_name] |
| 163 | * @param false $revert |
| 164 | * |
| 165 | * @return $this |
| 166 | */ |
| 167 | public function set_fields_map( array $fields_map, bool $revert = false ): self { |
| 168 | $fields_map = array_filter( $fields_map ); |
| 169 | |
| 170 | if ( ! $revert ) { |
| 171 | $this->fields_map = $fields_map; |
| 172 | |
| 173 | return $this; |
| 174 | } |
| 175 | |
| 176 | $this->fields_map = array_combine( |
| 177 | array_values( $fields_map ), |
| 178 | array_keys( $fields_map ) |
| 179 | ); |
| 180 | |
| 181 | return $this; |
| 182 | } |
| 183 | |
| 184 | public function set_request( $request ): Abstract_Modifier { |
| 185 | $this->request = array_merge( $this->request, $request ); |
| 186 | |
| 187 | return $this; |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Can be used after run action. |
| 192 | * Ex. in Base_Object_Property::do_after() |
| 193 | * |
| 194 | * @return Base_Modifier_Action |
| 195 | */ |
| 196 | public function get_action(): Base_Modifier_Action { |
| 197 | if ( is_null( $this->action ) ) { |
| 198 | $this->action = $this->get_supported_action(); |
| 199 | } |
| 200 | |
| 201 | return $this->action; |
| 202 | } |
| 203 | } |
| 204 |