All Projects → ash-jc-allen → Laravel Exchange Rates

ash-jc-allen / Laravel Exchange Rates

Licence: mit
A Laravel wrapper package for interacting with the exchangeratesapi.io API.

Projects that are alternatives of or similar to Laravel Exchange Rates

Pipedrive
Complete Pipedrive API client for PHP
Stars: ✭ 138 (-23.33%)
Mutual labels:  hacktoberfest, laravel
Laravelresources
Speed Up package development for Laravel Apps with API's
Stars: ✭ 152 (-15.56%)
Mutual labels:  hacktoberfest, laravel
Laravel Ide Helper
Laravel IDE Helper
Stars: ✭ 11,893 (+6507.22%)
Mutual labels:  hacktoberfest, laravel
Simple Qrcode
An easy-to-use PHP QrCode generator with first-party support for Laravel.
Stars: ✭ 1,923 (+968.33%)
Mutual labels:  hacktoberfest, laravel
Novapackages
Stars: ✭ 169 (-6.11%)
Mutual labels:  hacktoberfest, laravel
Jigsaw
Simple static sites with Laravel’s Blade.
Stars: ✭ 1,823 (+912.78%)
Mutual labels:  hacktoberfest, laravel
Platform
A @laravel based RAD platform for back-office applications, admin/user panels, and dashboards.
Stars: ✭ 2,623 (+1357.22%)
Mutual labels:  hacktoberfest, laravel
Voyager
Voyager - The Missing Laravel Admin
Stars: ✭ 10,801 (+5900.56%)
Mutual labels:  hacktoberfest, laravel
Laravel 8 Stisla Jetstream
Laravel 8 + Jetstream + Livewire + Stisla
Stars: ✭ 159 (-11.67%)
Mutual labels:  hacktoberfest, laravel
Bdgt
Big finance tools in a small package
Stars: ✭ 159 (-11.67%)
Mutual labels:  hacktoberfest, laravel
Daybydaycrm
DaybydayCRM an open-source CRM, to help you keep track of your daily workflow.
Stars: ✭ 1,856 (+931.11%)
Mutual labels:  hacktoberfest, laravel
Laravel Messenger
Simple user messaging package for Laravel
Stars: ✭ 2,140 (+1088.89%)
Mutual labels:  hacktoberfest, laravel
Health
Laravel Health Panel
Stars: ✭ 1,774 (+885.56%)
Mutual labels:  hacktoberfest, laravel
Ignition
A beautiful error page for Laravel apps
Stars: ✭ 1,885 (+947.22%)
Mutual labels:  hacktoberfest, laravel
Oc Mall Plugin
🏪 E-commerce solution for October CMS
Stars: ✭ 128 (-28.89%)
Mutual labels:  hacktoberfest, laravel
Has Parameters
A trait that allows you to pass arguments to Laravel middleware in a more PHP'ish way.
Stars: ✭ 149 (-17.22%)
Mutual labels:  hacktoberfest, laravel
Laravel Mail Editor
MailEclipse ⚡ Laravel Mailable Editor!
Stars: ✭ 1,714 (+852.22%)
Mutual labels:  hacktoberfest, laravel
Onramp
Easing the onramp for new or non-PHP developers to become Laravel devs.
Stars: ✭ 123 (-31.67%)
Mutual labels:  hacktoberfest, laravel
Symposium
Management of proposals, bios, photos, etc. for conference speakers.
Stars: ✭ 157 (-12.78%)
Mutual labels:  hacktoberfest, laravel
Laravel Debugbar
Laravel Debugbar (Integrates PHP Debug Bar)
Stars: ✭ 13,485 (+7391.67%)
Mutual labels:  hacktoberfest, laravel

Laravel Exchange Rates

Latest Version on Packagist Build Status Total Downloads PHP from Packagist GitHub license

Table of Contents

Overview

