8
8
namespace Vaened \SwiftCart \Tests \Utils ;
9
9
10
10
use Vaened \PriceEngine \Adjustments \Adjusters ;
11
+ use Vaened \PriceEngine \Adjustments \Charge ;
12
+ use Vaened \PriceEngine \Adjustments \Discount ;
11
13
use Vaened \PriceEngine \Money \Amount ;
12
14
use Vaened \SwiftCart \Entities \Chargeable ;
13
15
use Vaened \SwiftCart \Entities \Discountable ;
14
16
use Vaened \SwiftCart \Entities \Tradable ;
15
17
use Vaened \SwiftCart \UniqueId ;
16
18
19
+ use function Lambdish \Phunctional \each ;
20
+
17
21
final class Product implements Tradable, Discountable, Chargeable
18
22
{
23
+ private readonly Adjusters $ charges ;
24
+
25
+ private readonly Adjusters $ discounts ;
26
+
19
27
public function __construct (
20
28
private readonly string $ id ,
21
29
private readonly Amount $ amount ,
22
30
)
23
31
{
32
+ $ this ->charges = Adjusters::empty ();
33
+ $ this ->discounts = Adjusters::empty ();
24
34
}
25
35
26
36
public static function random (): self
@@ -33,6 +43,19 @@ public static function create(Amount $amount): self
33
43
return new self (UniqueId::random ()->uniqueId (), $ amount );
34
44
}
35
45
46
+ public function with (Discount |Charge ...$ adjusters ): self
47
+ {
48
+ each (function (Discount |Charge $ adjuster ) {
49
+ if ($ adjuster instanceof Discount) {
50
+ $ this ->discounts ->push ($ adjuster );
51
+ } else {
52
+ $ this ->charges ->push ($ adjuster );
53
+ }
54
+ }, $ adjusters );
55
+
56
+ return $ this ;
57
+ }
58
+
36
59
public function uniqueId (): string
37
60
{
38
61
return $ this ->id ;
@@ -45,11 +68,11 @@ public function amount(): Amount
45
68
46
69
public function charges (): Adjusters
47
70
{
48
- return Adjusters:: empty () ;
71
+ return $ this -> charges ;
49
72
}
50
73
51
74
public function discounts (): Adjusters
52
75
{
53
- return Adjusters:: empty () ;
76
+ return $ this -> discounts ;
54
77
}
55
78
}
0 commit comments