2017 © Pedro Peláez
 

library price

image

leaphly/price

  • Tuesday, June 16, 2015
  • by liuggio
  • Repository
  • 4 Watchers
  • 45 Stars
  • 3,865 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 6 Forks
  • 1 Open issues
  • 3 Versions
  • 5 % Grown

The README.md

Price

Build Status Total Downloads Latest Stable Version, (*1)

A Price is the amount of a product in different currencies.

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')

Why?!

  • 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 :).

Goodies:

  • 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

Simple usage

  • The T-Shirt costs 10€ and 8£

Constructor

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

Usage with mixed explicit and converted values

$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

Do we use the same language?

  • An espresso coffee costs [2€ or 2.3$] here and [1€ or 1.2$] take away.

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)

API (still not stable)

Price

    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();

Example sum two prices

$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

With the \Iterator interface

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)

License License

This library is under the MIT license. See the complete license in the repository:, (*12)

Resources/meta/LICENSE

Test

bash composer.phar create-project leaphly/price ~1` bin/phpunit, (*13)

About

See also the list of contributors., (*14)

Reporting an issue or a feature request

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)

The Versions

16/06 2015

dev-master

9999999-dev

  Sources   Download

MIT

The Requires

 

The Development Requires

02/09 2014

v1.0.0-alpha2

1.0.0.0-alpha2

  Sources   Download

MIT

The Requires

 

The Development Requires

29/08 2014

v1.0.0-alpha

1.0.0.0-alpha

  Sources   Download

MIT

The Requires

 

The Development Requires