Code Monkey home page Code Monkey logo

swap's Introduction

Swap

Build Status SensioLabsInsight Latest Stable Version Latest Unstable Version Total Downloads License

Swap is an exchange rates library for PHP 5.3+. It offers multiple providers and leverage their ability to retrieve multiple quotes at once, while simulating this behavior for those who don't support it by using Guzzle's parallel HTTP requests.

Installation

The recommended way to install Swap is via Composer.

Add this line to your composer.json file:

{
    "require": {
        "florianv/swap": "~1.0"
    }
}

Tell Composer to update the dependency by running:

$ php composer.phar update florianv/swap

Usage

Firstly, you need to create an HTTP client:

$client = new \Guzzle\Http\Client();

Then you can create a provider:

$yahoo = new \Swap\Provider\YahooFinance($client);

Create a Swap instance and add the provider:

$swap = new Swap\Swap();
$swap->addProvider($yahoo);

Your job is to create a currency pair with a base and quote currencies exprimed as their ISO 4217 code, stick it to Swap and it will set its rate.

// Create the currency pair EUR/USD
$pair = new \Swap\Model\CurrencyPair('EUR', 'USD');

// Quotes the pair
$swap->quote($pair);

// 1.3751
echo $pair->getRate();

We created a currency pair EUR/USD, quoted it with the YahooFinance provider and got 1.3751 as rate which means that 1 EUR is exchanged for 1.3751 USD.

Multiple pairs

You can also quote multiple pairs at once:

use Swap\Model\CurrencyPair;

$eurUsd = new CurrencyPair('EUR', 'USD');
$usdGbp = new CurrencyPair('USD', 'GBP');
$gbpJpy = new CurrencyPair('GBP', 'JPY');

$swap->quote(array($eurUsd, $usdGbp, $gbpJpy));

// 1.3751
echo $eurUsd->getRate();

// 0.5938
echo $usdGbp->getRate();

// 171.5772
echo $gbpJpy->getRate();

Date

It is possible to retrieve the date at which the rate was calculated. If the provider does not give this information, it will be set to now.

// $date is a \DateTime instance
$date = $pair->getDate()

Chained providers

Providers can be chained. When an exception is thrown by a provider, the following will be used to quote the pairs that were not quoted.

$yahoo = new \Swap\Provider\YahooFinance($client);
$google = new \Swap\Provider\GoogleFinance($client);

$swap->addProvider($yahoo);
$swap->addProvider($google);

Exception Handling

Swap throws different types of exceptions:

try {
    $swap->quote($pair);
} catch (\Swap\Exception\QuotationException $e) {

    // Default exception when the quote operation failed
    // For example when the HTTP request failed

} catch (\Swap\Exception\UnsupportedBaseCurrencyException $e) {

    // Exception thrown when a currency is not supported as base by the provider

} catch (\Swap\Exception\UnsupportedCurrencyPairException $e) {

    // Exception thrown when the currency pair is not supported
}

Providers

Supports only EUR as base currency.

Supports multiple currencies as base and quote currencies.

Supports only USD as base currency for the free version and multiple ones for the enterprise version.

Supports multiple currencies as base and quote currencies.

Supports multiple currencies as base and quote currencies.

License

MIT

swap's People

Contributors

florianv avatar

Watchers

 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.