Giter Site home page Giter Site logo

currency's Introduction

Currency Package

Build Status

The Moltin currency composer package makes it easy to implement multi-currency pricing into your application and store the exchange data using one of the numerous data stores provided. You can also inject your own data store if you would like your data to be stored elsewhere.

Installation

Download and install composer from http://www.getcomposer.org/download

Add the following to your project composer.json file

{
    "require": {
        "moltin/currency": "dev-master"
    }
}

When you're done just run php composer.phar install and the package is ready to be used.

Usage

Below is a basic usage guide for this package.

Instantiating currency

Before you begin, you will need to know which storage, currencies and exchange method you are going to use. The exchange method defines where your exchange rates are retrieved from. The currencies method is used to retrieve your supported currencies for the current application.

In this example we're going to use the currencies file, exchange file and session for storage.

use Moltin\Currency\Currency as Currency;
use Moltin\Currency\Format\Runtime as RuntimeFormat;
use Moltin\Currency\Exchange\OpenExchangeRates as OpenExchange;

$currency = new Currency(new OpenExchange($app_id), new RuntimeFormat);

Setting the value

Now that you have Currency instantiated, you will now need to tell it what value you would like to convert. You can do this using the following method.

$currency->convert(9.33)->from('GBP');

Getting the value

The most basic action you can perform is rertieve the original value back from the method.

// Returns 9.33
$value = $currency->value();

Formatting as a currency

By default the currency is set to GBP so calling currency will format the value to a string with £ and correct decimal and thousand seperators.

// Returns £9.33
$value = $currency->format();

Rounding to common values

There are a number of common pricing formats built in to make "nice" prices easy to implement. These formats changes the default value and return the object to allow for chaining.

// Sets value to 10.00
$currency->zeros();

// Sets value to 9.99
$currency->nines();

// Sets value to 9.50
$currency->fifty();

// Returns £9.50
$value = $currency->fifty()->format();

Currency Exchange

The package makes it as easy as possible to quickly switch between currencies. Before each exchange the value is reset to default to ensure the correct price is assigned.

// Returns ~$14.47
$value = $currency->convert(9.33)->from('GBP')->to('USD')->format();

// Returns ~14.50
$value = $currency->convert(9.33)->from('GBP')->to('USD')->fifty()->value();

Resetting the value

After using exchange or any of the rounding functions to retrieve the default value you must call reset.

// Returns 10.00
$value = $currency->zeros()->value();

// Returns 9.33
$value = $currency->reset()->value();

currency's People

Contributors

axhini avatar chrisnharvey avatar josephsilber avatar

Stargazers

 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.