Skip to content
This repository was archived by the owner on Oct 14, 2023. It is now read-only.

Commit 591ad72

Browse files
committed
Timeseries rates
1 parent 34bf2fd commit 591ad72

File tree

7 files changed

+233
-21
lines changed

7 files changed

+233
-21
lines changed

README.md

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
# Laravel Currency
22
![Tests](https://github.com/amrshawky/laravel-currency/workflows/Tests/badge.svg?branch=master) ![Packagist License](https://img.shields.io/packagist/l/amrshawky/laravel-currency?color=success&label=License) ![Packagist Version](https://img.shields.io/packagist/v/amrshawky/laravel-currency?label=Packagist) ![Packagist Downloads](https://img.shields.io/packagist/dt/amrshawky/laravel-currency?color=success&label=Downloads)
33

4-
Laravel currency is a simple package for currency conversion, latest and historical exchange rates based on the free API [exchangerate.host](https://exchangerate.host "exchangerate.host Homepage") - no API keys needed!
4+
Laravel currency is a simple package for current and historical currency exchange rates & crypto exchange rates. based on the free API [exchangerate.host](https://exchangerate.host "exchangerate.host Homepage") - no API keys needed!
55

66
## Requirements
77
- PHP >= 7.2
88
- Laravel >= 6.0
99
- guzzlehttp >= 6.0
1010

1111
## Installation
12+
1213
```
1314
composer require amrshawky/laravel-currency
1415
```
1516

1617
## Usage
1718

1819
### 1. Currency Conversion
19-
To convert from one currency to another you may chain the methods like so:
20+
To convert from one currency to another you may chain the methods:
21+
2022
```php
2123
use AmrShawky\Currency\Facade\Currency;
2224

@@ -77,6 +79,7 @@ Currency::convert()
7779

7880
### 2. Latest Rates
7981
To get latest rates you may chain the methods:
82+
8083
```php
8184
use AmrShawky\Currency\Facade\Currency;
8285

@@ -88,6 +91,7 @@ This will return an `array` of all available currencies or `null` on failure.
8891

8992
#### Available Methods
9093
- Just like currency conversion you may chain any of the available methods:
94+
9195
```php
9296
use AmrShawky\Currency\Facade\Currency;
9397

@@ -97,17 +101,18 @@ Currency::rates()
97101
->base('GBP') //Changing base currency (default: EUR). Enter the three-letter currency code of your preferred base currency.
98102
->amount(5.66) //Specify the amount to be converted
99103
->round(2) //Round numbers to decimal places
100-
->source('ecb') //Switch data source between forex `default` or bank view
104+
->source('ecb') //Switch data source between forex `default`, bank view or crypto currencies.
101105
->get();
102106
```
103107

104108
### 3. Historical Rates
105109
Historical rates are available for most currencies all the way back to the year of 1999.
110+
106111
```php
107112
use AmrShawky\Currency\Facade\Currency;
108113

109114
Currency::rates()
110-
->historical('2020-01-01') // `YYYY-MM-DD` Required date parameter to get the rates for
115+
->historical('2020-01-01') //`YYYY-MM-DD` Required date parameter to get the rates for
111116
->get();
112117
```
113118
Same as latest rates you may chain any of the available methods:
@@ -116,13 +121,30 @@ use AmrShawky\Currency\Facade\Currency;
116121

117122
Currency::rates()
118123
->historical('2020-01-01')
119-
->symbols(['USD', 'EUR', 'EGP'])
124+
->symbols(['USD', 'EUR', 'CZK'])
120125
->base('GBP')
121126
->amount(5.66)
122127
->round(2)
123128
->source('ecb')
124129
->get();
125130
```
131+
### 4. Timeseries Rates
132+
Timeseries are for daily historical rates between two dates of your choice, with a maximum time frame of 365 days.
133+
This will return an `array` with keys as dates and values as an `array` of rates or `null` on failure.
134+
135+
```php
136+
use AmrShawky\Currency\Facade\Currency;
137+
138+
Currency::rates()
139+
->timeSeries('2021-05-01', '2021-05-04') //`YYYY-MM-DD` Required dates range parameters
140+
->symbols(['USD', 'EUR', 'EGP']) //[optional] An array of currency codes to limit output currencies
141+
->base('GBP') //[optional] Changing base currency (default: EUR). Enter the three-letter currency code of your preferred base currency.
142+
->amount(5.66) //[optional] Specify the amount to be converted (default: 1)
143+
->round(2) //[optional] Round numbers to decimal places
144+
->source('ecb') //[optional] Switch data source between forex `default`, bank view or crypto currencies.
145+
->get();
146+
```
147+
126148
### Throwing Exceptions
127149
The default behavior is to return `null` for errors that occur during the request _(connection timeout, DNS errors, client or server error status code, missing API success parameter, etc.)_.
128150

@@ -203,8 +225,5 @@ More information regarding list of bank sources [here](https://api.exchangerate.
203225

204226
For a list of all supported symbols [here](https://api.exchangerate.host/symbols "List of supported symbols")
205227

206-
## More features
207-
Coming soon!
208-
209228
## License
210229
The MIT License (MIT). Please see [LICENSE](../master/LICENSE) for more information.

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "amrshawky/laravel-currency",
3-
"description": "A Laravel package for currency conversion, latest and historical exchange rates based on the free API provided by exchangerate.host",
3+
"description": "A Laravel package for current and historical currency exchange rates & crypto exchange rates based on the free API provided by exchangerate.host",
44
"keywords": [
55
"laravel",
66
"currency",

docs/index.md

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
# Laravel Currency
22
![Tests](https://github.com/amrshawky/laravel-currency/workflows/Tests/badge.svg?branch=master) ![Packagist License](https://img.shields.io/packagist/l/amrshawky/laravel-currency?color=success&label=License) ![Packagist Version](https://img.shields.io/packagist/v/amrshawky/laravel-currency?label=Packagist) ![Packagist Downloads](https://img.shields.io/packagist/dt/amrshawky/laravel-currency?color=success&label=Downloads)
33

4-
Laravel currency is a simple package for currency conversion, latest and historical exchange rates based on the free API [exchangerate.host](https://exchangerate.host "exchangerate.host Homepage") - no API keys needed!
4+
Laravel currency is a simple package for current and historical currency exchange rates & crypto exchange rates. based on the free API [exchangerate.host](https://exchangerate.host "exchangerate.host Homepage") - no API keys needed!
55

66
## Requirements
77
- PHP >= 7.2
88
- Laravel >= 6.0
99
- guzzlehttp >= 6.0
1010

1111
## Installation
12+
1213
```
1314
composer require amrshawky/laravel-currency
1415
```
1516

1617
## Usage
1718

1819
### 1. Currency Conversion
19-
To convert from one currency to another you may chain the methods like so:
20+
To convert from one currency to another you may chain the methods:
21+
2022
```php
2123
use AmrShawky\Currency\Facade\Currency;
2224

@@ -85,7 +87,6 @@ Currency::rates()
8587
->latest()
8688
->get();
8789
```
88-
8990
This will return an `array` of all available currencies or `null` on failure.
9091

9192
#### Available Methods
@@ -100,7 +101,7 @@ Currency::rates()
100101
->base('GBP') //Changing base currency (default: EUR). Enter the three-letter currency code of your preferred base currency.
101102
->amount(5.66) //Specify the amount to be converted
102103
->round(2) //Round numbers to decimal places
103-
->source('ecb') //Switch data source between forex `default` or bank view
104+
->source('ecb') //Switch data source between forex `default`, bank view or crypto currencies.
104105
->get();
105106
```
106107

@@ -111,23 +112,39 @@ Historical rates are available for most currencies all the way back to the year
111112
use AmrShawky\Currency\Facade\Currency;
112113

113114
Currency::rates()
114-
->historical('2020-01-01') // `YYYY-MM-DD` Required date parameter to get the rates for
115+
->historical('2020-01-01') //`YYYY-MM-DD` Required date parameter to get the rates for
115116
->get();
116117
```
117118
Same as latest rates you may chain any of the available methods:
118-
119119
```php
120120
use AmrShawky\Currency\Facade\Currency;
121121

122122
Currency::rates()
123123
->historical('2020-01-01')
124-
->symbols(['USD', 'EUR', 'EGP'])
124+
->symbols(['USD', 'EUR', 'CZK'])
125125
->base('GBP')
126126
->amount(5.66)
127127
->round(2)
128128
->source('ecb')
129129
->get();
130130
```
131+
### 4. Timeseries Rates
132+
Timeseries are for daily historical rates between two dates of your choice, with a maximum time frame of 365 days.
133+
This will return an `array` with keys as dates and values as an `array` of rates or `null` on failure.
134+
135+
```php
136+
use AmrShawky\Currency\Facade\Currency;
137+
138+
Currency::rates()
139+
->timeSeries('2021-05-01', '2021-05-04') //`YYYY-MM-DD` Required dates range parameters
140+
->symbols(['USD', 'EUR', 'EGP']) //[optional] An array of currency codes to limit output currencies
141+
->base('GBP') //[optional] Changing base currency (default: EUR). Enter the three-letter currency code of your preferred base currency.
142+
->amount(5.66) //[optional] Specify the amount to be converted (default: 1)
143+
->round(2) //[optional] Round numbers to decimal places
144+
->source('ecb') //[optional] Switch data source between forex `default`, bank view or crypto currencies.
145+
->get();
146+
```
147+
131148
### Throwing Exceptions
132149
The default behavior is to return `null` for errors that occur during the request _(connection timeout, DNS errors, client or server error status code, missing API success parameter, etc.)_.
133150

@@ -208,8 +225,5 @@ More information regarding list of bank sources [here](https://api.exchangerate.
208225

209226
For a list of all supported symbols [here](https://api.exchangerate.host/symbols "List of supported symbols")
210227

211-
## More features
212-
Coming soon!
213-
214228
## License
215229
The MIT License (MIT). Please see [LICENSE](../master/LICENSE) for more information.

src/CurrencyRates.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,10 @@ public function round(int $places)
9898
*/
9999
protected function getResults(object $response)
100100
{
101-
if (!empty($response->rates)) {
102-
return (array) $response->rates;
101+
if (!empty($rates = (array) $response->rates)) {
102+
unset($response->rates);
103+
104+
return $rates;
103105
}
104106

105107
return null;

src/CurrencyRatesProxy.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,15 @@ public function historical(string $date)
2121
{
2222
return new CurrencyHistoricalRates($date);
2323
}
24+
25+
/**
26+
* @param string $date_from
27+
* @param string $date_to
28+
*
29+
* @return CurrencyTimeSeriesRates
30+
*/
31+
public function timeSeries(string $date_from, string $date_to)
32+
{
33+
return new CurrencyTimeSeriesRates($date_from, $date_to);
34+
}
2435
}

src/CurrencyTimeSeriesRates.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace AmrShawky\Currency;
4+
5+
use GuzzleHttp\Client;
6+
7+
class CurrencyTimeSeriesRates extends CurrencyRates
8+
{
9+
/**
10+
* CurrencyHistoricalRates constructor.
11+
*
12+
* @param string $start_date
13+
* @param string $end_date
14+
* @param Client|null $client
15+
*/
16+
public function __construct(string $start_date, string $end_date, ?Client $client = null)
17+
{
18+
parent::__construct($client);
19+
$this->base_url = "https://api.exchangerate.host/timeseries";
20+
$this->params['start_date'] = $start_date;
21+
$this->params['end_date'] = $end_date;
22+
}
23+
24+
/**
25+
* @param object $response
26+
*
27+
* @return mixed|null
28+
*/
29+
protected function getResults(object $response)
30+
{
31+
if (!empty($time_series = (array) $response->rates)) {
32+
foreach ($time_series as $date => $rates) {
33+
$time_series[$date] = (array) $rates;
34+
}
35+
unset($response->rates);
36+
37+
return $time_series;
38+
}
39+
40+
return null;
41+
}
42+
}

0 commit comments

Comments
 (0)