Giter Site home page Giter Site logo

money's Introduction

Money Value Object

Value object for handling monetary values

Scrutinizer Code Quality Code Coverage Build Status Packagist license

Installation

Via Composer:

composer require wsw/money

Usage

Instantiating a value:

<?php

use WSW\Money\Currency;
use WSW\Money\Money;

$money = new Money('1500.9999', new Currency('USD'));

Output values:

<?php

use WSW\Money\Currency;
use WSW\Money\Money;

$money = new Money("1500.9999", new Currency("USD"));

echo $money->getAmount();   // (string)  1500.999900
echo $money->getMicros();   // (integer) 1500999900
echo $money->getTruncate(); // (string)  1500.99
echo $money->getRound();    // (string)  1501.00
echo $money->getFormat();   // (string)  1.501,00

echo $money; // (string) 1.501,00

We verify that the values are equal:

<?php

use WSW\Money\Currency;
use WSW\Money\Money;

$money = new Money("1500.00", new Currency("USD"));
$money2 = new Money("1500.00", new Currency("USD"));
$money3 = new Money("1000.00", new Currency("USD"));

var_dump($money->equals($money2)); // bool(true)
var_dump($money->equals($money3)); // bool(false)

Compare values:

<?php

use WSW\Money\Currency;
use WSW\Money\Money;

$money  = new Money("1500.00", new Currency("USD"));
$money2 = new Money("1500.00", new Currency("USD"));
$money3 = new Money("1000.00", new Currency("USD"));

// Returns zero (0) when values and currencies are equal.
var_dump($money->compare($money2)); // int(0)

// Returns one (1) when the left object has the value greater than the right object.
var_dump($money->compare($money3)); // int(1)

// Returns minus one (-1) when the right object has the value greater than the left object.
var_dump($money3->compare($money)); // int(-1)

Add value:

<?php

use WSW\Money\Currency;
use WSW\Money\Money;

$money = new Money('100', new Currency('USD'));
$addValue = new Money('50', new Currency('USD'));

$newMoney = $money->add($addValue);

echo $newMoney->getAmount(); // (string) 150.000000
echo $newMoney->getTruncate(); // (string) 150.00
echo $newMoney->getRound(); // (string) 150.00
echo $newMoney->getFormat(); // (string) 150,00

echo $newMoney; // (string) 150,00

Add percentage value:

<?php

use WSW\Money\Currency;
use WSW\Money\Money;
use WSW\Money\Percentage;

$money = new Money('100', new Currency('USD'));
$percent = new Percentage("75%");

$newMoney = $money->addPercent($percent);

echo $newMoney->getAmount(); // (string) 175.000000
echo $newMoney->getTruncate(); // (string) 175.00
echo $newMoney->getRound(); // (string) 175.00
echo $newMoney->getFormat(); // (string) 175,00

echo $newMoney; // (string) 175,00

Subtract value:

<?php

use WSW\Money\Currency;
use WSW\Money\Money;

$money = new Money('100', new Currency('USD'));
$subValue = new Money('60.75', new Currency('USD'));

$newMoney = $money->sub($subValue);

echo $newMoney->getAmount(); // (string) 39.250000
echo $newMoney->getTruncate(); // (string) 39.25
echo $newMoney->getRound(); // (string) 39.25
echo $newMoney->getFormat(); // (string) 39,25

echo $newMoney; // (string) 39,25

Subtract percentage value:

<?php

use WSW\Money\Currency;
use WSW\Money\Money;
use WSW\Money\Percentage;

$money = new Money('100', new Currency('USD'));
$percent = new Percentage("75%");

$newMoney = $money->subPercent($percent);

echo $newMoney->getAmount(); // (string) 25.000000
echo $newMoney->getTruncate(); // (string) 25.00
echo $newMoney->getRound(); // (string) 25.00
echo $newMoney->getFormat(); // (string) 25,00

echo $newMoney; // (string) 25,00

Testing

$ composer test

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

money's People

Contributors

whera avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

npcalex

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.