@@ -19,11 +19,10 @@ class CurrencyLayer extends BaseCurrencyDriver implements CurrencyDriverContract
1919 /** @var string $baseCurrency CurrencyLayer's Free Plan base currency is 'USD' */
2020 protected $ baseCurrency = Symbol::USD ;
2121
22- protected $ httpParams = [
23- 'format ' => 1
22+ protected $ httpParams = [
23+ 'format ' => 1 ,
2424 ];
2525
26-
2726 /**
2827 * @param string|array $forCurrency
2928 *
@@ -40,12 +39,45 @@ public function get($forCurrency = []): ConversionResult
4039 // Get API response
4140 $ response = $ this ->apiRequest ('live ' , [
4241 'source ' => $ this ->getBaseCurrency (),
43- 'currencies ' => join (', ' , $ this ->getSymbols ())
42+ 'currencies ' => join (', ' , $ this ->getSymbols ()),
4443 ]);
4544
4645 // Transform rates response
4746 $ 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 ) {
4981 $ rates [substr ($ currency , 3 , 3 )] = $ rate ;
5082 }
5183
@@ -69,64 +101,35 @@ public function convert(float $amount = null, string $fromCurrency = null, strin
69101 $ this ->date ($ date );
70102
71103 // Overwrite/set params
72- if ($ amount !== null ) {
104+ if ($ amount !== null ) {
73105 $ this ->amount = $ amount ;
74106 }
75107
76- if ($ fromCurrency !== null ) {
108+ if ($ fromCurrency !== null ) {
77109 $ this ->baseCurrency = $ fromCurrency ;
78110 }
79111
80- if ($ toCurrency !== null ) {
112+ if ($ toCurrency !== null ) {
81113 $ this ->currencies = [$ toCurrency ];
82114 }
83115
84- // Get API response
85- $ response = $ this ->apiRequest ('convert ' , [
116+ $ params = [
86117 'from ' => $ this ->getBaseCurrency (),
87118 '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+ ];
108121
109- if (! empty (( array ) $ forCurrency )) {
110- $ this ->currencies (( array ) $ forCurrency );
122+ if (null !== $ this -> getDate ( )) {
123+ $ params [ ' date ' ] = $ this ->getDate ( );
111124 }
112125
113126 // 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 );
125128
126- return new ConversionResult ($ response ['source ' ], $ response ['timestamp ' ], $ rates );
129+ // Return the rate as a float
130+ return floatval ($ response ['result ' ]);
127131 }
128132
129-
130133 /**
131134 * Performs an HTTP request.
132135 *
@@ -150,5 +153,4 @@ function apiRequest(string $endpoint, array $params = [], string $method = 'GET'
150153
151154 return $ response ;
152155 }
153-
154156}
0 commit comments