Skip to content

Commit

Permalink
remove enums for php 8.0 support
Browse files Browse the repository at this point in the history
  • Loading branch information
514sid committed Sep 3, 2023
1 parent 866be4a commit 307781d
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 61 deletions.
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
25 changes: 12 additions & 13 deletions src/DecimalSeparatorGuesser.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,58 +3,57 @@
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
$canBeInteger = NumberValidator::canBeInteger($value);

// 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;
}

/**
* 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;
}
Expand Down
9 changes: 0 additions & 9 deletions src/Enums/DecimalSeparator.php

This file was deleted.

10 changes: 4 additions & 6 deletions src/NonNumericFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@

namespace Num;

use Num\Enums\DecimalSeparator;

class NonNumericFilter
{
public static function sanitize(string|int|float $value, ?DecimalSeparator $decimalSeparator = null): int|float
public static function sanitize(string|int|float $value, ?string $decimalSeparator = null): int|float
{
if (is_string($value)) {
$decimalSeparator = $decimalSeparator ?? DecimalSeparatorGuesser::guess($value);
$cleanedValue = preg_replace('/[^\d' . preg_quote($decimalSeparator->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;
Expand Down
7 changes: 3 additions & 4 deletions src/Num.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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);
}
Expand Down
10 changes: 4 additions & 6 deletions src/NumberValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Num;

use Num\Enums\DecimalSeparator;

class NumberValidator
{
public static function canBeInteger(string $input): bool
Expand Down Expand Up @@ -35,17 +33,17 @@ 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];
}

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
Expand Down
33 changes: 16 additions & 17 deletions tests/DecimalSeparatorGuesserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Num\Tests;

use Num\Enums\DecimalSeparator;
use PHPUnit\Framework\TestCase;
use Num\DecimalSeparatorGuesser;

Expand All @@ -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', ','],
];
}

Expand Down
5 changes: 2 additions & 3 deletions tests/NumTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Num\Tests;

use Num\Num;
use Num\Enums\DecimalSeparator;
use PHPUnit\Framework\TestCase;

class NumTest extends TestCase
Expand Down Expand Up @@ -46,7 +45,7 @@ public static function conversionData(): array
['-123.45', -123.45, -123],
[123, 123.0, 123],
],
'separator' => DecimalSeparator::POINT,
'separator' => '.',
],

'Comma Separator' => [
Expand All @@ -65,7 +64,7 @@ public static function conversionData(): array
['text', 0.0, 0],
[',12', 0.12, 0],
],
'separator' => DecimalSeparator::COMMA,
'separator' => ',',
],
];
}
Expand Down

0 comments on commit 307781d

Please sign in to comment.