|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace MoneyMan; |
|
4
|
|
|
|
|
5
|
|
|
use MoneyMan\Currency; |
|
6
|
|
|
use MoneyMan\Money; |
|
7
|
|
|
use Swap\Swap; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Exchange Class |
|
11
|
|
|
* |
|
12
|
|
|
* Defines how an exchange should be performed. A subclass is needed to derive |
|
13
|
|
|
* the exchange rate used for |
|
14
|
|
|
* |
|
15
|
|
|
* @category Exchange |
|
16
|
|
|
* @package Exchange |
|
17
|
|
|
* @author Graham A. Sutton <[email protected]> |
|
18
|
|
|
* @license MIT <http://mit.org> |
|
19
|
|
|
* @link http://mit.org |
|
20
|
|
|
*/ |
|
21
|
|
|
class Exchange |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* The exchange rate service. |
|
25
|
|
|
* @var \Swap\Swap |
|
26
|
|
|
*/ |
|
27
|
|
|
private $service; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Constructor |
|
31
|
|
|
* |
|
32
|
|
|
* Accepts the name of an exchange rates service from Swap. Default is set to |
|
33
|
|
|
* use Fixer. |
|
34
|
|
|
* |
|
35
|
|
|
* @param \Swap\Swap $service The service to query exchange rates from. |
|
36
|
|
|
*/ |
|
37
|
9 |
|
public function __construct($service) |
|
38
|
|
|
{ |
|
39
|
9 |
|
$this->service = $service; |
|
40
|
9 |
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Add two \MoneyMan\Money objects together. This will return a new \MoneyMan\Money |
|
44
|
|
|
* object. |
|
45
|
|
|
* |
|
46
|
|
|
* IMPORTANT! If the currencies are different, the second parameter will be converted |
|
47
|
|
|
* to the first parameter's currency and then added together. |
|
48
|
|
|
* |
|
49
|
|
|
* @param \MoneyMan\Money $money1 The first money object. |
|
50
|
|
|
* @param \MoneyMan\Money $money2 The second money object. |
|
51
|
|
|
* |
|
52
|
|
|
* @return \MoneyMan\Money |
|
53
|
|
|
*/ |
|
54
|
2 |
|
public function add(Money $money1, Money $money2) |
|
55
|
|
|
{ |
|
56
|
|
|
// Convert the second money object to the first money object's currency |
|
57
|
2 |
|
$converted_money = $this->exchange($money2, $money1->getCurrency()); |
|
58
|
|
|
|
|
59
|
|
|
// Return the new \MoneyMan\Money in the first parameter's currency |
|
60
|
2 |
|
return $money1->add($converted_money); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Subtract one \MoneyMan\Money object from another. This will return a brand new |
|
65
|
|
|
* \MoneyMan\Money object. |
|
66
|
|
|
* |
|
67
|
|
|
* IMPORTANT! If the currencies are different, the second parameter will be converted |
|
68
|
|
|
* to the first parameter's currency. Afterwards, the second parameter's exchanged |
|
69
|
|
|
* amount will be deducted from the first parameter's amount and returned in a |
|
70
|
|
|
* new \MoneyMan\Money object. |
|
71
|
|
|
* |
|
72
|
|
|
* @param \MoneyMan\Money $money1 The first money object |
|
73
|
|
|
* @param \MoneyMan\Money $money2 The second money object |
|
74
|
|
|
* |
|
75
|
|
|
* @return \MoneyMan\Money |
|
76
|
|
|
*/ |
|
77
|
2 |
|
public function subtract(Money $money1, Money $money2) |
|
78
|
|
|
{ |
|
79
|
|
|
// Convert the second money object to the first money object's currency |
|
80
|
2 |
|
$converted_money = $this->exchange($money2, $money1->getCurrency()); |
|
81
|
|
|
|
|
82
|
|
|
// Return the new \MoneyMan\Money in the first parameter's currency |
|
83
|
2 |
|
return $money1->subtract($converted_money); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Exchanges one \MoneyMan\Money object into the desired currency. |
|
88
|
|
|
* |
|
89
|
|
|
* This should return a brand new \MoneyMan\Money object. |
|
90
|
|
|
* |
|
91
|
|
|
* @param \MoneyMan\Money $base The money we are exchanging. |
|
92
|
|
|
* @param \MoneyMan\Currency $quote The currency we are exchanging to. |
|
93
|
|
|
* |
|
94
|
|
|
* @return \MoneyMan\Money |
|
95
|
|
|
*/ |
|
96
|
9 |
|
public function exchange(Money $base, Currency $quote) |
|
97
|
|
|
{ |
|
98
|
|
|
// If they are of the same currency, there's no need to query the service. |
|
99
|
9 |
|
$exchange_rate = $base->getCurrency()->equals($quote) |
|
100
|
2 |
|
? 1.00000 |
|
101
|
9 |
|
: $this->getExchangeRate($base->getCurrency(), $quote); |
|
102
|
|
|
|
|
103
|
9 |
|
return new Money( |
|
104
|
9 |
|
(int) round($base->getAmount() * $exchange_rate, 0), |
|
105
|
|
|
$quote |
|
106
|
|
|
); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Gets the exchange rate between the base and quote currencies. |
|
111
|
|
|
* |
|
112
|
|
|
* @param Currency $base The currency we want to exchange from. |
|
113
|
|
|
* @param Currency $quote The currency we want to exchange to. |
|
114
|
|
|
* |
|
115
|
|
|
* @return float |
|
116
|
|
|
*/ |
|
117
|
7 |
|
public function getExchangeRate(Currency $base, Currency $quote) |
|
118
|
|
|
{ |
|
119
|
7 |
|
$base_code = $base->getCode(); |
|
120
|
7 |
|
$quote_code = $quote->getCode(); |
|
121
|
|
|
|
|
122
|
7 |
|
return $this->service->latest("$base_code/$quote_code")->getValue(); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|