Skip to content

Commit

Permalink
Put back the assertNumberFormat() calls.
Browse files Browse the repository at this point in the history
  • Loading branch information
jsacksick committed Feb 21, 2023
1 parent c7cc36b commit 4c90d45
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion src/Calculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ final class Calculator
*/
public static function add(string $first_number, string $second_number, int $scale = 6): string
{
self::assertNumberFormat($first_number);
self::assertNumberFormat($second_number);
$result = bcadd($first_number, $second_number, $scale);

return self::trim($result);
Expand All @@ -44,6 +46,8 @@ public static function add(string $first_number, string $second_number, int $sca
*/
public static function subtract(string $first_number, string $second_number, int $scale = 6): string
{
self::assertNumberFormat($first_number);
self::assertNumberFormat($second_number);
$result = bcsub($first_number, $second_number, $scale);

return self::trim($result);
Expand All @@ -62,6 +66,8 @@ public static function subtract(string $first_number, string $second_number, int
*/
public static function multiply(string $first_number, string $second_number, int $scale = 6): string
{
self::assertNumberFormat($first_number);
self::assertNumberFormat($second_number);
$result = bcmul($first_number, $second_number, $scale);

return self::trim($result);
Expand All @@ -80,6 +86,8 @@ public static function multiply(string $first_number, string $second_number, int
*/
public static function divide(string $first_number, string $second_number, int $scale = 6): string
{
self::assertNumberFormat($first_number);
self::assertNumberFormat($second_number);
$result = bcdiv($first_number, $second_number, $scale);

return self::trim($result);
Expand Down Expand Up @@ -139,6 +147,7 @@ public static function floor(string $number): string
*/
public static function round(string $number, int $precision = 0, int $mode = PHP_ROUND_HALF_UP): string
{
self::assertNumberFormat($number);
if (!is_numeric($precision) || $precision < 0) {
throw new \InvalidArgumentException('The provided precision should be a positive number');
}
Expand All @@ -154,7 +163,7 @@ public static function round(string $number, int $precision = 0, int $mode = PHP
// The rounding direction is based on the first decimal after $precision.
$number_parts = explode('.', $number);
$decimals = !empty($number_parts[1]) ? $number_parts[1] : '0';
$relevant_decimal = isset($decimals[$precision]) ? $decimals[$precision] : 0;
$relevant_decimal = $decimals[$precision] ?? 0;
if ($relevant_decimal < 5) {
$number = $rounded_down;
} elseif ($relevant_decimal == 5) {
Expand Down Expand Up @@ -190,6 +199,8 @@ public static function round(string $number, int $precision = 0, int $mode = PHP
*/
public static function compare(string $first_number, string $second_number, int $scale = 6): int
{
self::assertNumberFormat($first_number);
self::assertNumberFormat($second_number);
return bccomp($first_number, $second_number, $scale);
}

Expand All @@ -216,4 +227,18 @@ public static function trim(string $number): string
return $number;
}

/**
* Assert that the given number is a numeric string value.
*
* @param string $number The number to check.
*
* @throws \InvalidArgumentException
*/
public static function assertNumberFormat(string $number)
{
if (!is_numeric($number)) {
throw new \InvalidArgumentException(sprintf('The provided value "%s" is not a numeric value.', $number));
}
}

}

0 comments on commit 4c90d45

Please sign in to comment.