Currency
, (*1)
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., (*2)
See how floats work in PHP - http://php.net/manual/en/language.types.float.php, (*3)
See what happen when you relies on float precision - http://stackoverflow.com/questions/3726721/php-math-precision, (*4)
This package also supports well-known cryptocurrencies listed in wikipedia - http://en.wikipedia.org/wiki/Cryptocurrency#List_of_cryptocurrencies, (*5)
Install
First you need to install SPL_Types - PECL extension:, (*6)
``` bash
$ pecl install SPL_Types, (*7)
If SPL_Types is installed you can now install currency package via Composer:
``` bash
$ 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, (*8)
<?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., (*9)
Testing
bash
$ phpunit
, (*10)
Contributing
Please see CONTRIBUTING for details., (*11)
Credits
License
The MIT License (MIT). Please see License File for more information., (*12)
, (*13)