From aef5b006c7628b0269235e2aa93a11f9f8a7d78e Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Thu, 3 Mar 2016 14:42:13 +0100 Subject: [PATCH] Adding basic arithmetic --- Localization/Money.php | 65 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 59 insertions(+), 6 deletions(-) diff --git a/Localization/Money.php b/Localization/Money.php index 1e0978d47..1d7a1e924 100644 --- a/Localization/Money.php +++ b/Localization/Money.php @@ -22,13 +22,22 @@ class Money { public function setInt(int $value) { $this->value = $value; } + + public function getInt() : int { + return $this->value; + } public function setString(string $value) { + $this->value = self::toInt($value, $this->decimal); + } + + public static function toInt(string $value, string $decimal = ',') : int + { $split = explode($value, $decimal); $left = ''; $left = $split[0]; - $left = str_replace($thousands, '', $left); + $left = str_replace($this->thousands, '', $left); $rigth = ''; if(count($split) > 1) { @@ -36,20 +45,64 @@ class Money { } $right = substr($right, 0, -self::DECIMALS); - $this->value = (int) round((int) $left + (int) $right, - self::DECIMALS + $decimals); + $this->value = (int) $left * 100000 + (int) $right; } public function getAmount(int $decimals = 2) : string { - if($decimals > ISO4270::{$currency}) { - $decimals = ISO4270::{$currency}; + if($decimals > ISO4270::{$this->currency}) { + $decimals = ISO4270::{$this->currency}; } - $value = (string) round($value, - self::DECIMALS + $decimals); + $value = (string) round($value, - self::DECIMALS + $this->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; + return ($decimals > 0) : number_format($left, 0, $this->thousands, $this->decimal); . $this->decimal . $right : (string) $left; + } + + public function add($value) + { + if(is_string($value) || is_float($value)) { + $this->value += self::toInt((string) $value); + } elseif(is_int($value)) { + $this->value += $value; + } elseif($value instanceof Money) { + $this->value += $value->getInt(); + } + } + + public function sub($value) + { + if(is_string($value) || is_float($value)) { + $this->value -= self::toInt((string) $value); + } elseif(is_int($value)) { + $this->value -= $value; + } elseif($value instanceof Money) { + $this->value -= $value->getInt(); + } + } + + public function mult($value) + { + if(is_string($value) || is_float($value)) { + $this->value *= self::toInt((string) $value); + } elseif(is_int($value)) { + $this->value *= $value; + } elseif($value instanceof Money) { + $this->value *= $value->getInt(); + } + } + + public function div($value) + { + if(is_string($value) || is_float($value)) { + $this->value = self::toInt((string) ($this->value / self::toInt((string) $value))); + } elseif(is_int($value)) { + $this->value = self::toInt((string) ($this->value / $value)); + } elseif($value instanceof Money) { + $this->value = self::toInt((string) ($this->value / $value->getInt())); + } } }