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