amelia/money

A library for working with the Open Exchange Rates API in PHP

v1.0.0-beta.2 2015-08-28 11:21 UTC

This package is auto-updated.

Last update: 2024-11-08 14:32:35 UTC


README

Build Status Scrutinizer Latest Stable Version MIT License

A PHP 5.4+ library for working with currency APIs and conversion.

Current Providers for data are:

Planned:

Feel free to add more!

Usage

To dive in with the OpenExchangeRates API implementation, go sign up for an app id. It's free!

Laravel 5

Money ships with a Laravel 5 service provider.

Simply add Amelia\Money\MoneyServiceProvider::class to your providers array in config/app.php.

You can type-hint Amelia\Money\FactoryInterface in controllers (or more importantly, form requests), or use app(FactoryInterface::class) to fetch it from the IoC container.

<?php

use Amelia\Money\FactoryInterface;

class FooController extends Controller {
    public function __construct(FactoryInterface $money) {
        $money->convert($amount = 130.01, $from = "USD", $to = "GBP");
        $money->getBase();
        $money->getRates();
        $newMoney = $money->base("GBP"); // switch the base currency
    }
}

Add the following configuration to the config/services.php array

"money" => [
    "api" => env("MONEY_API_TYPE", "openexchangerates"),
    "key" => env("MONEY_API_KEY", null),
],

Usage without a framework:

<?php

use Amelia\Money\OpenExchangeRatesFactory;
$converter = OpenExchangeRatesFactory::create(["key" => "YOUR_APP_ID"]);

$converter->convert(140, "gbp", "nok"); // currency codes are case-insensitive.
var_dump($converter->getRates(), $converter->getBase());

Converter instances are immutable. To change the base currency, use the ->base(string $base) method. To change the rates, make a new object.