1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Plane\Shop package. |
5
|
|
|
* |
6
|
|
|
* (c) Dariusz Korsak <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Plane\Shop; |
13
|
|
|
|
14
|
|
|
use Money\Money; |
15
|
|
|
use Money\MoneyFormatter; |
16
|
|
|
use Plane\Shop\CartInterface; |
17
|
|
|
use Plane\Shop\CartItemInterface; |
18
|
|
|
use Plane\Shop\Traits\CartCommon; |
19
|
|
|
use Money\Currencies\ISOCurrencies; |
20
|
|
|
use Plane\Shop\CartDiscountInterface; |
21
|
|
|
use Plane\Shop\CartPresenterInterface; |
22
|
|
|
use Money\Formatter\DecimalMoneyFormatter; |
23
|
|
|
|
24
|
|
|
class CartPresenter implements CartPresenterInterface |
25
|
|
|
{ |
26
|
|
|
use CartCommon; |
27
|
|
|
|
28
|
|
|
private $cart; |
29
|
|
|
|
30
|
|
|
private $priceFormatter; |
31
|
|
|
|
32
|
13 |
|
public function __construct(CartInterface $cart, MoneyFormatter $priceFormatter = null) |
33
|
|
|
{ |
34
|
13 |
|
$this->cart = $cart; |
35
|
13 |
|
$this->priceFormatter = $priceFormatter ?: new DecimalMoneyFormatter(new ISOCurrencies()); |
36
|
13 |
|
} |
37
|
|
|
|
38
|
2 |
|
public function totalNet(): string |
39
|
|
|
{ |
40
|
2 |
|
return $this->priceFormatter->format($this->cart->totalNet()); |
41
|
|
|
} |
42
|
|
|
|
43
|
2 |
|
public function totalGross(): string |
44
|
|
|
{ |
45
|
2 |
|
return $this->priceFormatter->format($this->cart->totalGross()); |
46
|
|
|
} |
47
|
|
|
|
48
|
2 |
|
public function tax(): string |
49
|
|
|
{ |
50
|
2 |
|
return $this->priceFormatter->format($this->cart->tax()); |
51
|
|
|
} |
52
|
|
|
|
53
|
2 |
|
public function totalAfterDiscounts(): string |
54
|
|
|
{ |
55
|
2 |
|
return $this->priceFormatter->format($this->cart->totalAfterDiscounts()); |
56
|
|
|
} |
57
|
|
|
|
58
|
2 |
|
public function shippingCost(): string |
59
|
|
|
{ |
60
|
2 |
|
return $this->priceFormatter->format($this->cart->shippingCost()); |
61
|
|
|
} |
62
|
|
|
|
63
|
2 |
|
public function paymentFee(): string |
64
|
|
|
{ |
65
|
2 |
|
return $this->priceFormatter->format($this->cart->paymentFee()); |
66
|
|
|
} |
67
|
|
|
|
68
|
1 |
|
public function toArray(): array |
69
|
|
|
{ |
70
|
1 |
|
$array = []; |
71
|
|
|
$array['items'] = array_map(function (CartItemInterface $item) { |
72
|
1 |
|
$itemArray = $item->toArray($this->getCurrency()); |
73
|
|
|
|
74
|
|
|
array_walk_recursive($itemArray, function (&$value, $key) { |
|
|
|
|
75
|
1 |
|
if ($value instanceof Money) { |
76
|
1 |
|
$value = $this->priceFormatter->format($value); |
77
|
|
|
} |
78
|
1 |
|
}); |
79
|
|
|
|
80
|
1 |
|
return $itemArray; |
81
|
1 |
|
}, $this->items()); |
82
|
|
|
|
83
|
1 |
View Code Duplication |
if (!is_null($this->getShipping())) { |
|
|
|
|
84
|
1 |
|
$array['shipping']['id'] = $this->getShipping()->getId(); |
85
|
1 |
|
$array['shipping']['name'] = $this->getShipping()->getName(); |
86
|
1 |
|
$array['shipping']['desc'] = $this->getShipping()->getDescription(); |
87
|
1 |
|
$array['shipping']['cost'] = $this->shippingCost(); |
88
|
|
|
} |
89
|
|
|
|
90
|
1 |
View Code Duplication |
if (!is_null($this->getPayment())) { |
|
|
|
|
91
|
1 |
|
$array['payment']['id'] = $this->getPayment()->getId(); |
92
|
1 |
|
$array['payment']['name'] = $this->getPayment()->getName(); |
93
|
1 |
|
$array['payment']['desc'] = $this->getPayment()->getDescription(); |
94
|
1 |
|
$array['payment']['fee'] = $this->paymentFee(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$array['discounts'] = array_map(function (CartDiscountInterface $discount) { |
98
|
|
|
return [ |
99
|
1 |
|
'text' => $discount->getDescription(), |
100
|
1 |
|
'price' => $this->priceFormatter->format($discount->getPriceAfterDiscount()) |
101
|
|
|
]; |
102
|
1 |
|
}, $this->getDiscounts()); |
103
|
|
|
|
104
|
1 |
|
$array['itemsQuantity'] = $this->itemsQuantity(); |
105
|
1 |
|
$array['totalNet'] = $this->totalNet(); |
106
|
1 |
|
$array['totalGross'] = $this->totalGross(); |
107
|
1 |
|
$array['tax'] = $this->tax(); |
108
|
1 |
|
$array['totalWeight'] = $this->weight(); |
109
|
1 |
|
$array['shippingCost'] = $this->shippingCost(); |
110
|
1 |
|
$array['paymentFee'] = $this->paymentFee(); |
111
|
1 |
|
$array['totalAfterDiscounts'] = $this->totalAfterDiscounts(); |
112
|
|
|
|
113
|
1 |
|
return $array; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.