exceptions
2 years ago
abstract-modifier.php
1 year ago
base-modifier-action.php
2 years ago
base-object-property.php
2 years ago
object-dynamic-property.php
2 years ago
object-properties-collection.php
2 years ago
base-object-property.php
138 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace Jet_Form_Builder\Actions\Methods; |
| 5 | |
| 6 | use Jet_Form_Builder\Classes\Arrayable\Arrayable; |
| 7 | use Jet_Form_Builder\Classes\Arrayable\Collection; |
| 8 | use Jet_Form_Builder\Classes\Arrayable\Collection_Item_Interface; |
| 9 | use Jet_Form_Builder\Exceptions\Silence_Exception; |
| 10 | |
| 11 | // If this file is called directly, abort. |
| 12 | if ( ! defined( 'WPINC' ) ) { |
| 13 | die; |
| 14 | } |
| 15 | |
| 16 | abstract class Base_Object_Property implements |
| 17 | Collection_Item_Interface, |
| 18 | Arrayable { |
| 19 | |
| 20 | protected $value = null; |
| 21 | protected $is_excluded = false; |
| 22 | |
| 23 | abstract public function get_label(): string; |
| 24 | |
| 25 | public function set_value( string $key, $value, Abstract_Modifier $modifier ) { |
| 26 | if ( ! $this->can_attach( $key, $value ) ) { |
| 27 | return; |
| 28 | } |
| 29 | /** |
| 30 | * This is necessary for those cases when we need to set |
| 31 | * (or not) some property and then build on it. |
| 32 | */ |
| 33 | $this->set_related( $modifier ); |
| 34 | $this->do_before( $key, $value, $modifier ); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * For dynamic properties, it cannot be constant, |
| 39 | * so we pass it through parameters |
| 40 | * |
| 41 | * @param string $key |
| 42 | * |
| 43 | * @param $value |
| 44 | * @param Abstract_Modifier $modifier |
| 45 | */ |
| 46 | public function do_before( string $key, $value, Abstract_Modifier $modifier ) { |
| 47 | $this->value = $value; |
| 48 | } |
| 49 | |
| 50 | public function do_after( Abstract_Modifier $modifier ) { |
| 51 | } |
| 52 | |
| 53 | public function can_attach( string $key, $value ): bool { |
| 54 | if ( is_a( $this, Object_Dynamic_Property::class ) ) { |
| 55 | return $this->is_supported( $key, $value ); |
| 56 | } |
| 57 | |
| 58 | return ! $this->is_excluded && is_null( $this->value ); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * @return null |
| 63 | * @throws Silence_Exception |
| 64 | */ |
| 65 | public function get_value( Abstract_Modifier $modifier ) { |
| 66 | if ( ! is_null( $this->value ) ) { |
| 67 | return $this->value; |
| 68 | } |
| 69 | throw new Silence_Exception( 'Empty value' ); |
| 70 | } |
| 71 | |
| 72 | public function exclude() { |
| 73 | $this->is_excluded = true; |
| 74 | } |
| 75 | |
| 76 | protected function set_related( Abstract_Modifier $modifier ) { |
| 77 | $related = $modifier->properties->get_by_ids( $this->get_related() ); |
| 78 | |
| 79 | /** @var Base_Object_Property $property */ |
| 80 | foreach ( $related as $property ) { |
| 81 | $field_name = $modifier->get_field_name_by_prop( $property->get_id() ); |
| 82 | |
| 83 | if ( false === $field_name ) { |
| 84 | continue; |
| 85 | } |
| 86 | |
| 87 | if ( $property->get_id() === $this->get_id() ) { |
| 88 | wp_die( |
| 89 | esc_attr( |
| 90 | sprintf( |
| 91 | /* translators: %s - object property name */ |
| 92 | __( |
| 93 | 'Logic error. Property [%s] can not be related by itself.', |
| 94 | 'jet-form-builder' |
| 95 | ), |
| 96 | $property->get_id() |
| 97 | ) |
| 98 | ) |
| 99 | ); |
| 100 | } |
| 101 | $value = $modifier->get_value( $field_name ); |
| 102 | |
| 103 | $property->set_value( $property->get_id(), $value, $modifier ); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * @return string |
| 109 | */ |
| 110 | public function get_attach_id(): string { |
| 111 | return $this->get_id(); |
| 112 | } |
| 113 | |
| 114 | public function get_help(): string { |
| 115 | return ''; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * @return string[] |
| 120 | */ |
| 121 | public function get_related(): array { |
| 122 | return array(); |
| 123 | } |
| 124 | |
| 125 | public function is_merge_value(): bool { |
| 126 | return false; |
| 127 | } |
| 128 | |
| 129 | public function to_array(): array { |
| 130 | return array( |
| 131 | 'value' => $this->get_id(), |
| 132 | 'label' => $this->get_label(), |
| 133 | 'help' => $this->get_help(), |
| 134 | ); |
| 135 | } |
| 136 | |
| 137 | } |
| 138 |