Skip to content

Commit

Permalink
add scientific notation handling to the sanitize method
Browse files Browse the repository at this point in the history
  • Loading branch information
514sid committed Nov 25, 2023
1 parent d38b57d commit 9460b6a
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions src/NonNumericFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,26 @@ public static function sanitize(string|int|float $value, ?DecimalSeparator $deci
if (is_string($value)) {
$value = trim($value);

$decimalSeparator = $decimalSeparator ?? DecimalSeparatorGuesser::guess($value);
$cleanedValue = preg_replace('/[^\d' . preg_quote($decimalSeparator->value) . ']/', '', $value);
$decimalSeparator = $decimalSeparator ?? DecimalSeparatorGuesser::guess($value);

if ($decimalSeparator === DecimalSeparator::COMMA) {
$cleanedValue = str_replace($decimalSeparator->value, DecimalSeparator::POINT->value, $cleanedValue);
}
if (strpos($value, 'e') !== false) {
return self::handleScientificNotation($value);
}

$floatValue = (float) $cleanedValue;
$cleanedValue = preg_replace('/[^\d' . preg_quote($decimalSeparator->value) . ']/', '', $value);

if ($decimalSeparator === DecimalSeparator::COMMA) {
$cleanedValue = str_replace($decimalSeparator->value, DecimalSeparator::POINT->value, $cleanedValue);
}

$floatValue = (float) $cleanedValue;

return self::isNegative($value) ? -$floatValue : $floatValue;
}
}

if (is_numeric($value)) {
return $value;
}
if (is_numeric($value)) {
return $value;
}

throw new \InvalidArgumentException('The value must be either numeric or a string.');
}
Expand Down

0 comments on commit 9460b6a

Please sign in to comment.