Cart.php
8 months ago
CartItem.php
8 months ago
PluginData.php
7 months ago
ReachContact.php
7 months ago
Totals.php
8 months ago
Cart.php
70 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Hostinger\Reach\Dto; |
| 4 | |
| 5 | class Cart { |
| 6 | |
| 7 | private string $hash; |
| 8 | private string $email; |
| 9 | private string $currency; |
| 10 | private array $items; |
| 11 | private array $totals; |
| 12 | |
| 13 | public function __construct( string $hash, string $email, string $currency, array $items, array $totals ) { |
| 14 | $this->hash = $hash; |
| 15 | $this->email = $email; |
| 16 | $this->currency = $currency; |
| 17 | $this->items = $items; |
| 18 | $this->totals = $totals; |
| 19 | } |
| 20 | |
| 21 | public function get_hash(): string { |
| 22 | return $this->hash; |
| 23 | } |
| 24 | |
| 25 | public function get_currency(): string { |
| 26 | return $this->currency; |
| 27 | } |
| 28 | |
| 29 | public function get_email(): string { |
| 30 | return $this->email; |
| 31 | } |
| 32 | |
| 33 | public function get_items(): array { |
| 34 | return array_map( |
| 35 | function ( $item ) { |
| 36 | $cart_item = new CartItem( $item['product_id'], $item['quantity'] ); |
| 37 | |
| 38 | return $cart_item->to_array(); |
| 39 | }, |
| 40 | $this->items |
| 41 | ); |
| 42 | } |
| 43 | |
| 44 | public function get_totals(): array { |
| 45 | $totals = Totals::from_cart_totals( $this->totals, $this->currency ); |
| 46 | |
| 47 | return $totals->to_array(); |
| 48 | } |
| 49 | |
| 50 | public function to_array(): array { |
| 51 | return array( |
| 52 | 'hash' => $this->get_hash(), |
| 53 | 'currency' => $this->get_currency(), |
| 54 | 'customer_email' => $this->get_email(), |
| 55 | 'items' => $this->get_items(), |
| 56 | 'totals' => $this->get_totals(), |
| 57 | ); |
| 58 | } |
| 59 | |
| 60 | public static function from_array( array $data ): Cart { |
| 61 | return new self( |
| 62 | $data['hash'], |
| 63 | $data['customer_email'], |
| 64 | $data['currency'], |
| 65 | $data['items'], |
| 66 | $data['totals'], |
| 67 | ); |
| 68 | } |
| 69 | } |
| 70 |