-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathConversionResult.php
More file actions
178 lines (143 loc) · 4.6 KB
/
ConversionResult.php
File metadata and controls
178 lines (143 loc) · 4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
declare(strict_types=1);
namespace Otherguy\Currency\Results;
use Brick\Math\BigDecimal;
use Brick\Math\Exception\MathException;
use Brick\Math\RoundingMode;
use Otherguy\Currency\Currency;
use Otherguy\Currency\Exceptions\CurrencyException;
class ConversionResult
{
public const int DEFAULT_SCALE = 8;
/**
* @var array<string, BigDecimal>
*/
public readonly array $originalConversionRates;
public readonly string $originalBaseCurrency;
/**
* @var array<string, BigDecimal>
*/
private array $conversionRates;
private string $baseCurrency;
/**
* @param array<string, BigDecimal|float|int|string> $rates
* @param int<0, max> $scale
*
* @throws MathException If a rate value is not a valid numeric.
*/
public function __construct(
string|Currency $baseCurrency,
public readonly ?string $date = null,
array $rates = [],
public readonly int $scale = self::DEFAULT_SCALE,
) {
$code = Currency::code($baseCurrency);
$this->originalBaseCurrency = $code;
$this->baseCurrency = $code;
$normalised = [];
foreach ($rates as $currency => $rate) {
$normalised[(string) $currency] = $this->toBigDecimal($rate);
}
$normalised[$code] = BigDecimal::one();
$this->originalConversionRates = $normalised;
$this->conversionRates = $normalised;
}
public function getBaseCurrency(): string
{
return $this->baseCurrency;
}
public function getDate(): ?string
{
return $this->date;
}
/**
* @throws CurrencyException
*/
public function setBaseCurrency(string|Currency $baseCurrency): self
{
$code = Currency::code($baseCurrency);
if (!isset($this->originalConversionRates[$code])) {
throw new CurrencyException("No conversion result for '{$code}'!");
}
if ($code === $this->originalBaseCurrency) {
$this->conversionRates = $this->originalConversionRates;
$this->baseCurrency = $code;
return $this;
}
$divisor = $this->originalConversionRates[$code];
$rebased = [];
foreach ($this->originalConversionRates as $currency => $rate) {
$rebased[$currency] = $rate->dividedBy($divisor, $this->scale, RoundingMode::HalfUp);
}
$rebased[$code] = BigDecimal::one();
$this->conversionRates = $rebased;
$this->baseCurrency = $code;
return $this;
}
/**
* @throws CurrencyException
*/
public function rate(string|Currency $currency): BigDecimal
{
$code = Currency::code($currency);
if (!isset($this->conversionRates[$code])) {
throw new CurrencyException("No conversion result for {$code}!");
}
return $this->conversionRates[$code];
}
/**
* @throws CurrencyException
*/
public function rateAsFloat(string|Currency $currency): float
{
return $this->rate($currency)->toFloat();
}
/**
* @throws CurrencyException
*/
public function convert(
BigDecimal|float|int|string $amount,
string|Currency $fromCurrency,
string|Currency $toCurrency,
): BigDecimal {
$from = Currency::code($fromCurrency);
$to = Currency::code($toCurrency);
if (!isset($this->originalConversionRates[$to])) {
throw new CurrencyException("No conversion result for '{$to}'!");
}
if (!isset($this->originalConversionRates[$from])) {
throw new CurrencyException("No conversion result for '{$from}'!");
}
return $this->toBigDecimal($amount)
->multipliedBy($this->originalConversionRates[$to])
->dividedBy($this->originalConversionRates[$from], $this->scale, RoundingMode::HalfUp);
}
/**
* @return array<string, BigDecimal>
*/
public function all(): array
{
return $this->conversionRates;
}
/**
* @return array<string, float>
*/
public function allAsFloats(): array
{
$floats = [];
foreach ($this->conversionRates as $code => $rate) {
$floats[$code] = $rate->toFloat();
}
return $floats;
}
/**
* @throws MathException
*/
private function toBigDecimal(BigDecimal|float|int|string $value): BigDecimal
{
if (is_float($value)) {
return BigDecimal::of((string) $value);
}
return $value instanceof BigDecimal ? $value : BigDecimal::of($value);
}
}