lukaszwit/currency

Currency handling with float operations safe

0.3.0 2014-11-16 15:20 UTC

This package is not auto-updated.

Last update: 2024-12-17 04:01:38 UTC


README

Latest Stable Version Latest Version Software License Build Status Code Coverage Quality Score Total Downloads

Currency package introduces strictly typed and precision safe containers to work with currencies. Currency keeps internally amount as string (the only safe representation of floating point number in PHP). It ensures that once any amount in given currency is defined you cannot change it's type and you can type hint your methods to use nay currency or one exact currency. Internally this lib uses SplType extension and SplString class. Keeping amounts in string prevents from floats problems and lets you use high precision libraries like bcMath to process arithmetic operations.

See how floats work in PHP - http://php.net/manual/en/language.types.float.php

See what happen when you relies on float precision - http://stackoverflow.com/questions/3726721/php-math-precision

This package also supports well-known cryptocurrencies listed in wikipedia - http://en.wikipedia.org/wiki/Cryptocurrency#List_of_cryptocurrencies

Install

First you need to install SPL_Types - PECL extension:

$ pecl install SPL_Types

If SPL_Types is installed you can now install currency package via Composer:

$ composer require lukaszwit/currency

Usage

<?php
use Currency\Eur;

$eur = new Eur;
$eur = '10'; // this is OK

$eur = 10; // but this emits UnexpectedValueException

Type hinting

<?php
use Currency\Usd;
use Currency\AbstractCurrency as Currency;

class Example
{
    public function workWithCurrency(Currency $c)
    {

    }

    public function workOnlyWithUsd(Usd $usd)
    {

    }
}

see more examples in examples directory - https://github.com/lukaszwit/currency/tree/master/examples.

Testing

$ phpunit

Contributing

Please see CONTRIBUTING for details.

Credits

License

The MIT License (MIT). Please see License File for more information.

Bitdeli Badge