Objectify

This commit is contained in:
Dennis Eichhorn 2016-03-03 14:18:42 +01:00
parent 2438bd28da
commit f397142aab

View File

@ -6,26 +6,24 @@ class Money {
const DECIMALS = 5; const DECIMALS = 5;
private static function getFromInt(int $value, string $currency = 'USD', string $thousands = ',', string $decimal = '.', int $decimals = 2) : string private $currency = 'USD';
private $thousands = ',';
private $decimal = '.';
private $value = 0;
public function __construct(string $currency = 'USD', string $thousands = ',', string $decimal = '.')
{ {
if($decimals > ISO4270::{$currency}) { $this->currency = $currency;
$decimals = ISO4270::{$currency}; $this->thousands = $thousands;
} $this->decimal = $decimal;
$value = (string) round($value, - self::DECIMALS + $decimals);
$left = substr($value, 0, -self::DECIMALS);
$right = substr($value, -self::DECIMALS);
return ($decimals > 0) : number_format($left, 0, $thousands, $decimal); . $decimal . $right : (string) $left;
} }
private static function getFromString(string $value, string $currency = 'USD', string $thousands = ',', string $decimal = '.', int $decimals = 2) : int public function setInt(int $value) {
{ $this->value = $value;
if($decimals > ISO4270::{$currency}) { }
$decimals = ISO4270::{$currency};
}
public function setString(string $value) {
$split = explode($value, $decimal); $split = explode($value, $decimal);
$left = ''; $left = '';
@ -38,8 +36,20 @@ class Money {
} }
$right = substr($right, 0, -self::DECIMALS); $right = substr($right, 0, -self::DECIMALS);
$value = (int) round((int) $left + (int) $right, - self::DECIMALS + $decimals); $this->value = (int) round((int) $left + (int) $right, - self::DECIMALS + $decimals);
}
return $value; public function getAmount(int $decimals = 2) : string
{
if($decimals > ISO4270::{$currency}) {
$decimals = ISO4270::{$currency};
}
$value = (string) round($value, - self::DECIMALS + $decimals);
$left = substr($value, 0, -self::DECIMALS);
$right = substr($value, -self::DECIMALS);
return ($decimals > 0) : number_format($left, 0, $this->thousands, $this->decimal); . $decimal . $right : (string) $left;
} }
} }