From f397142aabea63ab428e793006fe54427a304ae6 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Thu, 3 Mar 2016 14:18:42 +0100 Subject: [PATCH] Objectify --- Localization/Money.php | 52 +++++++++++++++++++++++++----------------- 1 file changed, 31 insertions(+), 21 deletions(-) diff --git a/Localization/Money.php b/Localization/Money.php index 99fed2a3c..1e0978d47 100644 --- a/Localization/Money.php +++ b/Localization/Money.php @@ -5,27 +5,25 @@ namespace phpOMS\Localization; class Money { 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}) { - $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, $thousands, $decimal); . $decimal . $right : (string) $left; + $this->currency = $currency; + $this->thousands = $thousands; + $this->decimal = $decimal; + } + + public function setInt(int $value) { + $this->value = $value; } - private static function getFromString(string $value, string $currency = 'USD', string $thousands = ',', string $decimal = '.', int $decimals = 2) : int - { - if($decimals > ISO4270::{$currency}) { - $decimals = ISO4270::{$currency}; - } - + public function setString(string $value) { $split = explode($value, $decimal); $left = ''; @@ -38,8 +36,20 @@ class Money { } $right = substr($right, 0, -self::DECIMALS); - $value = (int) round((int) $left + (int) $right, - self::DECIMALS + $decimals); - - return $value; + $this->value = (int) round((int) $left + (int) $right, - self::DECIMALS + $decimals); + } + + 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; } }