| 1 |
<?php |
| 2 |
|
| 3 |
namespace Hostinger\Reach\Models; |
| 4 |
|
| 5 |
|
| 6 |
use Hostinger\Reach\Setup\Encrypt; |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
die; |
| 10 |
} |
| 11 |
|
| 12 |
class Cart implements Model { |
| 13 |
|
| 14 |
public const STATUS_ACTIVE = 'active'; |
| 15 |
public const STATUS_ABANDONED = 'abandoned'; |
| 16 |
public const STATUS_ERROR = 'error'; |
| 17 |
public const STATUS_OLD = 'old'; |
| 18 |
public const STATUS_PROCESSING = 'processing'; |
| 19 |
|
| 20 |
public const STATUSES = array( |
| 21 |
self::STATUS_ACTIVE, |
| 22 |
self::STATUS_ABANDONED, |
| 23 |
self::STATUS_OLD, |
| 24 |
self::STATUS_PROCESSING, |
| 25 |
); |
| 26 |
|
| 27 |
private string $hash = ''; |
| 28 |
private ?int $customer_id = null; |
| 29 |
private string $email = ''; |
| 30 |
private array $items = array(); |
| 31 |
private array $totals = array(); |
| 32 |
private string $currency; |
| 33 |
private string $updated_at = ''; |
| 34 |
private string $status = self::STATUS_ACTIVE; |
| 35 |
|
| 36 |
public function __construct( array $db_array = array() ) { |
| 37 |
|
| 38 |
if ( array_key_exists( 'hash', $db_array ) ) { |
| 39 |
$this->set_hash( (string) $db_array['hash'] ); |
| 40 |
} |
| 41 |
|
| 42 |
if ( array_key_exists( 'customer_id', $db_array ) ) { |
| 43 |
$this->set_customer_id( $db_array['customer_id'] === null ? null : (int) $db_array['customer_id'] ); |
| 44 |
} |
| 45 |
|
| 46 |
if ( array_key_exists( 'customer_email', $db_array ) ) { |
| 47 |
$this->set_email( (string) $db_array['customer_email'] ); |
| 48 |
} |
| 49 |
|
| 50 |
if ( array_key_exists( 'items', $db_array ) ) { |
| 51 |
$this->set_items( $db_array['items'] ); |
| 52 |
} |
| 53 |
|
| 54 |
if ( array_key_exists( 'totals', $db_array ) ) { |
| 55 |
$this->set_totals( $db_array['totals'] ); |
| 56 |
} |
| 57 |
|
| 58 |
if ( array_key_exists( 'updated_at', $db_array ) ) { |
| 59 |
$this->set_updated_at( (string) $db_array['updated_at'] ); |
| 60 |
} |
| 61 |
|
| 62 |
if ( array_key_exists( 'currency', $db_array ) ) { |
| 63 |
$this->set_currency( (string) $db_array['currency'] ); |
| 64 |
} else { |
| 65 |
$this->set_currency( get_woocommerce_currency() ); |
| 66 |
} |
| 67 |
|
| 68 |
if ( array_key_exists( 'status', $db_array ) ) { |
| 69 |
$this->set_status( $db_array['status'] ); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
public function get_hash(): string { |
| 74 |
return $this->hash; |
| 75 |
} |
| 76 |
|
| 77 |
public function set_hash( string $hash ): void { |
| 78 |
$this->hash = sanitize_text_field( trim( $hash ) ); |
| 79 |
} |
| 80 |
|
| 81 |
public function get_customer_id(): ?int { |
| 82 |
return $this->customer_id; |
| 83 |
} |
| 84 |
|
| 85 |
public function set_customer_id( ?int $customer_id ): void { |
| 86 |
$this->customer_id = $customer_id; |
| 87 |
} |
| 88 |
|
| 89 |
public function get_email(): string { |
| 90 |
return Encrypt::decrypt( $this->email ); |
| 91 |
} |
| 92 |
|
| 93 |
public function set_email( string $email ): void { |
| 94 |
$this->email = $email; |
| 95 |
} |
| 96 |
|
| 97 |
public function get_items(): array { |
| 98 |
return $this->items; |
| 99 |
} |
| 100 |
|
| 101 |
public function set_items( mixed $items ): void { |
| 102 |
if ( is_string( $items ) ) { |
| 103 |
$decoded = json_decode( $items, true ); |
| 104 |
if ( is_array( $decoded ) ) { |
| 105 |
$this->items = $decoded; |
| 106 |
|
| 107 |
return; |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
$this->items = is_array( $items ) ? $items : array(); |
| 112 |
} |
| 113 |
|
| 114 |
public function get_totals(): array { |
| 115 |
return $this->totals; |
| 116 |
} |
| 117 |
|
| 118 |
public function set_totals( mixed $totals ): void { |
| 119 |
if ( is_string( $totals ) ) { |
| 120 |
$decoded = json_decode( $totals, true ); |
| 121 |
if ( is_array( $decoded ) ) { |
| 122 |
$this->totals = $decoded; |
| 123 |
|
| 124 |
return; |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
$this->totals = is_array( $totals ) ? $totals : array(); |
| 129 |
} |
| 130 |
|
| 131 |
public function get_currency(): string { |
| 132 |
return $this->currency; |
| 133 |
} |
| 134 |
|
| 135 |
public function set_currency( string $currency ): void { |
| 136 |
$this->currency = $currency; |
| 137 |
} |
| 138 |
|
| 139 |
public function get_updated_at(): string { |
| 140 |
return $this->updated_at; |
| 141 |
} |
| 142 |
|
| 143 |
public function set_updated_at( string $updated_at ): void { |
| 144 |
$this->updated_at = sanitize_text_field( trim( $updated_at ) ); |
| 145 |
} |
| 146 |
|
| 147 |
public function update(): void { |
| 148 |
$this->set_updated_at( current_time( 'mysql' ) ); |
| 149 |
} |
| 150 |
|
| 151 |
public function get_status(): string { |
| 152 |
return $this->status; |
| 153 |
} |
| 154 |
|
| 155 |
public function set_status( string $status ): void { |
| 156 |
if ( array_key_exists( $status, self::STATUSES ) ) { |
| 157 |
$this->status = $status; |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
public function to_array(): array { |
| 162 |
return array( |
| 163 |
'hash' => $this->get_hash(), |
| 164 |
'currency' => $this->get_currency(), |
| 165 |
'customer_id' => $this->get_customer_id(), |
| 166 |
'customer_email' => $this->get_email(), |
| 167 |
'items' => $this->get_items(), |
| 168 |
'totals' => $this->get_totals(), |
| 169 |
'updated_at' => $this->get_updated_at(), |
| 170 |
'status' => $this->get_status(), |
| 171 |
); |
| 172 |
} |
| 173 |
} |
| 174 |
|