| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\Variables\Storage\Entities; |
| 4 |
|
| 5 |
use InvalidArgumentException; |
| 6 |
|
| 7 |
class Variable { |
| 8 |
private array $data; |
| 9 |
|
| 10 |
private function __construct( array $data ) { |
| 11 |
$this->data = $data; |
| 12 |
} |
| 13 |
|
| 14 |
public static function create_new( array $data ): self { |
| 15 |
$now = gmdate( 'Y-m-d H:i:s' ); |
| 16 |
|
| 17 |
$data['created_at'] = $now; |
| 18 |
$data['updated_at'] = $now; |
| 19 |
|
| 20 |
return self::from_array( $data ); |
| 21 |
} |
| 22 |
|
| 23 |
public static function from_array( array $data ): self { |
| 24 |
$required = [ 'id', 'type', 'label', 'value' ]; |
| 25 |
|
| 26 |
foreach ( $required as $key ) { |
| 27 |
if ( ! array_key_exists( $key, $data ) ) { |
| 28 |
throw new InvalidArgumentException( |
| 29 |
sprintf( |
| 30 |
"Missing required field '%s' in %s::from_array()", |
| 31 |
esc_html( $key ), |
| 32 |
self::class |
| 33 |
) |
| 34 |
); |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
return new self( $data ); |
| 39 |
} |
| 40 |
|
| 41 |
public function soft_delete(): void { |
| 42 |
$this->data['deleted_at'] = $this->now(); |
| 43 |
} |
| 44 |
|
| 45 |
public function restore(): void { |
| 46 |
unset( $this->data['deleted_at'] ); |
| 47 |
// TODO to be removed if client is no longer need this |
| 48 |
unset( $this->data['deleted'] ); |
| 49 |
|
| 50 |
$this->data['updated_at'] = $this->now(); |
| 51 |
} |
| 52 |
|
| 53 |
private function now() { |
| 54 |
return gmdate( 'Y-m-d H:i:s' ); |
| 55 |
} |
| 56 |
|
| 57 |
public function to_array(): array { |
| 58 |
return array_diff_key( $this->data, array_flip( [ 'id' ] ) ); |
| 59 |
} |
| 60 |
|
| 61 |
public function id(): string { |
| 62 |
return $this->data['id']; |
| 63 |
} |
| 64 |
|
| 65 |
public function label(): string { |
| 66 |
return $this->data['label']; |
| 67 |
} |
| 68 |
|
| 69 |
public function order(): int { |
| 70 |
return $this->data['order']; |
| 71 |
} |
| 72 |
|
| 73 |
public function value() { |
| 74 |
return $this->data['value']; |
| 75 |
} |
| 76 |
|
| 77 |
public function set_value( $value ) { |
| 78 |
$this->data['value'] = $value; |
| 79 |
} |
| 80 |
|
| 81 |
public function type() { |
| 82 |
return $this->data['type']; |
| 83 |
} |
| 84 |
|
| 85 |
public function has_order(): int { |
| 86 |
return isset( $this->data['order'] ); |
| 87 |
} |
| 88 |
|
| 89 |
public function is_deleted(): bool { |
| 90 |
return isset( $this->data['deleted_at'] ); |
| 91 |
} |
| 92 |
|
| 93 |
public function apply_changes( array $data ): void { |
| 94 |
$allowed_fields = [ 'label', 'value', 'order' ]; |
| 95 |
$has_changes = false; |
| 96 |
|
| 97 |
foreach ( $allowed_fields as $field ) { |
| 98 |
if ( isset( $data[ $field ] ) ) { |
| 99 |
$this->data[ $field ] = $data[ $field ]; |
| 100 |
|
| 101 |
$has_changes = true; |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
if ( $has_changes ) { |
| 106 |
$this->data['updated_at'] = $this->now(); |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
|