2017 © Pedro Peláez
 

library price

Simple price model

image

ayeo/price

Simple price model

  • Thursday, July 26, 2018
  • by ayeo
  • Repository
  • 6 Watchers
  • 1 Stars
  • 5,848 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 2 Forks
  • 0 Open issues
  • 17 Versions
  • 14 % Grown

The README.md

Build Status Software License Packagist Version Coverage, (*1)

Price

Simple DDD price model. The goal is to make model usage as easy as possible. Creating object is easy and dont require any additional objects. Object acts as Value Object - is immutable and self-validating. It is designed to be side effect free., (*2)

API

Building

$price = new Price(float $nett, float $gross, "GBP")
$price = Price::buildByNett(float $nett, integer $tax, "USD") - returns Price
$price = Price::buildByGross(float $gross, integer $tax, "EUR") - returns Price

Tax

Tax aspects may be a bit confusing at first glance. You may need to build price providing tax rate:, (*3)

$price = Price::buildByNett(100.00, 8, "USD"):

In this case tax rate is known and it is equal to 8%. When you adding or subtracting prices with same tax rate the result price will come up with same rate. But if you operate with different rates result price has unknown tax rate., (*4)

$A = Price::buildByNett(100.00, 8, "USD"):
$B = Price::buildByNett(10.00, 11, "USD"):
$C = $A->add($B);
$C->hasTaxRate(); //returns false

You can still get tax percentage value (but it is not the rate!) using:, (*5)

$C->getTax()->getValue(); 

Operations

$priceA->add(Price $priceB) - returns Price
$priceA->subtract(Price $priceB) - returns Price
$priceA->multiply(integer $times) - returns Price

$priceA->addGross(float $value) - returns Price
$priceA->subtractGross(float $value) - returns Price

Immutable

Operations creates new instances, (*6)

$A = new Price(100.00, 120.00, 'USD');
$B = new Price(10.00, 12.00, 'USD');

$sum = $A->add($B);
$sum->getGross(); //returns 132.00
$A->getGross(); //returns 120.00
$B->getGross(); //returns 12.00

Comparing

$priceA->isEqual(Price $priceB) - returns bool
$priceA->isLower(Price $priceB) - returns bool
$priceA->isGreater(Price $priceB) - returns bool

Constraints

  • Nett and gross must be positive
  • Gross must not be lower than nett
  • Tax must be integer
  • Currency symbol is optional but if appears must follow iso 4217 (3 uppercase chars)

Todo

There exists currencies with different precision than 2. The map must be developed. https://en.wikipedia.org/wiki/ISO_4217#Active_codes, (*7)

Contributing

Feel free to PR, tests must pass., (*8)

The Versions

26/07 2018

dev-master

9999999-dev https://github.com/ayeo/price

Simple price model

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

model price

26/07 2018

1.0.13

1.0.13.0 https://github.com/ayeo/price

Simple price model

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

model price

17/05 2018

1.0.12

1.0.12.0 https://github.com/ayeo/price

Simple price model

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

model price

09/03 2018

1.0.11

1.0.11.0 https://github.com/ayeo/price

Simple price model

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

model price

09/03 2018

dev-tax-fix

dev-tax-fix https://github.com/ayeo/price

Simple price model

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

model price

08/03 2018

1.0.10

1.0.10.0 https://github.com/ayeo/price

Simple price model

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

model price

18/02 2018

1.0.9

1.0.9.0 https://github.com/ayeo/price

Simple price model

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

model price

10/02 2018

1.0.8

1.0.8.0 https://github.com/ayeo/price

Simple price model

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

model price

09/02 2018

1.0.7

1.0.7.0 https://github.com/ayeo/price

Simple price model

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

model price

09/02 2018

dev-empty-price

dev-empty-price https://github.com/ayeo/price

Simple price model

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

model price

09/02 2018

1.0.6

1.0.6.0 https://github.com/ayeo/price

Simple price model

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

model price

09/06 2017

1.0.5

1.0.5.0 https://github.com/ayeo/price

Simple price model

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

model price

09/06 2017

1.0.4

1.0.4.0 https://github.com/ayeo/price

Simple price model

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

model price

02/10 2016

1.0.3

1.0.3.0 https://github.com/ayeo/price

Simple price model

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

model price

16/08 2016

1.0.2

1.0.2.0 https://github.com/ayeo/price

Simple price model

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

model price

13/11 2015

1.0.1

1.0.1.0 https://github.com/ayeo/price

Simple price model

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

model price

16/10 2015

1.0.0

1.0.0.0 https://github.com/ayeo/price

Simple price model

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

model price