Revert "Better localization handling"

This reverts commit 738e2be03e.
This commit is contained in:
Dennis Eichhorn 2016-08-09 14:25:09 +02:00
parent 738e2be03e
commit 2a02969689

View File

@ -43,7 +43,7 @@ class Money implements \Serializable
* @var string
* @since 1.0.0
*/
private static $thousands = ',';
private $thousands = ',';
/**
* Decimal separator.
@ -51,23 +51,7 @@ class Money implements \Serializable
* @var string
* @since 1.0.0
*/
private static $decimal = '.';
/**
* Currency symbol.
*
* @var string
* @since 1.0.0
*/
private static $symbol = '';
/**
* Symbol position.
*
* @var int
* @since 1.0.0
*/
private static $position = 0;
private $decimal = '.';
/**
* Value.
@ -83,34 +67,15 @@ class Money implements \Serializable
* @param string|int|float $value Value
* @param string $thousands Thousands separator
* @param string $decimal Decimal separator
* @param string $symbol Currency symbol
* @param int $pos Symbol position
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function __construct($value = 0)
public function __construct($value = 0, string $thousands = ',', string $decimal = '.')
{
$this->value = is_int($value) ? $value : self::toInt((string) $value);
}
/**
* Set localization.
*
* @param string $thousands Thousands separator
* @param string $decimal Decimal separator
* @param string $symbol Currency symbol
* @param int $pos Symbol position
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public static function setLocalization(string $thousands = ',', string $decimal = '.', string $symbol = '', int $pos = 0)
{
self::$thousands = $thousands;
self::$decimal = $decimal;
self::$symbol = $symbol;
self::$position = $pos;
$this->value = is_int($value) ? $value : self::toInt((string) $value);
$this->thousands = $thousands;
$this->decimal = $decimal;
}
/**
@ -125,7 +90,7 @@ class Money implements \Serializable
*/
public function setString(string $value) : Money
{
$this->value = self::toInt($value, self::$thousands, self::$decimal);
$this->value = self::toInt($value, $this->thousands, $this->decimal);
return $this;
}
@ -168,35 +133,14 @@ class Money implements \Serializable
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function getAmount(int $decimals = 2, string $thousands = null, string $decimal = null) : string
public function getAmount(int $decimals = 2) : string
{
$value = (string) round($this->value, -self::MAX_DECIMALS + $decimals);
$thousands = $thousands ?? self::$thousands;
$decimal = $decimal ?? self::$decimal;
$left = substr($value, 0, -self::MAX_DECIMALS);
$right = substr($value, -self::MAX_DECIMALS);
$value = (string) round($this->value, -self::MAX_DECIMALS + $decimals);
return ($decimals > 0) ? number_format($left, 0, $decimal, $thousands) . $decimal . substr($right, 0, $decimals) : (string) $left;
}
$left = substr($value, 0, -self::MAX_DECIMALS);
$right = substr($value, -self::MAX_DECIMALS);
/**
* Get money.
*
* @param int $decimals Precision
*
* @return string
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function getCurrency(int $decimals = 2, string $thousands = null, string $decimal = null, string $symbol = null, int $position = null) : string
{
$thousands = $thousands ?? self::$thousands;
$decimal = $decimal ?? self::$decimal;
$symbol = $symbol ?? self::$symbol;
$position = $position ?? self::$position;
return ($position === 0 ? $smbol : '') . $this->getAmount($decimals, $thousands, $decimal) . ($position === 1 ? $smbol : '');
return ($decimals > 0) ? number_format($left, 0, $this->decimal, $this->thousands) . $this->decimal . substr($right, 0, $decimals) : (string) $left;
}
/**
@ -212,7 +156,7 @@ class Money implements \Serializable
public function add($value) : Money
{
if (is_string($value) || is_float($value)) {
$this->value += self::toInt((string) $value, self::$thousands, self::$decimal);
$this->value += self::toInt((string) $value, $this->thousands, $this->decimal);
} elseif (is_int($value)) {
$this->value += $value;
} elseif ($value instanceof Money) {
@ -248,7 +192,7 @@ class Money implements \Serializable
public function sub($value) : Money
{
if (is_string($value) || is_float($value)) {
$this->value -= self::toInt((string) $value, self::$thousands, self::$decimal);
$this->value -= self::toInt((string) $value, $this->thousands, $this->decimal);
} elseif (is_int($value)) {
$this->value -= $value;
} elseif ($value instanceof Money) {