Skip to content

Commit

Permalink
Merge pull request #1 from CommandString/main-1
Browse files Browse the repository at this point in the history
code refactoring
  • Loading branch information
514sid authored Aug 11, 2023
2 parents 83e5fa4 + b3bea13 commit d81902a
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 47 deletions.
41 changes: 41 additions & 0 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: PHP Composer

on:
push:
branches:
- '**'
pull_request:
branches:
- '**'

permissions:
contents: read

jobs:
build:

runs-on: ubuntu-latest

strategy:
fail-fast: false
matrix:
php: [ '8.1', '8.2' ]

steps:
- uses: actions/checkout@v3

- name: Validate composer.json and composer.lock
run: composer validate --strict

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
coverage: xdebug
ini-values: xdebug.mode=develop,debug,coverage,

- name: Install dependencies
run: composer install --prefer-dist --no-progress

- name: PHPUnit
run: composer tests
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,8 @@
},
"require-dev": {
"phpunit/phpunit": "^10.1"
},
"scripts": {
"tests": "phpunit"
}
}
77 changes: 30 additions & 47 deletions src/Num.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,18 @@ public static function float(string $value, ?string $decimalSeparator = null): f
$cleanedValue = preg_replace('/[^0-9' . preg_quote($decimalSeparator) . ']/', '', $value);

if ($decimalSeparator === self::COMMA) {
$floatValue = (float) str_replace($decimalSeparator, self::POINT, $cleanedValue);
} else {
$floatValue = (float) $cleanedValue;
return (float)str_replace($decimalSeparator, self::POINT, $cleanedValue);
}

return $floatValue;
return (float)$cleanedValue;
}

/**
* Convert a string to an integer.
*/
public static function int(string $value, ?string $decimalSeparator = null): int
{
return (int) self::float($value, $decimalSeparator);
return (int)self::float($value, $decimalSeparator);
}

/**
Expand All @@ -43,75 +41,60 @@ public static function guessDecimalSeparator(string $value): string
$pointCount = substr_count($value, self::POINT);
$commaCount = substr_count($value, self::COMMA);

if ($pointCount == 0 && $commaCount == 0) {
$hasComma = $commaCount > 0;
$hasPoint = $pointCount > 0;

if (!$hasComma && !$hasPoint) {
return self::POINT;
}

$canBeInteger = self::canBeInteger($value);

if ($pointCount > 0 && $commaCount == 0) {
if(!$canBeInteger && $pointCount === 1) {
return self::POINT;
}
if($canBeInteger && $pointCount === 1) {
return self::COMMA;
}
return ($pointCount > 1) ? self::COMMA : self::POINT;
return (!$canBeInteger) ? self::POINT : self::COMMA;
}

if ($commaCount > 0 && $pointCount == 0) {
if(!$canBeInteger && $commaCount === 1) {
return self::COMMA;
}
if($canBeInteger && $commaCount === 1) {
return self::POINT;
}
return ($commaCount > 1) ? self::POINT : self::COMMA;
return (!$canBeInteger) ? self::COMMA : self::POINT;
}

if ($pointCount < $commaCount) {
return self::POINT;
} elseif ($commaCount < $pointCount) {
if ($commaCount < $pointCount) {
return self::COMMA;
}

$lastPointPosition = strrpos($value, self::POINT);
$lastCommaPosition = strrpos($value, self::COMMA);

if ($lastPointPosition !== false && $lastCommaPosition !== false) {
return ($lastPointPosition > $lastCommaPosition) ? self::POINT : self::COMMA;
} elseif ($lastCommaPosition !== false) {
return self::COMMA;
} else {
$lastPointPosition = strrpos($value, self::POINT);
$lastCommaPosition = strrpos($value, self::COMMA);

if ($lastPointPosition !== false && $lastCommaPosition !== false) {
return ($lastPointPosition > $lastCommaPosition) ? self::POINT : self::COMMA;
} elseif ($lastPointPosition !== false) {
return self::POINT;
} elseif ($lastCommaPosition !== false) {
return self::COMMA;
}
}

return self::POINT;
}

public static function canBeInteger($input) {
public static function canBeInteger($input): bool
{
$cleanedInput = preg_replace("/[^0-9,.]/", "", $input);

$dotCount = substr_count($cleanedInput, ".");
$commaCount = substr_count($cleanedInput, ",");

if ($dotCount + $commaCount === 1) {
$dotPosition = strpos($cleanedInput, ".");
$commaPosition = strpos($cleanedInput, ",");

if ($dotCount === 1) {
$digitsAfterSeparator = substr($cleanedInput, $dotPosition + 1);
} else {
$digitsAfterSeparator = substr($cleanedInput, $commaPosition + 1);
}


$pos = (($dotCount === 1) ? $dotPosition : $commaPosition) + 1;
$digitsAfterSeparator = substr($cleanedInput, $pos);

$digitCount = strlen($digitsAfterSeparator);

if ($digitCount !== 3) {
return false;
}
}

return true;
}
}
}

0 comments on commit d81902a

Please sign in to comment.