A simple Laravel package used for interacting with the exchangeratesapi.io API. 'Laravel Exchange Rates' allow you to get the latest or historical exchange rates and convert monetary values between different currencies.

Installation

You can install the package via Composer:

composer require ashallendesign/laravel-exchange-rates

The package has been developed and tested to work with the following minimum requirements:

  • PHP 7.2
  • Laravel 6

Usage

Methods

Available Currencies

$exchangeRates = new ExchangeRate();
$exchangeRates->currencies();

Exchange Rate

Getting the Rate Between Two Currencies

To get the exchange for one currency to another, you can use the ->exchangeRate() method. When doing this, you can pass the currency code as a string as the second parameter. The ->exchangeRates() method will then return a string containing the exchange rate.

The example below shows how to get the exchange rate from 'GBP' to 'EUR' for today.

$exchangeRates = new ExchangeRate();
$result = $exchangeRates->exchangeRate('GBP', 'EUR');

// $result: '1.10086'

Note: If a Carbon date is passed as the third parameter, the exchange rate for that day will be returned (if valid). If no date is passed, today's exchange rate will be used.

Getting the Rate Between More Than Two Currencies

It is possible to get the exchange rates for multiple currencies in one call. This can be particularly useful if you are needing to get many exchange rates at once and do not want to multiple API calls.

To do this, you can use ->exchangeRate() method and pass an array of currency code strings as the second parameter. This will return the an array containing the exchange rates as strings.

The example below shows how to get the exchange rates from 'GBP' to 'EUR' and 'USD' for today.

$exchangeRates = new ExchangeRate();
$result = $exchangeRates->exchangeRate('GBP', ['EUR', 'USD']);

// $result: [
//     'EUR' => '1.10086',
//     'USD' => '1.25622'
// ];

Exchange Rates Between Date Range

Getting the Rates Between Two Currencies

To get the exchange rates between two currencies between a given date range, you can use the ->exchangeRateBetweenDateRange() method. When doing this, you can pass the currency code as a string as the second parameter. The method will then return an array containing the exchange rates.

The example below shows how to get the exchange rates from 'GBP' to 'EUR' for the past 3 days.

$exchangeRates = new ExchangeRate();
$result = $exchangeRates->exchangeRateBetweenDateRange('GBP', 'EUR', Carbon::now()->subWeek(), Carbon::now());

// $result: [
//     '2020-07-07' => 1.1092623405
//     '2020-07-08' => 1.1120625424
//     '2020-07-09' => 1.1153867604
// ];
Getting the Rates Between More Than Two Currencies

To get the exchange rates for multiple currencies in one call, you can pass an array of currency codes strings as the second parameter to the ->exchangeRateBetweenDateRange() method.

The example below shows how to get the exchange rates from 'GBP' to 'EUR' and 'USD' for the past 3 days.

$exchangeRates = new ExchangeRate();
$result = $exchangeRates->exchangeRateBetweenDateRange('GBP', ['EUR', 'USD'], Carbon::now()->subDays(3), Carbon::now());

// $result: [
//     '2020-07-07' => [
//         'EUR' => 1.1092623405,
//         'USD' => 1.2523571825,
//      ],
//     '2020-07-08' => [
//         'EUR' => 1.1120625424,
//         'USD' => 1.2550737853,
//      ],
//     '2020-07-09' => [
//         'EUR' => 1.1153867604,
//         'USD' => 1.2650716636,
//      ],
// ];

Convert Currencies

When passing in the monetary value (first parameter) that is to be converted, it's important that you pass it in the lowest denomination of that currency. For example, £1 GBP would be passed in as 100 (as £1 = 100 pence).

Converting Between Two Currencies

Similar to how you can get the exchange rate from one currency to another, you can also convert a monetary value from one currency to another. To do this you can use the ->convert() method.

The example below shows how to convert £1 'GBP' to 'EUR' at today's exchange rate.

$exchangeRates = new ExchangeRate();
$result = $exchangeRates->convert(100, 'GBP', 'EUR', Carbon::now());

