2017 © Pedro Peláez
 

library pricing

Pricing

image

commerceguys/pricing

Pricing

  • Wednesday, May 10, 2017
  • by bojanz
  • Repository
  • 22 Watchers
  • 146 Stars
  • 27,705 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 13 Forks
  • 7 Open issues
  • 1 Versions
  • 1 % Grown

The README.md

pricing

ABANDONED: Use moneyphp/money instead., (*1)

A PHP 5.4+ library for working with prices., (*2)

Depends on commerceguys/intl for currency information and formatting., (*3)

Prices

A price is a value object. Each operation (add, subtract, multiply, divide, round) produces a new price instance. All amounts are passed as strings, and manipulated using bcmath., (*4)

use CommerceGuys\Intl\Currency\CurrencyRepository;
use CommerceGuys\Pricing\Price;

$currencyRepository = new CurrencyRepository;
$currency = $currencyRepository->get('EUR');

// $firstPrice, $secondPrice, $thirdPrice, $total are all Price instances.
$firstPrice  = new Price('99.99', $currency);
$secondPrice = new Price('100', $currency);
$thirdPrice  = new Price('20.307', $currency);
// Every operation produces a new Price instance.
$total = $firstPrice
            ->add($secondPrice)
            ->subtract($thirdPrice)
            ->multiply('4')
            ->divide('2');
echo $total; // 359.366  EUR
echo $total->round(); // 359.37  EUR
echo $total->round(Price::ROUND_HALF_UP, 1); // 359.4 EUR
echo $total->greaterThan($firstPrice); // true

Currency conversion

use CommerceGuys\Intl\Currency\CurrencyRepository;
use CommerceGuys\Pricing\Price;

$currencyRepository = new CurrencyRepository;
$eur = $currencyRepository->get('EUR');
$usd = $currencyRepository->get('USD');

// Use an external library to get an actual exchange rate.
$rate = 1;
$eurPrice = new Price('100', $eur);
$usdPrice = $eurPrice->convert($usd, $rate);
echo $usdPrice;

An external library like Swap can be used to retrieve exchange rates., (*5)

Formatting

Use the NumberFormatter class provided by commerceguys/intl., (*6)

use CommerceGuys\Intl\Currency\CurrencyRepository;
use CommerceGuys\Intl\NumberFormat\NumberFormatRepository;
use CommerceGuys\Intl\Formatter\NumberFormatter;
use CommerceGuys\Pricing\Price;

$currencyRepository = new CurrencyRepository;
$currency = $currencyRepository->get('USD');
$price = new Price('99.99', $currency);

$numberFormatRepository = new NumberFormatRepository;
$numberFormat = $numberFormatRepository->get('en-US');

$currencyFormatter = new NumberFormatter($numberFormat, NumberFormatter::CURRENCY);
echo $currencyFormatter->formatCurrency($price->getAmount(), $price->getCurrency()); // $99.99

The Versions

10/05 2017

dev-master

9999999-dev

Pricing

  Sources   Download

MIT

The Requires

 

by Bojan Zivanovic