From 307781d41d40f15a6c460493da4c101a55771f08 Mon Sep 17 00:00:00 2001 From: 514sid <140138716+514sid@users.noreply.github.com> Date: Sun, 3 Sep 2023 11:24:50 +0400 Subject: [PATCH] remove enums for php 8.0 support --- composer.json | 6 ++--- src/DecimalSeparatorGuesser.php | 25 ++++++++++---------- src/Enums/DecimalSeparator.php | 9 -------- src/NonNumericFilter.php | 10 ++++---- src/Num.php | 7 +++--- src/NumberValidator.php | 10 ++++---- tests/DecimalSeparatorGuesserTest.php | 33 +++++++++++++-------------- tests/NumTest.php | 5 ++-- 8 files changed, 44 insertions(+), 61 deletions(-) delete mode 100644 src/Enums/DecimalSeparator.php diff --git a/composer.json b/composer.json index 017abb7..3ee36dc 100644 --- a/composer.json +++ b/composer.json @@ -14,11 +14,11 @@ } }, "require": { - "php": "^8.1" + "php": "^8.0" }, "require-dev": { - "phpunit/phpunit": "^10.1", - "orchestra/testbench": "^8.8" + "phpunit/phpunit": "^9.5|^10.1", + "orchestra/testbench": "^7.0|^8.0" }, "scripts": { "tests": "phpunit", diff --git a/src/DecimalSeparatorGuesser.php b/src/DecimalSeparatorGuesser.php index a13a411..4b830fb 100644 --- a/src/DecimalSeparatorGuesser.php +++ b/src/DecimalSeparatorGuesser.php @@ -3,22 +3,21 @@ namespace Num; use Num\NumberValidator; -use Num\Enums\DecimalSeparator; class DecimalSeparatorGuesser { /** * Guesses the appropriate decimal separator for the given numeric value. */ - public static function guess(string $value): DecimalSeparator + public static function guess(string $value): string { // Count occurrences of decimal point and comma in the value - $pointCount = substr_count($value, DecimalSeparator::POINT->value); - $commaCount = substr_count($value, DecimalSeparator::COMMA->value); + $pointCount = substr_count($value, '.'); + $commaCount = substr_count($value, ','); // If there are no decimal points or commas, default to decimal point if ($pointCount === 0 && $commaCount === 0) { - return DecimalSeparator::POINT; + return '.'; } // Check if the value can be treated as an integer @@ -26,25 +25,25 @@ public static function guess(string $value): DecimalSeparator // Determine the appropriate separator based on counts and integer check if ($pointCount > 0 && $commaCount === 0) { - return self::selectDecimalSeparator(!$canBeInteger, DecimalSeparator::POINT, DecimalSeparator::COMMA); + return self::selectDecimalSeparator(!$canBeInteger, '.', ','); } if ($commaCount > 0 && $pointCount === 0) { - return self::selectDecimalSeparator(!$canBeInteger, DecimalSeparator::COMMA, DecimalSeparator::POINT); + return self::selectDecimalSeparator(!$canBeInteger, ',', '.'); } // Choose separator based on the last occurrence of each separator return self::selectDecimalSeparator( - self::lastPosition($value, DecimalSeparator::POINT) > self::lastPosition($value, DecimalSeparator::COMMA), - DecimalSeparator::POINT, - DecimalSeparator::COMMA + self::lastPosition($value, '.') > self::lastPosition($value, ','), + '.', + ',' ); } /** * Selects a decimal separator based on a condition. */ - private static function selectDecimalSeparator(bool $condition, DecimalSeparator $trueOption, DecimalSeparator $falseOption): DecimalSeparator + private static function selectDecimalSeparator(bool $condition, string $trueOption, string $falseOption): string { return $condition ? $trueOption : $falseOption; } @@ -52,9 +51,9 @@ private static function selectDecimalSeparator(bool $condition, DecimalSeparator /** * Returns the position of the last occurrence of a separator in the string. */ - private static function lastPosition(string $string, DecimalSeparator $separator): int + private static function lastPosition(string $string, string $separator): int { - $lastPosition = strrpos($string, $separator->value); + $lastPosition = strrpos($string, $separator); return $lastPosition !== false ? $lastPosition : -1; } diff --git a/src/Enums/DecimalSeparator.php b/src/Enums/DecimalSeparator.php deleted file mode 100644 index c86b962..0000000 --- a/src/Enums/DecimalSeparator.php +++ /dev/null @@ -1,9 +0,0 @@ -value) . ']/', '', $value); + $cleanedValue = preg_replace('/[^\d' . preg_quote($decimalSeparator) . ']/', '', $value); - if ($decimalSeparator === DecimalSeparator::COMMA) { - $cleanedValue = str_replace($decimalSeparator->value, DecimalSeparator::POINT->value, $cleanedValue); + if ($decimalSeparator === ',') { + $cleanedValue = str_replace($decimalSeparator, '.', $cleanedValue); } $float = (float) $cleanedValue; diff --git a/src/Num.php b/src/Num.php index bf6be36..d8b208e 100644 --- a/src/Num.php +++ b/src/Num.php @@ -3,12 +3,11 @@ namespace Num; use Num\NonNumericFilter; -use Num\Enums\DecimalSeparator; use Num\DecimalSeparatorGuesser; class Num { - public static function float(float|int|string|null $value, ?DecimalSeparator $decimalSeparator = null): float + public static function float(float|int|string|null $value, ?string $decimalSeparator = null): float { if ($value === null) { return 0.00; @@ -17,7 +16,7 @@ public static function float(float|int|string|null $value, ?DecimalSeparator $de return (float) NonNumericFilter::sanitize($value, $decimalSeparator); } - public static function int(float|int|string|null $value, ?DecimalSeparator $decimalSeparator = null): int + public static function int(float|int|string|null $value, ?string $decimalSeparator = null): int { if ($value === null) { return 0; @@ -26,7 +25,7 @@ public static function int(float|int|string|null $value, ?DecimalSeparator $deci return (int) NonNumericFilter::sanitize($value, $decimalSeparator); } - public static function guessDecimalSeparator(string $value): DecimalSeparator + public static function guessDecimalSeparator(string $value): string { return DecimalSeparatorGuesser::guess($value); } diff --git a/src/NumberValidator.php b/src/NumberValidator.php index 8ac6836..e49fb6b 100644 --- a/src/NumberValidator.php +++ b/src/NumberValidator.php @@ -2,8 +2,6 @@ namespace Num; -use Num\Enums\DecimalSeparator; - class NumberValidator { public static function canBeInteger(string $input): bool @@ -35,8 +33,8 @@ private static function hasExactlyOneSeparator(array $separatorCounts): bool private static function countSeparators(string $input): array { - $pointCount = substr_count($input, DecimalSeparator::POINT->value); - $commaCount = substr_count($input, DecimalSeparator::COMMA->value); + $pointCount = substr_count($input, '.'); + $commaCount = substr_count($input, ','); return ['pointCount' => $pointCount, 'commaCount' => $commaCount]; } @@ -44,8 +42,8 @@ private static function countSeparators(string $input): array private static function findSeparatorPosition(string $input, array $separatorCounts): int { return $separatorCounts['pointCount'] === 1 ? - strpos($input, DecimalSeparator::POINT->value) : - strpos($input, DecimalSeparator::COMMA->value); + strpos($input, '.') : + strpos($input, ','); } private static function hasValidAmountOfDigitsBeforeSeparator(int $separatorPosition): bool diff --git a/tests/DecimalSeparatorGuesserTest.php b/tests/DecimalSeparatorGuesserTest.php index 7f90387..7da677c 100644 --- a/tests/DecimalSeparatorGuesserTest.php +++ b/tests/DecimalSeparatorGuesserTest.php @@ -2,7 +2,6 @@ namespace Num\Tests; -use Num\Enums\DecimalSeparator; use PHPUnit\Framework\TestCase; use Num\DecimalSeparatorGuesser; @@ -11,22 +10,22 @@ class DecimalSeparatorGuesserTest extends TestCase public static function decimalSeparatorProvider() { return [ - ['1,234,567.89', DecimalSeparator::POINT], - ['1,234,567', DecimalSeparator::POINT], - ['1 234 567.89', DecimalSeparator::POINT], - ['123,4567.89', DecimalSeparator::POINT], - ['1\'234\'567.89', DecimalSeparator::POINT], - ['123', DecimalSeparator::POINT], - ['text', DecimalSeparator::POINT], - ['12.34567', DecimalSeparator::POINT], - ['12,345', DecimalSeparator::POINT], - ['.12', DecimalSeparator::POINT], - ['12,3456', DecimalSeparator::COMMA], - ['1.234.567,89', DecimalSeparator::COMMA], - ['1 234 567,89', DecimalSeparator::COMMA], - ['1\'234\'567,89', DecimalSeparator::COMMA], - ['12,34567', DecimalSeparator::COMMA], - [',12', DecimalSeparator::COMMA], + ['1,234,567.89', '.'], + ['1,234,567', '.'], + ['1 234 567.89', '.'], + ['123,4567.89', '.'], + ['1\'234\'567.89', '.'], + ['123', '.'], + ['text', '.'], + ['12.34567', '.'], + ['12,345', '.'], + ['.12', '.'], + ['12,3456', ','], + ['1.234.567,89', ','], + ['1 234 567,89', ','], + ['1\'234\'567,89', ','], + ['12,34567', ','], + [',12', ','], ]; } diff --git a/tests/NumTest.php b/tests/NumTest.php index edf2f7e..7ba9e60 100644 --- a/tests/NumTest.php +++ b/tests/NumTest.php @@ -3,7 +3,6 @@ namespace Num\Tests; use Num\Num; -use Num\Enums\DecimalSeparator; use PHPUnit\Framework\TestCase; class NumTest extends TestCase @@ -46,7 +45,7 @@ public static function conversionData(): array ['-123.45', -123.45, -123], [123, 123.0, 123], ], - 'separator' => DecimalSeparator::POINT, + 'separator' => '.', ], 'Comma Separator' => [ @@ -65,7 +64,7 @@ public static function conversionData(): array ['text', 0.0, 0], [',12', 0.12, 0], ], - 'separator' => DecimalSeparator::COMMA, + 'separator' => ',', ], ]; }