Git Product home page Git Product logo

currencyphp's Introduction

CurrencyPHP

Build Status Latest Stable Version Total Downloads Monthly Downloads Daily Downloads License

Installation

composer install tomwright/currency-php

Usage

CurrencyPHP is just a basic wrapper. It cannot do conversions out of the box... Saying that... you only need to provide it with the conversion rates.

$rateFetcher = new MyConversionRateFetcher();
$factory = new CurrencyFactory($rateFetcher);

$gbp = $factory->create('GBP');
$usd = $factory->create('USD');

$priceInGBP = 100;
$priceInUSD = $gbp->convertTo($usd, $priceInGBP);
echo $priceInUSD; // 126

Rate Fetchers

Rate Fetchers are what CurrencyPHP uses to get conversion rates. Any Rate Fetcher you create should implement ConversionRateFetcherInterface.

Existing Rate Fetchers

If you have created your own Rate Fetcher and want it included here, please submit a pull request.

Creating Your Own

The following Rate Fetcher gives you some fixed exchange rates:

  • GBP to USD
  • USD to GBP
  • GBP to CAD
  • CAD to USD
class FixedRateFetcher implements ConversionRateFetcherInterface
{

    /**
     * @param Currency $from
     * @param Currency $to
     * @return float
     */
    public function getConversionRate(Currency $from, Currency $to)
    {
        $rates = [
            [
                'from' => 'GBP',
                'to' => 'USD',
                'rate' => 1.2547,
            ],
            [
                'from' => 'USD',
                'to' => 'GBP',
                'rate' => 0.7974,
            ],
            [
                'from' => 'GBP',
                'to' => 'CAD',
                'rate' => 1.6612,
            ],
            [
                'from' => 'CAD',
                'to' => 'USD',
                'rate' => 0.7539,
            ],
        ];

        $result = null;

        foreach ($rates as $rate) {
            if ($rate['from'] === $from->getCurrencyCode() && $rate['to'] === $to->getCurrencyCode()) {
                $result = $rate['rate'];
            }
        }
        return $result;
    }
}

Handling unknown conversion rates

One way conversion rates

The above Rate Fetcher has rates for both GBP to USD, and USD to GBP and this works great... but you'll also notice that it has CAD to USD, but no USD to CAD conversion rates. There is some logic implemented so that you only need to store 1 way conversion rates and it will automatically invert the rate if required.

Thanks to this logic, you can run a USD to CAD conversion using the above Rate Fetcher with no problems. The full list of conversion that the above can handle is as follows:

  • GBP to USD
  • USD to GBP
  • GBP to CAD
  • CAD to GBP
  • CAD to USD
  • USD to CAD

Missing conversion rates

If no conversion rate exists at all between the 2 currencies, an UnhandledConversionRate Exception will be thrown.

currencyphp's People

Contributors

tomwright avatar tomwright-awin avatar

Watchers

 avatar  avatar  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.