Download the PHP package happy-types/money-type without Composer
On this page you can find all versions of the php package happy-types/money-type. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download happy-types/money-type
More information about happy-types/money-type
Files in happy-types/money-type
Package money-type
Short Description Money library implements Money Value Object which contains amount and currency in the same object.
License MIT
Homepage https://github.com/antanas-arvasevicius/php-money-type
Informations about the package money-type
Money library
This library adds a new type Money which abstracts a real money object. Money has an Value and Currency. Without Money object you must store these values in separate variables like $amount, $currency. Its really hard to produce nice API methods without money object.
Example:
Should it return value or object ?
or better?
now you can apply some mathematics to money object:
or add and convert to some currency in same time
or with mutiple currencies ? wha?
comparing moneys with different currencies ?
To enable currency conversion you must implement interface and register it to using:
Implementation example of can be found at
Money
Class Money is a Value Object which means that all operations will create new instance of Money and all instances are immutable
This class is used in all system to describe money value. It is {amount, currency} tuple.
Methods , , , will implicitly convert specified argument into appropriate currency.
For currency conversion you can implement interface and register your money converter using
Default decimal precision is 4 digits
You can compare only two objects with exact the same precisions. All operations will use highest precisions of available operands.
use static factory to create Money instances. don`t use constructors directly!
amount must be specified in string type and decimal separator must be a dot (e.g. "123.45") You cannot specify or . only <- string, and dot separated number
Money amount must be string type only. Number must be dot separated. e.g. "123.45" not "123,45" (bc limitation) You cannot use float or integer types. Its defensive restriction to ensure that you wont loose precision accidentaly. In case if you want to specify integers or floats or string you can use Money::undefined($currency = '', $precision = -1)Money::create(false)Money::create(null)Money::create('')` e.g. Money::create('') === Money::undefined(); Money::create('0.00') !== Money::undefined(); isDefined()isUndefined() a = Money::create('10.00', 'LTL') b = a->convertTo('USD')->convertTo('LVL')->convertTo('LTL'); isEqualExact()add()subtract()Money::setDefaultCurrency($defaultCurrency)composer.json`` "require": { "happy-types/money-type" : "~1.1" } } composer update require_once(dirname(FILE).'/vendor/autoload.php'); ```
example is for index.php which is in project root folder.