yaroslawww /
laravel-quick-checkout
| 1 | <?php |
||
| 2 | |||
| 3 | namespace QuickCheckout; |
||
| 4 | |||
| 5 | use Illuminate\Support\Str; |
||
| 6 | use QuickCheckout\Contracts\ProductInterface; |
||
| 7 | |||
| 8 | /** |
||
| 9 | * @mixin ProductInterface |
||
| 10 | */ |
||
| 11 | class LineItem |
||
| 12 | { |
||
| 13 | protected ?ProductInterface $product = null; |
||
| 14 | |||
| 15 | protected string $id; |
||
| 16 | |||
| 17 | protected int $quantity = 1; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @param array|ProductInterface $product |
||
| 21 | * @param int $quantity |
||
| 22 | * @param string|null $id |
||
| 23 | * |
||
| 24 | * @throws \Exception |
||
| 25 | */ |
||
| 26 | 5 | public function __construct(array|ProductInterface $product, int $quantity = 1, ?string $id = null) |
|
| 27 | { |
||
| 28 | 5 | $this->id = $id ?? (string) Str::uuid(); |
|
| 29 | |||
| 30 | 5 | $this->quantity = $quantity; |
|
| 31 | 5 | if ($product instanceof ProductInterface) { |
|
| 32 | 4 | $this->product = $product; |
|
| 33 | } else { |
||
| 34 | 3 | $this->product = CartProduct::fromArray($product); |
|
| 35 | } |
||
| 36 | 5 | } |
|
| 37 | |||
| 38 | 2 | public function toArray(): array |
|
| 39 | { |
||
| 40 | return [ |
||
| 41 | 2 | 'id' => $this->id, |
|
| 42 | 2 | 'quantity' => $this->quantity, |
|
| 43 | 2 | 'product' => $this->product()?->toArray(), |
|
| 44 | ]; |
||
| 45 | } |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Get product from session. |
||
| 49 | * |
||
| 50 | * @param array|null $data |
||
| 51 | * |
||
| 52 | * @return static|null |
||
| 53 | * @throws \Exception |
||
| 54 | */ |
||
| 55 | 4 | public static function fromArray(?array $data = null): ?static |
|
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 56 | { |
||
| 57 | 4 | if (is_array($data) && |
|
| 58 | 4 | !empty($data['product']) |
|
| 59 | ) { |
||
| 60 | 3 | return new static($data['product'], $data['quantity'] ?? 1, $data['id'] ?? null); |
|
| 61 | } |
||
| 62 | |||
| 63 | 1 | return null; |
|
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Get Line item product. |
||
| 68 | * |
||
| 69 | * @return ProductInterface|null |
||
| 70 | */ |
||
| 71 | 5 | public function product(): ?ProductInterface |
|
| 72 | { |
||
| 73 | 5 | return $this->product; |
|
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Get quantity. |
||
| 78 | * |
||
| 79 | * @return int |
||
| 80 | */ |
||
| 81 | 3 | public function quantity(): int |
|
| 82 | { |
||
| 83 | 3 | return $this->quantity; |
|
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @return string |
||
| 88 | */ |
||
| 89 | 1 | public function id(): string |
|
| 90 | { |
||
| 91 | 1 | return $this->id; |
|
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @return int |
||
| 96 | * @throws \Exception |
||
| 97 | */ |
||
| 98 | 3 | public function total(): int |
|
| 99 | { |
||
| 100 | 3 | return $this->product()?->price() * $this->quantity(); |
|
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Propagate call to product |
||
| 105 | * |
||
| 106 | * @param string $method |
||
| 107 | * @param array $arguments |
||
| 108 | * |
||
| 109 | * @return false|mixed |
||
| 110 | * @throws \Exception |
||
| 111 | */ |
||
| 112 | 1 | public function __call(string $method, array $arguments) |
|
| 113 | { |
||
| 114 | 1 | $product = $this->product(); |
|
| 115 | 1 | if ($product && method_exists($product, $method)) { |
|
| 116 | 1 | return call_user_func_array([ ($this->product()), $method ], $arguments); |
|
| 117 | } |
||
| 118 | |||
| 119 | 1 | throw new \BadMethodCallException("Method [{$method}] not exists in class: " . __CLASS__); |
|
| 120 | } |
||
| 121 | } |
||
| 122 |