-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathExchangeRate.php
More file actions
197 lines (177 loc) · 5.24 KB
/
Copy pathExchangeRate.php
File metadata and controls
197 lines (177 loc) · 5.24 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<?php
/**
* @package SugiPHP.Money
*/
namespace SugiPHP\Money;
use InvalidArgumentException;
class ExchangeRate
{
/**
* Source Currency. The currency from which the exchange will be done.
*
* @var Currency
*/
protected $sourceCurrency;
/**
* Target Currency. The currency to which the exchange will be done.
*
* @var Currency
*/
protected $targetCurrency;
/**
* Conversion rate.
*
* @var numeric
*/
protected $rate;
/**
* Exchange Rate Object Constructor.
*
* __construct(Currency $source, Currency $target, numeric $rate);
*
* __construct($string);
*
* @throws InvalidArgumentException If conversion ratio is not numeric
* @throws InvalidArgumentException If conversion ratio is 0
*/
public function __construct($source, $target = null, $rate = null)
{
$paramCount = func_num_args();
if (1 === $paramCount) {
$this->createFromString($source);
} elseif ($paramCount === 3) {
$this->setRate($rate);
if (is_string($source)) {
$source = new Currency($source);
}
if (is_string($target)) {
$target = new Currency($target);
}
$this->sourceCurrency = $source;
$this->targetCurrency = $target;
} else {
throw new InvalidArgumentException("ExchangeRate constructor expects 1 or 3 params. {$paramCount} given");
}
}
/**
* Returns string formated the same way fromString() static method expects:
* "BGN/EUR 0.511280836"
*
* @return string
*/
public function __toString()
{
return $this->getSourceCurrency()."/".$this->getTargetCurrency()." ".$this->GetRate();
}
/**
* Returns the exchange rate.
*
* @return numeric
*/
public function getRate()
{
return $this->rate;
}
/**
* Returns the source currency.
*
* @return Currency
*/
public function getSourceCurrency()
{
return $this->sourceCurrency;
}
/**
* Returns the destination currency.
*
* @return Currency
*/
public function getTargetCurrency()
{
return $this->targetCurrency;
}
/**
* Converts amount from source to target currency.
* If $money parameter is in target currency reverse conversions will be made and
* money of source currency will be returned.
*
* @param numeric|Money $money
*
* @return Money
*
* @throws InvalidArgumentException
*/
public function exchange($money)
{
if (is_numeric($money)) {
return new Money($money * $this->rate, $this->targetCurrency);
}
if (!($money instanceof Money)) {
throw new InvalidArgumentException("exchange method accepts only numeric or Money objects.");
}
$currency = $money->getCurrency();
if ($currency->isEqualTo($this->sourceCurrency)) {
return new Money($money->getAmount() * $this->rate, $this->targetCurrency);
}
if ($currency->isEqualTo($this->targetCurrency)) {
return new Money($money->getAmount() / $this->rate, $this->sourceCurrency);
}
throw new InvalidArgumentException(
sprintf(
"exchange method expected %s or %s, but %s received",
$this->sourceCurrency,
$this->targetCurrency,
$currency
)
);
}
public function invert()
{
$cur = $this->sourceCurrency;
$this->sourceCurrency = $this->targetCurrency;
$this->targetCurrency = $cur;
$this->setRate(1 / $this->rate);
}
/**
* Sets exchange rate for the current currencies.
*
* @param numeric $rate Conversion rate.
*
* @throws InvalidArgumentException If conversion ratio is not numeric
*/
protected function setRate($rate)
{
if (!is_numeric($rate)) {
throw new InvalidArgumentException("Conversion rate must be a numeric value");
}
if (empty($rate)) {
throw new InvalidArgumentException("Conversion rate must be a non zero value");
}
$this->rate = $rate;
}
/**
* This will create an Exchange Rate object from the given string.
* Example: "BGN/USD 1.4567".
*
* @param string $string
*
* @return ExchangeRate
*
* @throws InvalidArgumentException If the string cannot be converted to Money object
*/
protected function createFromString($string)
{
$currency1 = "(?P<currency1>[A-Z]{3,3})";
$currency2 = "(?P<currency2>[A-Z]{3,3})";
$rate = "(?P<rate>[0-9]*\.?[0-9]+)";
$pattern = "~^".$currency1."/".$currency2."\s+".$rate."$~";
if (!preg_match($pattern, $string, $matches)) {
throw new InvalidArgumentException(
"Can't create currency pair from ISO string {$string}, format of string is invalid"
);
};
$this->setRate($matches["rate"]);
$this->sourceCurrency = new Currency($matches["currency1"]);
$this->targetCurrency = new Currency($matches["currency2"]);
}
}