1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Plane\Shop package. |
7
|
|
|
* |
8
|
|
|
* (c) Dariusz Korsak <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Plane\Shop; |
15
|
|
|
|
16
|
|
|
use Money\Money; |
17
|
|
|
use Money\Currency; |
18
|
|
|
use InvalidArgumentException; |
19
|
|
|
use Money\Currencies\ISOCurrencies; |
20
|
|
|
use Money\Parser\DecimalMoneyParser; |
21
|
|
|
|
22
|
|
|
class Payment implements PaymentInterface |
23
|
|
|
{ |
24
|
|
|
public const FEE_FIXED = 'fixed'; |
25
|
|
|
|
26
|
|
|
public const FEE_PERCENTAGE = 'percentage'; |
27
|
|
|
|
28
|
|
|
private $id; |
29
|
|
|
|
30
|
|
|
private $name; |
31
|
|
|
|
32
|
|
|
private $description; |
33
|
|
|
|
34
|
|
|
private $fee; |
35
|
|
|
|
36
|
|
|
private $feeType; |
37
|
|
|
|
38
|
|
|
private $feeTypes = [ |
39
|
|
|
self::FEE_FIXED, |
40
|
|
|
self::FEE_PERCENTAGE, |
41
|
|
|
]; |
42
|
|
|
|
43
|
|
|
private $requiredFields = [ |
44
|
|
|
'id', |
45
|
|
|
'name', |
46
|
|
|
'fee', |
47
|
|
|
]; |
48
|
|
|
|
49
|
5 |
|
public function __construct(array $data, string $feeType = self::FEE_FIXED) |
50
|
|
|
{ |
51
|
5 |
|
if (count(array_intersect_key(array_flip($this->requiredFields), $data)) !== count($this->requiredFields)) { |
52
|
1 |
|
throw new InvalidArgumentException( |
53
|
1 |
|
'Cannot create object, required array keys: ' . implode(', ', $this->requiredFields) |
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
4 |
|
if (!in_array($feeType, $this->feeTypes, true)) { |
58
|
1 |
|
throw new InvalidArgumentException('Invalid fee type'); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
// waiting for typed properties in PHP 7.4 |
62
|
3 |
|
foreach ($data as $property => $value) { |
63
|
3 |
|
$this->$property = $value; |
64
|
|
|
} |
65
|
|
|
|
66
|
3 |
|
$this->feeType = $feeType; |
67
|
3 |
|
} |
68
|
|
|
|
69
|
1 |
|
public static function createWithFixedFee(array $data): Payment |
70
|
|
|
{ |
71
|
1 |
|
return new Payment($data, self::FEE_FIXED); |
72
|
|
|
} |
73
|
|
|
|
74
|
1 |
|
public static function createWithPercentageFee(array $data): Payment |
75
|
|
|
{ |
76
|
1 |
|
return new Payment($data, self::FEE_PERCENTAGE); |
77
|
|
|
} |
78
|
|
|
|
79
|
1 |
|
public function getId(): int |
80
|
|
|
{ |
81
|
1 |
|
return $this->id; |
82
|
|
|
} |
83
|
|
|
|
84
|
1 |
|
public function getName(): string |
85
|
|
|
{ |
86
|
1 |
|
return $this->name; |
87
|
|
|
} |
88
|
|
|
|
89
|
1 |
|
public function getDescription(): string |
90
|
|
|
{ |
91
|
1 |
|
return $this->description; |
92
|
|
|
} |
93
|
|
|
|
94
|
3 |
|
public function getFee(Money $totalPrice, string $currency): Money |
95
|
|
|
{ |
96
|
3 |
|
if ($this->feeType === self::FEE_PERCENTAGE) { |
97
|
1 |
|
return $totalPrice->multiply($this->fee / 100, Money::ROUND_HALF_DOWN); |
98
|
|
|
} |
99
|
|
|
|
100
|
2 |
|
$moneyParser = new DecimalMoneyParser(new ISOCurrencies()); |
101
|
|
|
|
102
|
2 |
|
return $moneyParser->parse((string) $this->fee, new Currency($currency)); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function toArray(): array |
106
|
|
|
{ |
107
|
|
|
return [ |
108
|
|
|
'id' => $this->getId(), |
109
|
|
|
'name' => $this->getName(), |
110
|
|
|
'desc' => $this->getDescription(), |
111
|
|
|
]; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|