1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace SyliusCart\Domain\Model; |
6
|
|
|
|
7
|
|
|
use Money\Money; |
8
|
|
|
use SyliusCart\Domain\ValueObject\CartItemQuantity; |
9
|
|
|
use SyliusCart\Domain\ValueObject\ProductCode; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @author Arkadiusz Krakowiak <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
final class CartItem |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var ProductCode |
18
|
|
|
*/ |
19
|
|
|
private $productCode; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var CartItemQuantity |
23
|
|
|
*/ |
24
|
|
|
private $quantity; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var Money |
28
|
|
|
*/ |
29
|
|
|
private $unitPrice; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param ProductCode $productCode |
33
|
|
|
* @param CartItemQuantity $quantity |
34
|
|
|
* @param Money $unitPrice |
35
|
|
|
*/ |
36
|
|
|
public function __construct( |
37
|
|
|
ProductCode $productCode, |
38
|
|
|
CartItemQuantity $quantity, |
39
|
|
|
Money $unitPrice |
40
|
|
|
) { |
41
|
|
|
$this->productCode = $productCode; |
42
|
|
|
$this->quantity = $quantity; |
43
|
|
|
$this->unitPrice = $unitPrice; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param ProductCode $productCode |
48
|
|
|
* @param CartItemQuantity $quantity |
49
|
|
|
* @param Money $unitPrice |
50
|
|
|
* |
51
|
|
|
* @return CartItem |
52
|
|
|
*/ |
53
|
|
|
public static function create(ProductCode $productCode, CartItemQuantity $quantity, Money $unitPrice): self |
54
|
|
|
{ |
55
|
|
|
return new self($productCode, $quantity, $unitPrice); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param CartItem $cartItem |
60
|
|
|
* |
61
|
|
|
* @return CartItem |
62
|
|
|
*/ |
63
|
|
|
public function merge(CartItem $cartItem): self |
64
|
|
|
{ |
65
|
|
|
$newQuantity = $this->quantity->add($cartItem->quantity()); |
66
|
|
|
$newUnitPrice = $cartItem->unitPrice(); |
67
|
|
|
|
68
|
|
|
return new self($cartItem->productCode(), $newQuantity, $newUnitPrice); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return ProductCode |
73
|
|
|
*/ |
74
|
|
|
public function productCode(): ProductCode |
75
|
|
|
{ |
76
|
|
|
return $this->productCode; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return CartItemQuantity |
81
|
|
|
*/ |
82
|
|
|
public function quantity(): CartItemQuantity |
83
|
|
|
{ |
84
|
|
|
return $this->quantity; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return Money |
89
|
|
|
*/ |
90
|
|
|
public function unitPrice(): Money |
91
|
|
|
{ |
92
|
|
|
return $this->unitPrice; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|