// $result: 110.15884906

Note: If a Carbon date is passed as the third parameter, the exchange rate for that day will be returned (if valid). If no date is passed, today's exchange rate will be used.

Converting Between More Than Two Currencies

You can also use the ->convert() method to convert a monetary value from one currency to multiple currencies. To do this, you can pass an array of currency codes strings as the third parameter.

The example below show how to convert £1 'GBP' to 'EUR' and 'USD' at today's exchange rate.

$exchangeRates = new ExchangeRate();
$result = $exchangeRates->convert(100, 'GBP', ['EUR', 'USD'], Carbon::now());

// $result: [
//     'EUR' => 110.15884906,
//     'USD' => 125.30569081
// ];

Convert Currencies Between Date Range

When passing in the monetary value (first parameter) that is to be converted, it's important that you pass it in the lowest denomination of that currency. For example, £1 GBP would be passed in as 100 (as £1 = 100 pence).

Converting Between Two Currencies in a Date Range

Similar to getting the exchange rates between a date range, you can also get convert monetary values from one currency to another using the exchange rates. To do this you can use the ->convertBetweenDateRange() method.

The example below shows how to convert £1 'GBP' to 'EUR' using the exchange rates for the past 3 days.

$exchangeRates = new ExchangeRate();
$exchangeRates->convertBetweenDateRange(100, 'GBP', 'EUR', Carbon::now()->subDays(3), Carbon::now());

// $result: [
//     '2020-07-07' => 110.92623405,
//     '2020-07-08' => 111.20625424,
//     '2020-07-09' => 111.53867604,
// ];
Converting Between More Than Two Currencies in a Date Range

You can also use the ->convertBetweenDateRange() method to convert a monetary value from one currency to multiple currencies using the exchange rates between a date range. To do this, you can pass an array of currency codes strings as the third parameter.

The example below show how to convert £1 'GBP' to 'EUR' and 'USD' at the past three days' exchange rates.

$exchangeRates = new ExchangeRate();
$result = $exchangeRates->exchangeRateBetweenDateRange('GBP', ['EUR', 'USD'], Carbon::now()->subDays(3), Carbon::now());

// $result: [
//     '2020-07-07' => [
//         'EUR' => 110.92623405,
//         'USD' => 125.23571825,
//      ],
//     '2020-07-08' => [
//         'EUR' => 111.20625424,
//         'USD' => 125.50737853,
//      ],
//     '2020-07-09' => [
//         'EUR' => 111.53867604,
//         'USD' => 126.50716636,
//      ],
// ];

Facade

If you prefer to use facades in Laravel, you can choose to use the provided ExchangeRate facade instead of instantiating the AshAllenDesign\LaravelExchangeRates\Classes\ExchangeRate class manually.

The example below shows an example of how you could use the facade to get the available currencies:

<?php
    
    namespace App\Http\Controllers;
    
    use ExchangeRate;
    
    class TestController extends Controller
    {
        public function index()
        {
            return ExchangeRate::currencies();
        }
    }

Validation Rule

Laravel Exchange Rates comes with its own ValidCurrency rule for validating currencies. This can be useful for if you need to be sure that a currency (maybe one provided by the user) is supported by the library. The example below show how you can use the rule for validating the currency.

<?php
    
    namespace App\Http\Controllers;
    
    use AshAllenDesign\LaravelExchangeRates\Rules\ValidCurrency;
    use Illuminate\Support\Facades\Validator;

    class TestController extends Controller
    {
        public function index()
        {
            $formData = [
                'currency' => 'GBP',
            ];
    
            $rules = [
                'currency' => new ValidCurrency,
            ];
    
            $validator = Validator::make($formData, $rules);
        }
    }

Examples

This example shows how to convert 100 pence (£1) from Great British Pounds to Euros. The current exchange rate will be used (unless a cached rate for this date already exists).

