Add Money composition class to Support

This commit is contained in:
Nabeel Shahzad
2018-02-27 21:38:05 -06:00
parent ae0c3c5f27
commit a6afcc944c
2 changed files with 112 additions and 0 deletions

111
app/Support/Money.php Normal file
View File

@@ -0,0 +1,111 @@
<?php
/**
*
*/
namespace App\Support;
use Money\Currency;
use Money\Money as MoneyBase;
/**
* Compositional wrapper to MoneyPHP with some helpers
* @package App\Support
*/
class Money
{
public $money;
/**
* @param $amount
* @return MoneyBase
* @throws \InvalidArgumentException
*/
public static function create($amount)
{
return new MoneyBase(
$amount,
new Currency(config('phpvms.currency'))
);
}
/**
* Money constructor.
* @param $amount
* @throws \InvalidArgumentException
*/
public function __construct($amount)
{
$this->money = static::create($amount);
}
/**
* @return string
*/
public function getAmount()
{
return $this->money->getAmount();
}
/**
*
* @return string
*/
public function __toString()
{
// TODO: Implement __toString() method.
return $this->money->getAmount();
}
/**
* Add an amount
* @param $amount
*/
public function add($amount)
{
$this->money = $this->money->add($amount);
}
/**
* Subtract an amount
* @param $amount
*/
public function subtract($amount)
{
$this->money = $this->money->subtract($amount);
}
/**
* Multiply by an amount
* @param $amount
*/
public function multiply($amount)
{
$this->money = $this->money->multiply($amount);
}
/**
* Divide by an amount
* @param $amount
*/
public function divide($amount)
{
$this->money = $this->money->divide($amount);
}
/**
* @param $money
* @return bool
* @throws \InvalidArgumentException
*/
public function equals($money)
{
if($money instanceof Money) {
return $this->money->equals($money->money);
} elseif($money instanceof MoneyBase) {
return $this->money->equals($money);
} else {
return $this->money->equals(static::create($money));
}
}
}

View File

@@ -8,6 +8,7 @@ return [
'extensions' => [
'openssl',
'pdo',
'intl',
'mbstring',
'tokenizer',
'json',