| @@ -1,12 +1,8 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace Elementor\Modules\Variables\Storage\Entities; |
| 4 | 4 | |
| 5 | -use Elementor\Modules\Variables\Adapters\Prop_Type_Adapter; | |
| 6 | -use Elementor\Modules\Variables\PropTypes\Size_Variable_Prop_Type; | |
| 7 | -use Elementor\Modules\Variables\Storage\Exceptions\Type_Mismatch; | |
| 8 | -use Elementor\Modules\Variables\Storage\Exceptions\InvalidVariable; | |
| 9 | 5 | use InvalidArgumentException; |
| 10 | 6 | |
| 11 | 7 | class Variable { |
| 12 | 8 | private array $data; |
| @@ -85,12 +81,8 @@ | ||
| 85 | 81 | public function type() { |
| 86 | 82 | return $this->data['type']; |
| 87 | 83 | } |
| 88 | 84 | |
| 89 | - public function set_type( $type ) { | |
| 90 | - $this->data['type'] = $type; | |
| 91 | - } | |
| 92 | - | |
| 93 | 85 | public function has_order(): int { |
| 94 | 86 | return isset( $this->data['order'] ); |
| 95 | 87 | } |
| 96 | 88 | |
| @@ -97,50 +89,12 @@ | ||
| 97 | 89 | public function is_deleted(): bool { |
| 98 | 90 | return isset( $this->data['deleted_at'] ); |
| 99 | 91 | } |
| 100 | 92 | |
| 101 | - /** | |
| 102 | - * @throws Type_Mismatch If a type that is not allowed to be changed is passed. | |
| 103 | - */ | |
| 104 | - private function maybe_apply_type( array $data ) { | |
| 105 | - if ( ! array_key_exists( 'type', $data ) ) { | |
| 106 | - return false; | |
| 107 | - } | |
| 108 | - | |
| 109 | - $current_type = $this->type(); | |
| 110 | - $target_type = $data['type']; | |
| 111 | - | |
| 112 | - if ( $current_type === $target_type ) { | |
| 113 | - return false; | |
| 114 | - } | |
| 115 | - | |
| 116 | - $custom_size_prop_type = Prop_Type_Adapter::GLOBAL_CUSTOM_SIZE_VARIABLE_KEY; | |
| 117 | - $size_prop_type = Size_Variable_Prop_Type::get_key(); | |
| 118 | - | |
| 119 | - $allowed_types = [ $custom_size_prop_type, $size_prop_type ]; | |
| 120 | - | |
| 121 | - $is_valid_transition = | |
| 122 | - in_array( $current_type, $allowed_types, true ) && | |
| 123 | - in_array( $target_type, $allowed_types, true ); | |
| 124 | - | |
| 125 | - if ( ! $is_valid_transition ) { | |
| 126 | - throw new Type_Mismatch( 'Type change is forbidden' ); | |
| 127 | - } | |
| 128 | - | |
| 129 | - $this->set_type( $data['type'] ); | |
| 130 | - | |
| 131 | - return true; | |
| 132 | - } | |
| 133 | - | |
| 134 | - /** | |
| 135 | - * @throws InvalidVariable If the variable is not valid. | |
| 136 | - */ | |
| 137 | 93 | public function apply_changes( array $data ): void { |
| 138 | - $this->validate(); | |
| 94 | + $allowed_fields = [ 'label', 'value', 'order' ]; | |
| 95 | + $has_changes = false; | |
| 139 | 96 | |
| 140 | - $allowed_fields = [ 'label', 'value', 'order', 'type', 'sync_to_v3' ]; | |
| 141 | - $has_changes = $this->maybe_apply_type( $data ); | |
| 142 | - | |
| 143 | 97 | foreach ( $allowed_fields as $field ) { |
| 144 | 98 | if ( isset( $data[ $field ] ) ) { |
| 145 | 99 | $this->data[ $field ] = $data[ $field ]; |
| 146 | 100 | |
| @@ -150,22 +104,6 @@ | ||
| 150 | 104 | |
| 151 | 105 | if ( $has_changes ) { |
| 152 | 106 | $this->data['updated_at'] = $this->now(); |
| 153 | 107 | } |
| 154 | - } | |
| 155 | - | |
| 156 | - /** | |
| 157 | - * @return bool True if the variable is valid, throws an exception otherwise. | |
| 158 | - * @throws InvalidVariable If the variable is not valid. | |
| 159 | - */ | |
| 160 | - public function validate(): bool { | |
| 161 | - if ( strpos( $this->label(), ' ' ) !== false ) { | |
| 162 | - throw new InvalidVariable( 'Label cannot contain spaces' ); | |
| 163 | - } | |
| 164 | - | |
| 165 | - if ( strlen( $this->label() ) > 50 ) { | |
| 166 | - throw new InvalidVariable( 'Label cannot be longer than 50 characters' ); | |
| 167 | - } | |
| 168 | - | |
| 169 | - return true; | |
| 170 | 108 | } |
| 171 | 109 | } |