<?php
    
    namespace App\Http\Controllers;
    
    use AshAllenDesign\LaravelExchangeRates\Classes\ExchangeRate;
    
    class TestController extends Controller
    {
        public function index()
        {
            $exchangeRates = new ExchangeRate();
            return $exchangeRates->convert(100, 'GBP', 'EUR', Carbon::now());
        }
    }

Caching

Busting Cached Exchange Rates

By default, the responses all of the requests to the exchangeratesapi.io API are cached. This allows for significant performance improvements and reduced bandwidth from your server.

However, if for any reason you require a fresh result from the API and not a cached result, the ->shouldBustCache() method can be used. The example below shows how to ignore the cached value (if one exists) and make a new API request.

<?php
    
    namespace App\Http\Controllers;
    
    use AshAllenDesign\LaravelExchangeRates\Classes\ExchangeRate;
    
    class TestController extends Controller
    {
        public function index()
        {
            $exchangeRates = new ExchangeRate();
            return $exchangeRates->shouldBustCache()->convert(100, 'GBP', 'EUR', Carbon::now());
        }
    }

Preventing Exchange Rates from Being Cached

It is also possible to prevent the exchange rates from being cached at all. To do this, you can use the ->shouldCache(false) method. The example below shows how to get an exchange rate and not cache it:

<?php
    
    namespace App\Http\Controllers;
    
    use AshAllenDesign\LaravelExchangeRates\Classes\ExchangeRate;
    
    class TestController extends Controller
    {
        public function index()
        {
            $exchangeRates = new ExchangeRate();
            return $exchangeRates->shouldCache(false)->convert(100, 'GBP', 'EUR', Carbon::now());
        }
    }

Note: The caching works by storing exchange rates after fetching them from the API. As an example, if you were to fetch the exchange rates for 'GBP' to 'EUR' for 20-11-2019 - 27-11-2019, the rates between these dates will be cached as a single cache item. This cache item will only be retrieved if you attempt to fetch the same exchange rates on with the exact same currencies and date range.

Therefore, if you were to try and get 'GBP' to 'EUR' for 20-11-2019 - 26-11-2019, a new API request would be made because the date range is different.

Supported Currencies

Laravel Exchange Rates supports working with the following currencies (sorted in A-Z order):

Code Currency Name
AUD Australian dollar
BGN Bulgarian lev
BRL Brazilian real
CAD Canadian
CHF Swiss franc
CNY Chinese yuan renminbi
CZK Czech koruna
DKK Danish krone
EUR Euro
GBP Pound sterling
HKD Hong Kong dollar
HRK Croatian kuna
HUF Hungarian forint
IDR Indonesian rupiah
ILS Israeli shekel
INR Indian rupee
ISK Icelandic krone
JPY Japanese yen
KRW South Korean won
MXN Mexican peso
MYR Malaysian ringgit
NOK Norwegian krone
NZD New Zealand dollar
PHP Philippine peso
PLN Polish zloty
RON Romanian leu
RUB Russian rouble
SEK Swedish krona
SGD Singapore dollar
THB Thai baht
TRY Turkish lira
USD US dollar
ZAR South African rand

Note: Please note that the currencies are available because they are exposed in the exchangeratesapi.io API.

Testing

vendor/bin/phpunit

Security

If you find any security related issues, please contact me directly at [email protected] to report it.

Contribution

If you wish to make any changes or improvements to the package, feel free to make a pull request.

To contribute to this library, please use the following guidelines before submitting your pull request:

  • Write tests for any new functions that are added. If you are updating existing code, make sure that the existing tests pass and write more if needed.
  • Follow PSR-2 coding standards.
  • Make all pull requests to the master branch.

Credits

Changelog

Check the CHANGELOG to get more information about the latest changes.

Upgrading

Check the UPGRADE guide to get more information on how to update this library to newer versions.

License

The MIT License (MIT). Please see License File for more information.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].