-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit d841325
Showing
19 changed files
with
1,053 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) [2024] [Bruno Freire] | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
# PHP Frankfurter Currency Exchange | ||
|
||
A simple PHP library for exchanging currencies based on api.frankfurter.app | ||
|
||
## Installation | ||
|
||
Install via Composer: | ||
|
||
```bash | ||
composer require brunoinds/frankfurter-laravel | ||
``` | ||
|
||
## Usage | ||
|
||
The `Exchange` class provides methods for exchanging between BRL and USD: | ||
|
||
```php | ||
use Brunoinds\FrankfurterLaravel\Exchange; | ||
use Brunoinds\FrankfurterLaravel\Enums\Currency; | ||
|
||
// Get current exchange rate | ||
$result = Exchange::now()->convert(Currency::USD, 1)->to(Currency::BRL); | ||
|
||
// Get historical exchange rate | ||
$date = new DateTime('2023-12-10'); | ||
$result = Exchange::on($date) | ||
->convert(Currency::USD, 1) | ||
->to(Currency::BRL); | ||
echo $result // 0.27 | ||
|
||
``` | ||
|
||
The `Currency` enum provides constants for the supported currencies: | ||
|
||
```php | ||
use Brunoinds\FrankfurterLaravel\Enums\Currency; | ||
|
||
Currency::USD; | ||
Currency::BRL; | ||
Currency::EUR; | ||
Currency::AUD; | ||
Currency::BGN; | ||
Currency::CAD; | ||
Currency::CHF; | ||
Currency::CNY; | ||
Currency::CZK; | ||
Currency::DKK; | ||
Currency::GBP; | ||
Currency::HKD; | ||
Currency::HUF; | ||
Currency::IDR; | ||
Currency::ILS; | ||
Currency::INR; | ||
Currency::ISK; | ||
Currency::JPY; | ||
Currency::KRW; | ||
Currency::MXN; | ||
Currency::MYR; | ||
Currency::NOK; | ||
Currency::NZD; | ||
Currency::PHP; | ||
Currency::PLN; | ||
Currency::RON; | ||
Currency::SEK; | ||
Currency::SGD; | ||
Currency::THB; | ||
Currency::TRY; | ||
Currency::ZAR; | ||
``` | ||
|
||
## Testing | ||
|
||
Unit tests are located in the `tests` directory. Run tests with: | ||
|
||
``` | ||
composer test | ||
``` | ||
|
||
## Contributing | ||
|
||
Pull requests welcome! | ||
|
||
## License | ||
|
||
MIT License | ||
|
||
## Powered by: | ||
- [API Frankfurter.app](https://www.frankfurter.app/docs/) | ||
|
||
Let me know if you would like any sections expanded or have any other feedback! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"name": "brunoinds/frankfurter-laravel", | ||
"minimum-stability": "stable", | ||
"prefer-stable": true, | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Bruno Freire", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"autoload": { | ||
"psr-4": { | ||
"Brunoinds\\FrankfurterLaravel\\": "src/" | ||
} | ||
}, | ||
"require": {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
namespace Brunoinds\FrankfurterLaravel\Converter; | ||
|
||
use Illuminate\Support\Facades\Cache; | ||
use DateTime; | ||
use Carbon\Carbon; | ||
|
||
|
||
class Converter{ | ||
public static function convertFromTo(DateTime $date, float $amount, string $from, string $to){ | ||
return Converter::fetchConvertion($date, $amount, $from, $to); | ||
} | ||
private static function fetchConvertion(DateTime $date, float $amount, string $from, string $to){ | ||
if ($date->format('Y-m-d') > Carbon::now()->timezone('America/Lima')->format('Y-m-d')){ | ||
$date = Carbon::now()->timezone('America/Lima')->toDateTime(); | ||
} | ||
|
||
$dateString = $date->format('Y-m-d'); | ||
|
||
$curl = curl_init(); | ||
|
||
$curlURL = 'https://api.frankfurter.app/' . $dateString . '?from=' . $from . '&to=' . $to . '&amount=' . $amount; | ||
|
||
$stores = []; | ||
$cachedValue = Cache::store('file')->get('Brunoinds/FrankfurterLaravelStore'); | ||
if ($cachedValue){ | ||
$stores = json_decode($cachedValue, true); | ||
if (isset($stores[$curlURL])){ | ||
return $stores[$curlURL]; | ||
} | ||
} | ||
|
||
|
||
curl_setopt_array($curl, [ | ||
CURLOPT_URL => $curlURL, | ||
CURLOPT_RETURNTRANSFER => true, | ||
CURLOPT_SSL_VERIFYPEER => 0, | ||
CURLOPT_ENCODING => '', | ||
CURLOPT_MAXREDIRS => 2, | ||
CURLOPT_TIMEOUT => 0, | ||
CURLOPT_FOLLOWLOCATION => true, | ||
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, | ||
CURLOPT_CUSTOMREQUEST => 'GET', | ||
]); | ||
|
||
$response = curl_exec($curl); | ||
curl_close($curl); | ||
$results = json_decode($response, true); | ||
|
||
if (json_last_error() !== JSON_ERROR_NONE) { | ||
throw new \Exception('Invalid JSON response: "' . json_last_error_msg(). '". The API response was: ' . $response); | ||
} | ||
|
||
try { | ||
$rate = $results['rates'][$to]; | ||
|
||
$stores[$curlURL] = $rate; | ||
Cache::store('file')->put('Brunoinds/FrankfurterLaravelStore', json_encode($stores)); | ||
|
||
return $rate; | ||
} catch (\Throwable $th) { | ||
throw new \Exception('Invalid JSON response: "' . json_last_error_msg(). '". The API response was: ' . $response); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
namespace Brunoinds\FrankfurterLaravel\Enums; | ||
|
||
enum Currency:string { | ||
case USD = 'USD'; | ||
case BRL = 'BRL'; | ||
case EUR = 'EUR'; | ||
case AUD = 'AUD'; | ||
case BGN = 'BGN'; | ||
case CAD = 'CAD'; | ||
case CHF = 'CHF'; | ||
case CNY = 'CNY'; | ||
case CZK = 'CZK'; | ||
case DKK = 'DKK'; | ||
case GBP = 'GBP'; | ||
case HKD = 'HKD'; | ||
case HUF = 'HUF'; | ||
case IDR = 'IDR'; | ||
case ILS = 'ILS'; | ||
case INR = 'INR'; | ||
case ISK = 'ISK'; | ||
case JPY = 'JPY'; | ||
case KRW = 'KRW'; | ||
case MXN = 'MXN'; | ||
case MYR = 'MYR'; | ||
case NOK = 'NOK'; | ||
case NZD = 'NZD'; | ||
case PHP = 'PHP'; | ||
case PLN = 'PLN'; | ||
case RON = 'RON'; | ||
case SEK = 'SEK'; | ||
case SGD = 'SGD'; | ||
case THB = 'THB'; | ||
case TRY = 'TRY'; | ||
case ZAR = 'ZAR'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace Brunoinds\FrankfurterLaravel; | ||
|
||
|
||
use DateTime; | ||
use Brunoinds\FrankfurterLaravel\ExchangeDate\ExchangeDate; | ||
|
||
class Exchange{ | ||
public static function on(DateTime $date): ExchangeDate | ||
{ | ||
return new ExchangeDate($date); | ||
} | ||
public static function now():ExchangeDate{ | ||
return new ExchangeDate(new DateTime()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace Brunoinds\FrankfurterLaravel\ExchangeDate; | ||
|
||
use Brunoinds\FrankfurterLaravel\Converter\Converter; | ||
use Brunoinds\FrankfurterLaravel\Enums\Currency; | ||
use Brunoinds\FrankfurterLaravel\ExchangeTransaction\ExchangeTransaction; | ||
use DateTime; | ||
|
||
class ExchangeDate{ | ||
public DateTime $date; | ||
|
||
public function __construct(DateTime $date){ | ||
$this->date = $date; | ||
} | ||
|
||
public function convert(Currency $currency, float $amount): ExchangeTransaction{ | ||
return new ExchangeTransaction($this, $currency, $amount); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
namespace Brunoinds\FrankfurterLaravel; | ||
|
||
//Require composer autoload | ||
require __DIR__ . '/../vendor/autoload.php'; | ||
|
||
use Brunoinds\FrankfurterLaravel\Enums\Currency; | ||
use Brunoinds\FrankfurterLaravel\Exchange; | ||
use DateTime; | ||
|
||
$result = Exchange::on(DateTime::createFromFormat('Y-m-d', '2023-12-10'))->convert(Currency::USD, 1)->to(Currency::BRL); | ||
var_dump($result); | ||
|
||
|
||
$date = DateTime::createFromFormat('Y-m-d', '2023-12-10'); | ||
|
||
$result = Exchange::on($date) | ||
->convert(Currency::USD, 1) | ||
->to(Currency::BRL); | ||
|
||
echo $result; // 0.27 | ||
|
||
|
||
Exchange::now()->convert(Currency::USD, 1)->to(Currency::BRL); // 0.32 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace Brunoinds\FrankfurterLaravel\ExchangeTransaction; | ||
|
||
use Brunoinds\FrankfurterLaravel\Converter\Converter; | ||
use Brunoinds\FrankfurterLaravel\Enums\Currency; | ||
use Brunoinds\FrankfurterLaravel\ExchangeDate\ExchangeDate; | ||
|
||
class ExchangeTransaction | ||
{ | ||
private ExchangeDate $exchangeDate; | ||
private Currency $currency; | ||
private float $amount; | ||
|
||
public function __construct(ExchangeDate $exchangeDate, Currency $currency, float $amount) | ||
{ | ||
$this->exchangeDate = $exchangeDate; | ||
$this->currency = $currency; | ||
$this->amount = $amount; | ||
} | ||
|
||
public function to(Currency $currency): float | ||
{ | ||
if ($this->currency === $currency) { | ||
return $this->amount; | ||
} | ||
|
||
return Converter::convertFromTo($this->exchangeDate->date, $this->amount, $this->currency->value, $currency->value); | ||
|
||
throw new \Exception('Invalid currency'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
// autoload.php @generated by Composer | ||
|
||
if (PHP_VERSION_ID < 50600) { | ||
if (!headers_sent()) { | ||
header('HTTP/1.1 500 Internal Server Error'); | ||
} | ||
$err = 'Composer 2.3.0 dropped support for autoloading on PHP <5.6 and you are running '.PHP_VERSION.', please upgrade PHP or use Composer 2.2 LTS via "composer self-update --2.2". Aborting.'.PHP_EOL; | ||
if (!ini_get('display_errors')) { | ||
if (PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg') { | ||
fwrite(STDERR, $err); | ||
} elseif (!headers_sent()) { | ||
echo $err; | ||
} | ||
} | ||
trigger_error( | ||
$err, | ||
E_USER_ERROR | ||
); | ||
} | ||
|
||
require_once __DIR__ . '/composer/autoload_real.php'; | ||
|
||
return ComposerAutoloaderInit83dc51f18499c5b442343696b7634cfe::getLoader(); |
Oops, something went wrong.