Giter Site home page Giter Site logo

laravel-currencylayer's Introduction

๐Ÿ’ฑ Laravel package for currencylayer.com

Latest Stable Version Latest Unstable Version Total Downloads License

Build Status Test Coverage Maintainability Quality Score StyleCI

Simple Laravel package for integrating with currencylayer.com currency rates.

Requirements

  • PHP 7.1 or higher
  • Laravel 5.8 or higher

Installation

You can install the package via composer:

composer require orkhanahmadov/laravel-currencylayer

Publish package migration and config files:

php artisan vendor:publish --provider="Orkhanahmadov\LaravelCurrencylayer\LaravelCurrencylayerServiceProvider"

Set your currencylayer.com access key in .env file:

CURRENCYLAYER_ACCESS_KEY=your-key-here

You can find your access key in Currencylayer Dashboard.

Configuration

After publishing configuration file it will be available in config directory as currencylayer.php

It has following settings:

  • access_key - currencylayer.com access key, by default uses value from .env file
  • https_connection - if set to true all calls to currencylayer API endpoint will be over HTTPS, instead of default HTTP

Usage

You can type-hint Orkhanahmadov\LaravelCurrencylayer\Contracts\CurrencyService to inject it from container:

use Orkhanahmadov\LaravelCurrencylayer\Contracts\CurrencyService;

class CurrencyController
{
    public function index(CurrencyService $currencyService)
    {
        $currencyService->live('USD', 'EUR');
    }
}

Anywhere outside container you can create instance of the service with app() helper:

$currencyService = app('currencylayer');
$currencyService->live('USD', 'EUR');

You can also use provided facade:

\Currencylayer::live('USD', 'EUR');

Available methods

All methods save rates to database table when fetched.

live()

Method fetches live rates from currencylayer.com

$currencyService->live('USD', 'EUR');

First argument is source currency, second argument is converted currency. You can also pass instance of Orkhanahmadov\LaravelCurrencylayer\Models\Currency as a source currency.

use Orkhanahmadov\LaravelCurrencylayer\Models\Currency;

$usd = Currency::where('code', 'USD')->first();
$currencyService->live($usd, 'EUR');

To get live rates for multiple currencies, pass multiple currency codes:

$currencyService->live('USD', 'EUR', 'CHF', 'BTC', 'RUB');
// or
$currencyService->live('USD', ['EUR', 'CHF', 'BTC', 'RUB']);

rate()

Method fetches rates for given date from currencylayer.com

$currencyService->rate('USD', '2019-01-25', 'EUR');

First argument is source currency, second argument is date, third argument is converted currency. You can also pass instance of Orkhanahmadov\LaravelCurrencylayer\Models\Currency as a source currency argument and instance of Carbon\Carbon as a date argument.

use Carbon\Carbon;
use Orkhanahmadov\LaravelCurrencylayer\Models\Currency;

$usd = Currency::where('code', 'USD')->first();
$today = Carbon::today();
$currencyService->rate($usd, $today, 'EUR');

To get rates for multiple currencies, pass multiple currency codes:

$currencyService->rate('USD', '2019-01-25', 'EUR', 'CHF', 'BTC', 'RUB');
// or
$currencyService->rate('USD', '2019-01-25', ['EUR', 'CHF', 'BTC', 'RUB']);

Commands

Package comes with 2 commands:

php artisan currencylayer:live - fetches live rates and outputs the values. First argument is source currency, second and next arguments are target currencies:

php artisan currencylayer:live USD EUR CHF

This will fetch live rates for USD to EUR and USD to CHF.

php artisan currencylayer:rate - fetches rates for given date and outputs the values. First argument is source currency, second argument is date, third and next arguments are target currencies:

php artisan currencylayer:rate USD 2019-01-25 EUR CHF

This will fetch rates for 2019-01-25 for USD to EUR and USD to CHF.

Models

Package comes with 2 database models:

  • Orkhanahmadov\LaravelCurrencylayer\Models\Currency - stores each fetched currency codes
  • Orkhanahmadov\LaravelCurrencylayer\Models\Rate - stores rate for each currency exchange based on timestamp

Currency model has rateFor() method you can use to get currency rate. First argument is target currency code or can be instance of Currency.

use Orkhanahmadov\LaravelCurrencylayer\Models\Currency;

$usd = Currency::where('code', 'USD')->first();
$rate = $usd->rateFor('EUR');
// or
$eur = Currency::where('code', 'EUR')->first();
$rate = $usd->rateFor($eur);

This will fetch latest USD to EUR rate.

You can also pass date or Carbon\Carbon instance as a second argument to get rates for that date:

use Carbon\Carbon;
use Orkhanahmadov\LaravelCurrencylayer\Models\Currency;

$usd = Currency::where('code', 'USD')->first();
$rate = $usd->rateFor('EUR', '2019-01-25');
// or
$today = Carbon::today();
$rate = $usd->rateFor('EUR', $today);

Testing

composer test

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

License

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

laravel-currencylayer's People

Contributors

orkhanahmadov avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.