In Dollar a t-shirt costs 4$,
in Eur the same t-shirt costs 3€
if British Pound the cost is given by the current conversion of 0.7900
in PHP is:
$tShirtPrice = new Price(
[
'EUR' => 300,
'USD' => 400
],
['EUR/GBP 0.7900'] // array of conversions
);
echo $tShirtPrice->inEUR(); // 300 same as ->getAmount('EUR')
echo $tShirtPrice->inUSD(); // 400 same as ->getAmount('USD')
echo $tShirtPrice->inGBP(); // 237 same as ->getAmount('GBP')
- Because is not recommended to work with the float for the money in PHP...
- Because is better to implement money as value objects.
- Because in the e-commerce domain a product has always* a different price for different currencies.
- Because we needed :).
- It helps you to work with money.
- It helps you to work with currencies.
- It helps you to work with multiple currencies, converted or explicit.
- It is shipped with some math operations:
addition
,multiplication
,division
,subtraction
... - This library extends the mathiasverraes/money.
- Immutable Value Object.
- Shipped with an example of
DoctrineType
- The T-Shirt costs 10€ and 8£
Usage with explicit currency values.
$ticketPrice = new Price(
[
'EUR' => 1000,
'GBP' => 800
]
);
echo $ticketPrice->inEUR(); // return 1000
var_dump($ticketPrice->availableCurrencies()); // array with EUR, GBP
$ticketPrice = new Price(
[
'EUR' => 100,
'USD' => 130
],
['EUR/GBP 0.7901'] // this is an array of conversions with the ISO standard format.
);
echo $ticketPrice->inEUR(); // 100
echo $ticketPrice->inGBP(); // 79 is calculated
var_dump($ticketPrice->availableCurrencies()); // array with EUR, USD, GBP
- An espresso coffee costs [2€ or 2.3$] here and [1€ or 1.2$] take away.
espresso
is a product.
here
and take away
are contexts (still is a missing feature).
2€
2.3$
is a Price with 2 currencies,
1€
1.2$
is a Price with 2 currencies,
2€ or 2.3$ here, and 1€ or 1.2$ for take away.
is a PriceList (still is a missing feature).
public function inXYZ($currency); // ZYX is a valid currency like EUR or GBP
public function getAmount($currency);
public function hasAmount($currency);
public function availableCurrencies();
public function equals(Price $other);
public function add(Price $addend);
public function subtract(Price $subtrahend);
public function multiply($multiplier);
public function divide($divisor);
public function isZero();
$ticketPrice = new Price(
[
'EUR' => 100,
'USD' => 130
],
['EUR/GBP 0.7901'] // this is an array of conversions
);
$shirtPrice = new Price(
[
'EUR' => 200,
'CHF' => 300,
'GBP' => 400
],
);
// sum
$sumPrice = $ticketPrice->add($shirtPrice);
$sumPrice->inEUR(); // 100+200= 400
$sumPrice->inGBP(); // 79+400= 479
$sumPrice->inUSD(); // 130
$sumPrice->inCHF(); // 300
Implement the \Iterator
so Price is an array of Money.
$price = new Price ....
foreach ($price as $money) {
echo $money->getAmount() . ' in '. $money->getCurrencies();
}
use Money\Money;
use Money\CurrencyPair;
$price = new Price(
array(
Money::EUR(5),
Money::USD(10),
Money::GBP(10),
'TRY' => 120 // or mixed
),
[
CurrencyPair::createFromIso('USD/CHF 1.5'),
]
);
Note: the iteration is valid only on the explicit currencies not on the converted one.
This library is under the MIT license. See the complete license in the repository:
Resources/meta/LICENSE
composer.phar create-project leaphly/price ~1`
bin/phpunit
See also the list of contributors.
Issues and feature requests are tracked in the Github issue tracker.
Note: this library uses the dev
version of the Mathias Verraes Money.