restuniverse / currency
This package provides Laravel functions such as currency formatting and conversion using up-to-date exchange rates.
Requires
- php: ^7.2|^8.0
- illuminate/cache: ^6.0|^7.0|^8.0|^9.0
- illuminate/console: ^6.0|^7.0|^8.0|^9.0
- illuminate/support: ^6.0|^7.0|^8.0|^9.0
Requires (Dev)
- mockery/mockery: ^1.3
- phpstan/phpstan: ^0.12.14
- phpunit/phpunit: ^8.0
- squizlabs/php_codesniffer: ^3.5
Suggests
- illuminate/database: Allows for storing of currencies in the database
README
Installation
Composer
From the command line run:
$ composer require restuniverse/currency
Laravel's >=5.5 Auto-Discovery
Simply install the package and let Laravel do its magic.
Manual Setup
Once installed you need to register the service provider with the application. Open up config/app.php
and find the providers
key.
'providers' => [ \RestUniverse\Currency\CurrencyServiceProvider::class, ]
This package also comes with a facade, which provides an easy way to call the the class. Open up config/app.php
and find the aliases
key.
'aliases' => [ 'Currency' => \RestUniverse\Currency\Facades\Currency::class, ];
Publish the configurations
Run this on the command line from the root of your project:
php artisan vendor:publish --provider="RestUniverse\Currency\CurrencyServiceProvider" --tag=config
A configuration file will be published to config/currency.php
.
Migration
If currencies are going to be stored in the database.
php artisan vendor:publish --provider="RestUniverse\Currency\CurrencyServiceProvider" --tag=migrations
Run this on the command line from the root of your project to generate the table for storing currencies:
$ php artisan migrate
note: Add your API_KEY to the .env file with (REST_UNIVERSE_API_KEY).
Basic usage
The simplest way to use these methods is through the helper function currency()
or by using the facade. For the examples below we will use the helper method.
currency($amount, $from = null, $to = null, $format = true)
Arguments:
$amount
- The float amount to convert
$from
- The current currency code of the amount. If not set, the application default will be used (see config/currency.php
file).
$to
- The currency code to convert the amount to. If not set, the user-set currency is used.
$format
- Should the returned value be formatted.
Usage:
echo currency(12.00); // Will format the amount using the user selected currency echo currency(12.00, 'USD', 'EUR'); // Will format the amount from the default currency to EUR
Updating Exchange Rates
Update exchange rates from restuniverse.com. An API key is needed to use Rest Universe. Add yours to the config file.
php artisan currency:hydrate
Security
If you discover any security related issues, please email security@restuniverse.com instead of using the issue tracker.