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

Commit 12d3139

Browse files
committed
Adding latest and historical rates
1 parent d836498 commit 12d3139

15 files changed

+593
-89
lines changed

README.md

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
# Laravel Currency
22
![Tests](https://github.com/amrshawky/laravel-currency/workflows/Tests/badge.svg?branch=master)
33

4-
Laravel currency is a simple package for converting currencies 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 currency conversion, latest and historical exchange rates based on the free API [exchangerate.host](https://exchangerate.host "exchangerate.host Homepage") - no API keys needed!
55

6-
## Minimum requirements
7-
Laravel 6
6+
## Requirements
7+
- PHP >= 7.2
8+
- Laravel >= 6.0
9+
- guzzlehttp >= 6.0
810

911
## Installation
1012
```
@@ -13,6 +15,7 @@ composer require amrshawky/laravel-currency
1315

1416
## Usage
1517

18+
### 1. Currency Conversion
1619
To convert from one currency to another you may chain the methods like so:
1720
```php
1821
Currency::convert()
@@ -31,7 +34,7 @@ Currency::convert()
3134
->amount(50)
3235
->get();
3336
```
34-
## Available Methods
37+
#### Available Methods
3538
- Convert currency using historical exchange rates `YYYY-MM-DD`:
3639

3740
```php
@@ -61,10 +64,53 @@ Currency::convert()
6164
->source('ecb')
6265
->get();
6366
```
67+
68+
### 2. Latest Rates
69+
To get latest rates you may chain the methods like so:
70+
```php
71+
Currency::rates()
72+
->latest()
73+
->get();
74+
```
75+
This will return an `array` of all available currencies or `null` on failure.
76+
77+
#### Available Methods
78+
- Just like currency conversion you may chain any of the available methods like so:
79+
```php
80+
Currency::rates()
81+
->latest()
82+
->symbols(['USD', 'EUR', 'EGP']) //An array of currency codes to limit output currencies
83+
->base('GBP') //Changing base currency. Enter the three-letter currency code of your preferred base currency.
84+
->amount(5.66) //Specify the amount to be converted
85+
->round(2) //Round numbers to decimal places
86+
->source('ecb') //Switch data source between forex `default` or bank view
87+
->get();
88+
```
89+
90+
### 3. Historical Rates
91+
Historical rates are available for most currencies all the way back to the year of 1999.
92+
```php
93+
Currency::rates()
94+
->historical('2020-01-01') // `YYYY-MM-DD` Required date parameter to get the rates for
95+
->get();
96+
```
97+
Same as latest rates you may chain any of the available methods like so:
98+
```php
99+
Currency::rates()
100+
->historical('2020-01-01')
101+
->symbols(['USD', 'EUR', 'EGP'])
102+
->base('GBP')
103+
->amount(5.66)
104+
->round(2)
105+
->source('ecb')
106+
->get();
107+
```
64108
More information regarding list of bank sources [here](https://api.exchangerate.host/sources "List of bank sources")
65109

110+
For a list of all supported symbols [here](https://api.exchangerate.host/symbols "List of supported symbols")
111+
66112
## More features
67-
coming soon!
113+
Coming soon!
68114

69115
## License
70116
The MIT License (MIT). Please see [LICENSE](../master/LICENSE) for more information.

composer.json

Lines changed: 3 additions & 2 deletions
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 and latest exchange rates based on the free API provided by exchangerate.host",
3+
"description": "A Laravel package for currency conversion, latest and historical exchange rates based on the free API provided by exchangerate.host",
44
"keywords": [
55
"laravel",
66
"currency",
@@ -18,7 +18,8 @@
1818
"require": {
1919
"php": "^7.2",
2020
"guzzlehttp/guzzle": ">=6.0",
21-
"ext-json": "*"
21+
"ext-json": "*",
22+
"illuminate/support": "6.*|7.*|8.*"
2223
},
2324
"require-dev": {
2425
"phpunit/phpunit": ">=8.0"

src/API.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
namespace AmrShawky\Currency;
4+
5+
use AmrShawky\Currency\Traits\HttpRequest;
6+
use Closure;
7+
8+
abstract class API
9+
{
10+
use HttpRequest;
11+
12+
/**
13+
* @var null
14+
*/
15+
protected $base_url = null;
16+
17+
/**
18+
* @var array
19+
*/
20+
protected $query_params = [];
21+
22+
/**
23+
* @var array
24+
*/
25+
protected $query_params_callback = null;
26+
27+
/**
28+
* @param Object $response
29+
*
30+
* @return mixed
31+
*/
32+
protected abstract function getResults(Object $response);
33+
34+
protected function buildQueryParams()
35+
{
36+
if ($this->query_params_callback !== null) {
37+
$this->query_params = call_user_func($this->query_params_callback);
38+
}
39+
40+
if (!is_array($this->query_params)) {
41+
throw new \Exception('Query params should be an array!');
42+
}
43+
44+
if (property_exists(get_called_class(), 'params') && !empty($this->params)) {
45+
foreach ($this->params as $key => $param) {
46+
$this->query_params[$key] = $param;
47+
}
48+
}
49+
}
50+
51+
/**
52+
* @return mixed|null
53+
*/
54+
public function get()
55+
{
56+
$this->buildQueryParams();
57+
58+
$response = $this->request(
59+
$this->base_url,
60+
$this->query_params
61+
);
62+
63+
return $response ? $this->getResults($response) : null;
64+
}
65+
66+
/**
67+
* @param Closure $callback
68+
*/
69+
protected function setQueryParams(Closure $callback)
70+
{
71+
$this->query_params_callback = $callback;
72+
}
73+
}

src/Currency.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,21 @@
22

33
namespace AmrShawky\Currency;
44

5-
use GuzzleHttp\Client;
6-
75
class Currency
86
{
7+
/**
8+
* @return CurrencyConversion
9+
*/
910
public function convert()
1011
{
11-
return new CurrencyConversion(
12-
new Client()
13-
);
12+
return new CurrencyConversion();
13+
}
14+
15+
/**
16+
* @return CurrencyRatesProxy
17+
*/
18+
public function rates()
19+
{
20+
return new CurrencyRatesProxy();
1421
}
1522
}

src/CurrencyConversion.php

Lines changed: 51 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,16 @@
22

33
namespace AmrShawky\Currency;
44

5-
use AmrShawky\Currency\Traits\HttpRequest;
65
use AmrShawky\Currency\Traits\ParamsOverload;
76
use GuzzleHttp\Client;
87

9-
class CurrencyConversion
8+
class CurrencyConversion extends API
109
{
11-
use HttpRequest, ParamsOverload;
12-
10+
use ParamsOverload;
1311
/**
1412
* @var string
1513
*/
16-
private $base_url = 'https://api.exchangerate.host/convert';
14+
protected $base_url = 'https://api.exchangerate.host/convert';
1715

1816
/**
1917
* Required base currency
@@ -30,34 +28,55 @@ class CurrencyConversion
3028
private $to = null;
3129

3230
/**
33-
* @var float
31+
* @var null
3432
*/
35-
private $amount = 1.00;
33+
private $places = null;
3634

3735
/**
38-
* @var array
36+
* @var float
3937
*/
40-
private $params = [];
38+
private $amount = 1.00;
4139

4240
/**
4341
* @var array
4442
*/
45-
private $query_params = [];
46-
47-
private $available_params = [
43+
protected $available_params = [
4844
'round',
4945
'date',
50-
'source'
46+
'source',
47+
'places'
5148
];
5249

5350
/**
54-
* @var
51+
* CurrencyConversion constructor.
52+
*
53+
* @param Client|null $client
5554
*/
56-
private $client;
57-
58-
public function __construct(Client $client)
55+
public function __construct(?Client $client = null)
5956
{
60-
$this->client = $client;
57+
$this->client = $client ?? new Client();
58+
59+
$this->setQueryParams(function () {
60+
if (!$this->from) {
61+
throw new \Exception('Base currency is not specified!');
62+
}
63+
64+
if (!$this->to) {
65+
throw new \Exception('Target currency is not specified!');
66+
}
67+
68+
$params = [
69+
'from' => $this->from,
70+
'to' => $this->to,
71+
'amount' => $this->amount
72+
];
73+
74+
if ($this->places) {
75+
$params['places'] = $this->places;
76+
}
77+
78+
return $params;
79+
});
6180
}
6281

6382
/**
@@ -82,6 +101,17 @@ public function to(string $currency)
82101
return $this;
83102
}
84103

104+
/**
105+
* @param $places
106+
*
107+
* @return $this
108+
*/
109+
public function round(int $places)
110+
{
111+
$this->places = $places;
112+
return $this;
113+
}
114+
85115
/**
86116
* @param float $amount
87117
*
@@ -94,42 +124,12 @@ public function amount(float $amount)
94124
}
95125

96126
/**
97-
* @throws \Exception
127+
* @param object $response
98128
*
99-
* @return float|null
129+
* @return mixed|null
100130
*/
101-
public function get()
131+
protected function getResults(object $response)
102132
{
103-
if (!$this->from) {
104-
throw new \Exception('Base currency is not specified!');
105-
}
106-
107-
if (!$this->to) {
108-
throw new \Exception('Target currency is not specified!');
109-
}
110-
111-
$this->buildQueryParams();
112-
113-
$response = $this->request(
114-
$this->base_url,
115-
$this->query_params
116-
);
117-
118133
return $response->result ?? null;
119134
}
120-
121-
private function buildQueryParams()
122-
{
123-
$this->query_params = [
124-
'from' => $this->from,
125-
'to' => $this->to,
126-
'amount' => $this->amount
127-
];
128-
129-
if (!empty($this->params)) {
130-
foreach ($this->params as $key => $param) {
131-
$this->query_params[$key] = $param;
132-
}
133-
}
134-
}
135135
}

src/CurrencyHistoricalRates.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
4+
namespace AmrShawky\Currency;
5+
6+
7+
use GuzzleHttp\Client;
8+
9+
class CurrencyHistoricalRates extends CurrencyRates
10+
{
11+
/**
12+
* CurrencyHistoricalRates constructor.
13+
*
14+
* @param string $date
15+
* @param Client|null $client
16+
*/
17+
public function __construct(string $date, ?Client $client = null)
18+
{
19+
parent::__construct($client);
20+
$this->base_url = "https://api.exchangerate.host/{$date}";
21+
}
22+
}

0 commit comments

Comments
 (0)