2017 © Pedro Peláez
 

library currency-converter

A php library to handle currency conversion

image

sprain/currency-converter

A php library to handle currency conversion

  • Monday, April 20, 2015
  • by sprain
  • Repository
  • 1 Watchers
  • 1 Stars
  • 24 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 5 Versions
  • 0 % Grown

The README.md

SprainCurrencyConverter

SprainCurrencyConverter is a php library to convert currencies. It allows great flexibility by use of built-in or custom providers., (*1)

Build Status Scrutinizer Code Quality, (*2)

Installation

Add SprainCurrencyConverter to your composer.json:, (*3)

{
    "require": {
       "sprain/currency-converter": "~0.1"
    }
}

Now tell composer to download the bundle by running the command:, (*4)

$ php composer.phar update sprain/currency-converter

Usage

<?php

include 'vendor/autoload.php';

// Create an instance
$converter = new \Sprain\CurrencyConverter\CurrencyConverter();

// Optional, but recommended: add a cache which will be used by some of the providers.
// You can use any cache system supported by Doctrine Cache.
$cache = new \Doctrine\Common\Cache\FilesystemCache(sys_get_temp_dir());
$converter->setCache($cache);

// Add at least one provider with its dependencies
$browser = new Buzz\Browser();
$converter->addProvider(new \Sprain\CurrencyConverter\Provider\FixerIoProvider($browser));

// Optional: Add more providers. The converter will then loop over the providers until
// a result is found. Use the second parameter to define the provider's priority.
// Higher values mean higher priority. The highest priority provider will be checked before all others.
$converter->addProvider(new \Sprain\CurrencyConverter\Provider\SwissGovernmentProvider($browser), 255);

// Do a quick conversion
$convertedAmount = $converter->convert(100)->from('USD')->to('EUR')->quick();

// Or alternatively, get a conversion object which contains more data
$conversion = $converter->convert(100)->from('USD')->to('EUR')->getConversion();
$convertedAmount    = $conversion->getAmountInTargetCurrency();
$exchangeRate       = $conversion->getExchangeRate();
$successfulProvider = $conversion->getProvider();

Providers

Included providers

  • FixerIoProvider
    Exchange rates provided by fixer.io, which gets its data from the European Central Bank.
    Supported currencies: No restrictions defined, (*5)

  • SwissGovernmentProvider
    Exchange rates provided by the Swiss government. See www.estv.admin.ch.
    Supported currencies: CHF, (*6)

  • YahooProvider
    Exchange rates provided by finance.yahoo.com.
    Supported currencies: No restrictions defined, (*7)

Create your own custom provider

SprainCurrencyConverter easily allows you to add your own custom provider. This is helpful if for instance you keep exchange rates locally in a database., (*8)

It's simple:, (*9)

<?php

namespace Acme\Your\Project;

use Sprain\CurrencyConverter\Provider\Abstracts\AbstractProvider;

// Simply extend the AbstractProvider which comes with SprainCurrencyConverter
class MyCustomProvider extends AbstractProvider
{
    public function doGetExchangeRate()
    {
        $baseCurrency   = $this->getBaseCurrency();
        $targetCurrency = $this->getTargetCurrency();

        // do your thing and return the exchange rate which represents the
        // value of 1 unit of the base currency in units of the target currency.
        // ...

        return $exchangeRate;
    }

    // Return your provider's name.
    public function getName()
    {
        return 'My custom provider';
    }

    // Optional:
    // Add this method and return an array of supported currencies.
    // The provider will only be used if either base currency _or_ target currency
    // is a member of this array.
    public function getSupportedCurrencies()
    {
        return array('USD', 'EUR');
    }
}

Caching in custom providers

If your provider uses a remote api or any other method which takes some time to get data, you might want to cache the exchange rate., (*10)

To achieve this, simply extend AbstractCacheableProvider instead of AbstractProvider:, (*11)

<?php

namespace Acme\Your\Project;

use Sprain\CurrencyConverter\Provider\Abstracts\AbstractCacheableProvider;

class MyCacheableCustomProvider extends AbstractCacheableProvider
{
    ... same, same as above
}

If a cache was injected into CurrencyConverter, every exchange rate fetched by your provider will now automatically be cached for 24 hours., (*12)

License

This library is under the MIT license. See the complete license in:, (*13)

Resources/meta/LICENSE

The Versions

20/04 2015

dev-master

9999999-dev https://github.com/sprain/CurrencyConverter

A php library to handle currency conversion

  Sources   Download

MIT

The Requires

 

The Development Requires

currency money conversion

20/04 2015

0.2

0.2.0.0 https://github.com/sprain/CurrencyConverter

A php library to handle currency conversion

  Sources   Download

MIT

The Requires

 

The Development Requires

currency money conversion

16/03 2015

0.1.2

0.1.2.0 https://github.com/sprain/CurrencyConverter

A php library to handle currency conversion

  Sources   Download

MIT

The Requires

 

The Development Requires

currency money conversion

15/03 2015

0.1.1

0.1.1.0 https://github.com/sprain/CurrencyConverter

A php library to handle currency conversion

  Sources   Download

MIT

The Requires

 

The Development Requires

currency money conversion

15/03 2015

0.1

0.1.0.0 https://github.com/sprain/CurrencyConverter

A php library to handle currency conversion

  Sources   Download

MIT

The Requires

 

The Development Requires

currency money conversion