Wallogit.com
2017 © Pedro Peláez
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:, (*2)
$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')
addition, multiplication, division, subtraction ...DoctrineType
Usage with explicit currency values., (*3)
$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
espresso is a product., (*4)
here and take away are contexts (still is a missing feature)., (*5)
2€ 2.3$ is a Price with 2 currencies,, (*6)
1€ 1.2$ is a Price with 2 currencies,, (*7)
2€ or 2.3$ here, and 1€ or 1.2$ for take away. is a PriceList (still is a missing feature)., (*8)
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., (*9)
``` php $price = new Price .... foreach ($price as $money) { echo $money->getAmount() . ' in '. $money->getCurrencies(); }, (*10)
#### Use it with the Money Value Object ``` php 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., (*11)
This library is under the MIT license. See the complete license in the repository:, (*12)
Resources/meta/LICENSE
bash
composer.phar create-project leaphly/price ~1`
bin/phpunit, (*13)
See also the list of contributors., (*14)
Issues and feature requests are tracked in the Github issue tracker., (*15)
Note: this library uses the dev version of the Mathias Verraes Money., (*16)