Clean up various code.

This commit is contained in:
James Cole
2018-07-22 12:52:07 +02:00
parent d193a6aec4
commit d4ba014a8a
14 changed files with 171 additions and 95 deletions

View File

@@ -33,12 +33,12 @@ class Amount implements ConverterInterface
* Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.
* - Jamie Zawinski.
*
*
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @SuppressWarnings(PHPMD.NPathComplexity)
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @param $value
*
* @return string
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function convert($value): string
{
@@ -46,59 +46,43 @@ class Amount implements ConverterInterface
return '0';
}
Log::debug(sprintf('Start with amount "%s"', $value));
$original = $value;
$value = (string)$value;
$value = $this->stripAmount($value);
$len = \strlen($value);
$decimalPosition = $len - 3;
$altPosition = $len - 2;
$decimal = null;
$original = $value;
$value = $this->stripAmount((string)$value);
$decimal = null;
if (($len > 2 && '.' === $value[$decimalPosition]) || ($len > 2 && strpos($value, '.') > $decimalPosition)) {
if ($this->decimalIsDot($value)) {
$decimal = '.';
Log::debug(sprintf('Decimal character in "%s" seems to be a dot.', $value));
}
if ($len > 2 && ',' === $value[$decimalPosition]) {
if ($this->decimalIsComma($value)) {
$decimal = ',';
Log::debug(sprintf('Decimal character in "%s" seems to be a comma.', $value));
}
// decimal character is null? find out if "0.1" or ".1" or "0,1" or ",1"
if ($len > 1 && ('.' === $value[$altPosition] || ',' === $value[$altPosition])) {
$decimal = $value[$altPosition];
Log::debug(sprintf('Alternate search resulted in "%s" for decimal sign.', $decimal));
if ($this->alternativeDecimalSign($value)) {
$decimal = $this->getAlternativeDecimalSign($value);
}
// decimal character still null? Search from the left for '.',',' or ' '.
if (null === $decimal) {
Log::debug('Decimal is still NULL, probably number with >2 decimals. Search for a dot.');
$res = strrpos($value, '.');
if (!(false === $res)) {
// blandly assume this is the one.
Log::debug(sprintf('Searched from the left for "." in amount "%s", assume this is the decimal sign.', $value));
$decimal = '.';
}
unset($res);
$decimal = $this->findFromLeft($value);
}
// if decimal is dot, replace all comma's and spaces with nothing. then parse as float (round to 4 pos)
if ('.' === $decimal) {
$search = [',', ' '];
$value = str_replace($search, '', $value);
Log::debug(sprintf('Converted amount from "%s" to "%s".', $original, $value));
}
if (',' === $decimal) {
$search = ['.', ' '];
$value = str_replace($search, '', $value);
$value = str_replace(',', '.', $value);
// if decimal is dot, replace all comma's and spaces with nothing
if (null !== $decimal) {
$value = $this->replaceDecimal($decimal, $value);
Log::debug(sprintf('Converted amount from "%s" to "%s".', $original, $value));
}
if (null === $decimal) {
// replace all:
$search = ['.', ' ', ','];
$value = str_replace($search, '', $value);
Log::debug(sprintf('No decimal character found. Converted amount from "%s" to "%s".', $original, $value));
}
if ($value{0} === '.') {
if ('.' === $value{0}) {
$value = '0' . $value;
}
@@ -114,18 +98,113 @@ class Amount implements ConverterInterface
return $formatted;
}
private function bcround($number, $scale = 0)
/**
* Check if the value has a dot or comma on an alternative place,
* catching strings like ",1" or ".5".
*
* @param string $value
*
* @return bool
*/
private function alternativeDecimalSign(string $value): bool
{
$fix = "5";
for ($i = 0; $i < $scale; $i++) {
$fix = "0$fix";
}
$number = bcadd($number, "0.$fix", $scale + 1);
$length = \strlen($value);
$altPosition = $length - 2;
return bcdiv($number, "1.0", $scale);
return $length > 1 && ('.' === $value[$altPosition] || ',' === $value[$altPosition]);
}
/**
* Helper function to see if the decimal separator is a comma.
*
* @param string $value
*
* @return bool
*/
private function decimalIsComma(string $value): bool
{
$length = \strlen($value);
$decimalPosition = $length - 3;
return $length > 2 && ',' === $value[$decimalPosition];
}
/**
* Helper function to see if the decimal separator is a dot.
*
* @param string $value
*
* @return bool
*/
private function decimalIsDot(string $value): bool
{
$length = \strlen($value);
$decimalPosition = $length - 3;
return ($length > 2 && '.' === $value[$decimalPosition]) || ($length > 2 && strpos($value, '.') > $decimalPosition);
}
/**
* Search from the left for decimal sign.
*
* @param string $value
*
* @return string
*/
private function findFromLeft(string $value): ?string
{
$decimal = null;
Log::debug('Decimal is still NULL, probably number with >2 decimals. Search for a dot.');
$res = strrpos($value, '.');
if (!(false === $res)) {
// blandly assume this is the one.
Log::debug(sprintf('Searched from the left for "." in amount "%s", assume this is the decimal sign.', $value));
$decimal = '.';
}
return $decimal;
}
/**
* Returns the alternative decimal point used, such as a dot or a comma,
* from strings like ",1" or "0.5".
*
* @param string $value
*
* @return string
*/
private function getAlternativeDecimalSign(string $value): string
{
$length = \strlen($value);
$altPosition = $length - 2;
return $value[$altPosition];
}
/**
* @param string $decimal
* @param string $value
*
* @return string
*/
private function replaceDecimal(string $decimal, string $value): string
{
$search = [',', ' ']; // default when decimal sign is a dot.
if (',' === $decimal) {
$search = ['.', ' '];
}
$value = str_replace($search, '', $value);
/** @noinspection CascadeStringReplacementInspection */
$value = str_replace(',', '.', $value);
return $value;
}
/**
* Strip amount from weird characters.
*
* @param string $value
*
* @return string