44use Otherguy \Currency \Exceptions \ApiException ;
55use Otherguy \Currency \Exceptions \CurrencyException ;
66use Otherguy \Currency \Results \ConversionResult ;
7+ use Otherguy \Currency \Symbol ;
78
89/**
910 * Class FixerIo
@@ -16,7 +17,7 @@ class FixerIo extends BaseCurrencyDriver implements CurrencyDriverContract
1617 protected $ apiURL = 'data.fixer.io/api ' ;
1718
1819 /** @var string $baseCurrency Fixer.io's Free Plan base currency is 'EUR' */
19- protected $ baseCurrency = ' EUR ' ;
20+ protected $ baseCurrency = Symbol:: EUR ;
2021
2122 /**
2223 * @param string|array $forCurrency
@@ -34,94 +35,100 @@ public function get($forCurrency = []): ConversionResult
3435 // Get API response
3536 $ response = $ this ->apiRequest ('latest ' , [
3637 'base ' => $ this ->getBaseCurrency (),
37- 'symbols ' => join (', ' , $ this ->getSymbols ())
38+ 'symbols ' => join (', ' , $ this ->getSymbols ()),
3839 ]);
3940
4041 return new ConversionResult ($ response ['base ' ], $ response ['date ' ], $ response ['rates ' ]);
4142 }
4243
4344 /**
44- * Converts any amount in a given currency to another currency.
45- *
46- * @param float $amount The amount to convert.
47- * @param string $fromCurrency The base currency.
48- * @param string $toCurrency The target currency.
45+ * @param int|string|DateTime $date
46+ * @param string|array $forCurrency
4947 *
50- * @return float The conversion result.
48+ * @return ConversionResult
5149 *
5250 * @throws CurrencyException
5351 */
54- public function convert ( float $ amount = null , string $ fromCurrency = null , string $ toCurrency = null ): float
52+ public function historical ( $ date = null , $ forCurrency = [] ): ConversionResult
5553 {
56- // Overwrite/set params
57- if ($ amount !== null ) {
58- $ this ->amount = $ amount ;
59- }
60-
61- if ($ fromCurrency !== null ) {
62- $ this ->baseCurrency = $ fromCurrency ;
63- }
54+ // Set date
55+ $ this ->date ($ date );
6456
65- if ( $ toCurrency !== null ) {
66- $ this ->currencies = [ $ toCurrency ] ;
57+ if (! empty (( array ) $ forCurrency ) ) {
58+ $ this ->currencies (( array ) $ forCurrency ) ;
6759 }
6860
6961 // Get API response
70- $ response = $ this ->apiRequest ('convert ' , [
71- 'from ' => $ this ->getBaseCurrency (),
72- 'to ' => reset ($ this ->currencies ),
73- 'amount ' => $ this ->amount
62+ $ response = $ this ->apiRequest ($ this ->date , [
63+ 'base ' => $ this ->getBaseCurrency (),
64+ 'symbols ' => join (', ' , $ this ->getSymbols ()),
7465 ]);
7566
76- // Return the rate as a float
77- return floatval ($ response ['info ' ]['rate ' ]);
67+ return new ConversionResult ($ response ['base ' ], $ response ['date ' ], $ response ['rates ' ]);
7868 }
7969
8070 /**
81- * @param int|string|DateTime $date
82- * @param string|array $forCurrency
71+ * Converts any amount in a given currency to another currency.
8372 *
84- * @return ConversionResult
73+ * @param float $amount The amount to convert.
74+ * @param string $fromCurrency The base currency.
75+ * @param string $toCurrency The target currency.
76+ * @param int|string|DateTime $date The date to get the conversion rate for.
8577 *
86- * @throws CurrencyException
78+ * @return float The conversion result.
79+ *
80+ * @throws ApiException
8781 */
88- public function historical ( $ date = null , $ forCurrency = [] ): ConversionResult
82+ public function convert ( float $ amount = null , string $ fromCurrency = null , string $ toCurrency = null , $ date = null ): float
8983 {
9084 // Set date
9185 $ this ->date ($ date );
9286
93- if (!empty ((array )$ forCurrency )) {
94- $ this ->currencies ((array )$ forCurrency );
87+ // Overwrite/set params
88+ if ($ amount !== null ) {
89+ $ this ->amount = $ amount ;
90+ }
91+
92+ if ($ fromCurrency !== null ) {
93+ $ this ->baseCurrency = $ fromCurrency ;
94+ }
95+
96+ if ($ toCurrency !== null ) {
97+ $ this ->currencies = [$ toCurrency ];
98+ }
99+
100+ $ params = [
101+ 'from ' => $ this ->getBaseCurrency (),
102+ 'to ' => reset ($ this ->currencies ),
103+ 'amount ' => $ this ->amount ,
104+ ];
105+
106+ if (null !== $ this ->getDate ()) {
107+ $ params ['date ' ] = $ this ->getDate ();
95108 }
96109
97110 // Get API response
98- $ response = $ this ->apiRequest ($ this ->date , [
99- 'base ' => $ this ->getBaseCurrency (),
100- 'symbols ' => join (', ' , $ this ->getSymbols ())
101- ]);
111+ $ response = $ this ->apiRequest ('convert ' , $ params );
102112
103- return new ConversionResult ($ response ['base ' ], $ response ['date ' ], $ response ['rates ' ]);
113+ // Return the rate as a float
114+ return floatval ($ response ['result ' ]);
104115 }
105116
106117 /**
107118 * Performs an HTTP request.
108119 *
109120 * @param string $endpoint The API endpoint.
121+ * @param array $params The query parameters for this request.
110122 * @param string $method The HTTP method (defaults to 'GET').
111123 *
112124 * @return array|bool The response as decoded JSON.
113125 *
114- * @throws CurrencyException
126+ * @throws ApiException
115127 */
116- public function apiRequest (string $ endpoint , string $ method = 'GET ' )
128+ function apiRequest (string $ endpoint, array $ params = [] , string $ method = 'GET ' )
117129 {
118130 // Perform actual API request.
119- $ response = parent ::apiRequest ($ endpoint , $ method );
120-
121- // If the response is not an array, something went wrong.
122- if (! is_array ($ response )) {
123- throw new ApiException ('Unexpected API response! ' );
124- }
131+ $ response = parent ::apiRequest ($ endpoint , $ params , $ method );
125132
126133 // Handle response exceptions.
127134 if ($ response ['success ' ] == false ) {
0 commit comments