Skip to content

Commit

Permalink
Merge pull request #2 from levizwannah/develop
Browse files Browse the repository at this point in the history
added is equal function
  • Loading branch information
levizwannah authored Oct 4, 2023
2 parents 1938076 + 35aaee2 commit 97bca2f
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 4 deletions.
40 changes: 37 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,44 @@
![Deployment tests](https://github.com/levizwannah/php-nums-to-letters/actions/workflows/php.yml/badge.svg) ![Development tests](https://github.com/levizwannah/php-nums-to-letters/actions/workflows/develop-php.yml/badge.svg?branch=develop)
# PHP Numbers To Letter
This Library converts numbers to Letters using A-Z.
0 = A, 25 = Z, using that, we can convert any number to a letter format. Even decimals.
This Library converts Integers to Letters using A-Z.
0 = A, 25 = Z, using that, we can convert any integer to a letter format.

## Installation
Install using composer
```bash
composer require levizwannah/php-nums-to-letters
```

## Methods
### toLetters($numbers)
Converts numbers to Letter format (Capital letters only);

```php
use LeviZwannah\PhpNumsToLetters\Converter;

$letters = Converter::toLetters(12345);
```

### toNumber($letters)
Converts letters to number format. The letters will be converted to uppercase first.
```php
use LeviZwannah\PhpNumsToLetters\Converter;

$number = Converter::toLetters('AABC');
```

### isEqual($val1, $val2)
This method tries to efficiently compares $val1 and $val2. Either of them can be string|integers. The comparison will still work.

```php
use LeviZwannah\PhpNumsToLetters\Converter;

Converter::isEqual(1, 'B'); // true
Converter::isEqual(1, 1); // true;
Converter::isEqual('B', 'B'); // true;
Converter::isEqual(0, 'B'); // false
Converter::isEqual('dw', 'DW'); // true
```

## Note
### Capital Letters only
Expand All @@ -19,7 +49,11 @@ When the number has a negative sign, the sign will be preserved in the conversio
-1 = -B and -C = -2.

### Decimal
No support for decimals
Only Integers are supported at the moment because 1/26 gives a very long decimal.

## Do the Math
With only 7 Letters, you can represent -8,031,810,176 to 8,031,810,176 numbers.

## Use cases
1. Beautiful Order Invoice Numbers from Order IDS or some random Number.
2. Representing IDs
32 changes: 31 additions & 1 deletion src/Converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ private static function __toLetters($number) {

public static function toLetters($number) {

if(!preg_match("/^[-\+]?[0-9]+$/", $number . '')) throw new InvalidNumberException($number);
if(static::isWord($number)) return $number;

if(!preg_match("/^[-\+]?[0-9]+$/", $number . '')) throw new InvalidNumberException($number);
$sign = $number < 0 ? '-' : '';
$number = abs($number);
$output = static::__toLetters($number);
Expand All @@ -50,13 +51,42 @@ public static function toLetters($number) {
}

public static function toNumber($letters) {

if(static::isNumber($letters)) return (int) $letters;

$letters = strtoupper($letters);
if(!preg_match("/^[-\+]?[A-Z]+$/", $letters)) throw new InvalidNumberException($letters);


$sign = $letters[0] === '-' ? -1 : 1;
$output = (int)static::__toNumber($letters);

return $sign * $output;
}


/**
* Checks if two values (string or numbers) are equal in this system.
* @param string|int $val1 letters or number
* @param string|int $val2 letters or number
* @note The type of the values will be checked, so don't worry about the types.
* @return bool
*/
public static function isEqual($val1, $val2) {
if(static::isNumber($val1) && static::isNumber($val2)) return $val1 == $val2;
if(static::isWord($val1) && static::isWord($val2)) return strcasecmp($val1, $val2) === 0;

return static::toNumber($val1) == static::toNumber($val2);
}

private static function isWord($val): bool
{
return (bool) preg_match("/^[-\+]?[A-Z]+$/", $val);
}

private static function isNumber($val): bool
{
return (bool) preg_match("/^[-\+]?[0-9]+$/", $val . "");
}
}
?>
10 changes: 10 additions & 0 deletions tests/Unit/ConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,14 @@ public function testSignedLetters(): void
{
$this->assertEquals(-1, Converter::toNumber("-B"));
}

public function testIsEqual(): void
{
$this->assertFalse(Converter::isEqual(0, 'B'));
$this->assertTrue(Converter::isEqual(1, 'B'));
$this->assertTrue(Converter::isEqual(1, 1));
$this->assertTrue(Converter::isEqual('ABC', 'ABC'));
$this->assertFalse(Converter::isEqual('DW', '-DW'));
$this->assertTrue(Converter::isEqual('dw', 'DW'));
}
}

0 comments on commit 97bca2f

Please sign in to comment.