@@ -19,11 +19,10 @@ class CurrencyLayer extends BaseCurrencyDriver implements CurrencyDriverContract
19
19
/** @var string $baseCurrency CurrencyLayer's Free Plan base currency is 'USD' */
20
20
protected $ baseCurrency = Symbol::USD ;
21
21
22
- protected $ httpParams = [
23
- 'format ' => 1
22
+ protected $ httpParams = [
23
+ 'format ' => 1 ,
24
24
];
25
25
26
-
27
26
/**
28
27
* @param string|array $forCurrency
29
28
*
@@ -40,12 +39,45 @@ public function get($forCurrency = []): ConversionResult
40
39
// Get API response
41
40
$ response = $ this ->apiRequest ('live ' , [
42
41
'source ' => $ this ->getBaseCurrency (),
43
- 'currencies ' => join (', ' , $ this ->getSymbols ())
42
+ 'currencies ' => join (', ' , $ this ->getSymbols ()),
44
43
]);
45
44
46
45
// Transform rates response
47
46
$ rates = [];
48
- foreach ($ response ['quotes ' ] as $ currency => $ rate ) {
47
+ foreach ($ response ['quotes ' ] as $ currency => $ rate ) {
48
+ $ rates [substr ($ currency , 3 , 3 )] = $ rate ;
49
+ }
50
+
51
+ return new ConversionResult ($ response ['source ' ], $ response ['timestamp ' ], $ rates );
52
+ }
53
+
54
+ /**
55
+ * @param int|string|DateTime $date
56
+ * @param string|array $forCurrency
57
+ *
58
+ * @return ConversionResult
59
+ *
60
+ * @throws CurrencyException
61
+ */
62
+ public function historical ($ date = null , $ forCurrency = []): ConversionResult
63
+ {
64
+ // Set date
65
+ $ this ->date ($ date );
66
+
67
+ if (!empty ((array )$ forCurrency )) {
68
+ $ this ->currencies ((array )$ forCurrency );
69
+ }
70
+
71
+ // Get API response
72
+ $ response = $ this ->apiRequest ('historical ' , [
73
+ 'date ' => $ this ->date ,
74
+ 'source ' => $ this ->getBaseCurrency (),
75
+ 'currencies ' => join (', ' , $ this ->getSymbols ()),
76
+ ]);
77
+
78
+ // Transform rates response
79
+ $ rates = [];
80
+ foreach ($ response ['quotes ' ] as $ currency => $ rate ) {
49
81
$ rates [substr ($ currency , 3 , 3 )] = $ rate ;
50
82
}
51
83
@@ -69,64 +101,35 @@ public function convert(float $amount = null, string $fromCurrency = null, strin
69
101
$ this ->date ($ date );
70
102
71
103
// Overwrite/set params
72
- if ($ amount !== null ) {
104
+ if ($ amount !== null ) {
73
105
$ this ->amount = $ amount ;
74
106
}
75
107
76
- if ($ fromCurrency !== null ) {
108
+ if ($ fromCurrency !== null ) {
77
109
$ this ->baseCurrency = $ fromCurrency ;
78
110
}
79
111
80
- if ($ toCurrency !== null ) {
112
+ if ($ toCurrency !== null ) {
81
113
$ this ->currencies = [$ toCurrency ];
82
114
}
83
115
84
- // Get API response
85
- $ response = $ this ->apiRequest ('convert ' , [
116
+ $ params = [
86
117
'from ' => $ this ->getBaseCurrency (),
87
118
'to ' => reset ($ this ->currencies ),
88
- 'amount ' => $ this ->amount
89
- ]);
90
-
91
- // Return the rate as a float
92
- return floatval ($ response ['info ' ]['rate ' ]);
93
- }
94
-
95
- /**
96
- * @param int|string|DateTime $date
97
- * @param string|array $forCurrency
98
- *
99
- * @return ConversionResult
100
- *
101
- * @throws CurrencyException
102
- * @throws \GuzzleHttp\Exception\GuzzleException
103
- */
104
- public function historical ($ date = null , $ forCurrency = []): ConversionResult
105
- {
106
- // Set date
107
- $ this ->date ($ date );
119
+ 'amount ' => $ this ->amount ,
120
+ ];
108
121
109
- if (! empty (( array ) $ forCurrency )) {
110
- $ this ->currencies (( array ) $ forCurrency );
122
+ if (null !== $ this -> getDate ( )) {
123
+ $ params [ ' date ' ] = $ this ->getDate ( );
111
124
}
112
125
113
126
// Get API response
114
- $ response = $ this ->apiRequest ('historical ' , [
115
- 'date ' => $ this ->date ,
116
- 'source ' => $ this ->getBaseCurrency (),
117
- 'currencies ' => join (', ' , $ this ->getSymbols ())
118
- ]);
119
-
120
- // Transform rates response
121
- $ rates = [];
122
- foreach ($ response ['quotes ' ] as $ currency => $ rate ) {
123
- $ rates [substr ($ currency , 3 , 3 )] = $ rate ;
124
- }
127
+ $ response = $ this ->apiRequest ('convert ' , $ params );
125
128
126
- return new ConversionResult ($ response ['source ' ], $ response ['timestamp ' ], $ rates );
129
+ // Return the rate as a float
130
+ return floatval ($ response ['result ' ]);
127
131
}
128
132
129
-
130
133
/**
131
134
* Performs an HTTP request.
132
135
*
@@ -150,5 +153,4 @@ function apiRequest(string $endpoint, array $params = [], string $method = 'GET'
150
153
151
154
return $ response ;
152
155
}
153
-
154
156
}
0 commit comments