Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
nabeghe committed Oct 22, 2024
0 parents commit 7c92cc0
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea
.phpunit.result.cache
/vendor/
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) Hadi Akbarzadeh info@elatel.ir

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
71 changes: 71 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# Alphanum for PHP.

> Converting decimal numbers to alphanums, which include uppercase & lowercase letters as well as the underscore character, & vice versa.
**Notice:** It's useful for shortening links by converting post numbers to alphanumeric characters.

<hr>

## 🫡 Usage

### 🚀 Installation

You can install the package via composer:

```bash
composer require nabeghe/colory
```

<hr>

#### Example:

```php
<?php

require 'vendor/autoload.php';

use Nabeghe\Alphanum\Alphanum;

echo "[[ To Alphanum ]]\n";
echo '0 => '.Alphanum::generate(0)."\n"; // 0
echo '1 => '.Alphanum::generate(1)."\n"; // 1
echo '10 => '.Alphanum::generate(10)."\n"; // A
echo '11 => '.Alphanum::generate(11)."\n"; // B
echo '12 => '.Alphanum::generate(12)."\n"; // C
echo '13 => '.Alphanum::generate(13)."\n"; // D
echo '14 => '.Alphanum::generate(14)."\n"; // E
echo '100 => '.Alphanum::generate(100)."\n"; // 1f
echo '1000 => '.Alphanum::generate(1000)."\n"; // Gf
echo '1403 => '.Alphanum::generate(1403)."\n"; // NN
echo '2024 => '.Alphanum::generate(2024)."\n"; // Yj
echo '1000000 => '.Alphanum::generate(1000000)."\n"; // 4clf
echo '1000000000 => '.Alphanum::generate(1000000000)."\n"; // 1H9clf
echo 'PHP_INT_MAX => '.Alphanum::generate(PHP_INT_MAX)."\n"; // FFDZXWuKFV7
echo "\n";

echo "[[ To Decimal ]]\n";
echo '0 => '.Alphanum::toDecimal('0')."\n"; // 0
echo '1 => '.Alphanum::toDecimal('1')."\n"; // 1
echo 'A => '.Alphanum::toDecimal('A')."\n"; // 10
echo 'B => '.Alphanum::toDecimal('B')."\n"; // 11
echo 'C => '.Alphanum::toDecimal('C')."\n"; // 12
echo 'D => '.Alphanum::toDecimal('D')."\n"; // 13
echo 'E => '.Alphanum::toDecimal('E')."\n"; // 14
echo '1f => '.Alphanum::toDecimal('1f')."\n"; // 100
echo 'Gf => '.Alphanum::toDecimal('Gf')."\n"; // 1000
echo 'NN => '.Alphanum::toDecimal('NN')."\n"; // 1403
echo 'Yj => '.Alphanum::toDecimal('Yj')."\n"; // 2024
echo '4clf => '.Alphanum::toDecimal('4clf')."\n"; // 1000000
echo '1H9clf => '.Alphanum::toDecimal('1H9clf')."\n"; // 1000000000
echo 'FFDZXWuKFV7 => '.Alphanum::toDecimal('FFDZXWuKFV7')."\n"; // PHP_INT_MAX
echo "\n";
```

<hr>

## 📖 License

Copyright (c) 2024 Hadi Akbarzadeh

Licensed under the MIT license, see [LICENSE.md](LICENSE.md) for details.
28 changes: 28 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "nabeghe/alphanum",
"description": "Converting decimal numbers to alphanums, which include uppercase & lowercase letters as well as the underscore character, & vice versa.",
"keywords": [
"alphanum", "alphabetic", "string", "numeric", "numbers", "decimal", "sexagesimal", "hexadecimal",
"library", "helper", "support"
],
"type": "library",
"version": "0.1.0",
"homepage": "https://github.com/nabeghe/alphanum-php",
"license": "MIT",
"autoload": {
"psr-4": {
"Nabeghe\\Alphanum\\": "src/"
}
},
"authors": [
{
"name": "Hadi Akbarzadeh",
"email": "hadicoder@gmail.com",
"homepage": "https://elatel.ir",
"role": "Developer"
}
],
"require": {
"php": ">=7.0"
}
}
52 changes: 52 additions & 0 deletions src/Alphanum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php namespace Nabeghe\Alphanum;

/**
* A way to converting numbers between decimal & alphanumeric formats & vice versa.
*/
class Alphanum
{
public const CHARS = '0123456789ABCDEFGHIJKLMNPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_';

/**
* @param int $number Non-negative decimal number.
* @return ?string
*/
public static function generate($number)
{
if ($number < 0) {
return null;
}
if ($number === 0) {
return '0';
}

$result = '';
while ($number > 0) {
$result = static::CHARS[$number % 60].$result;
$number = intdiv($number, 60);
}

return $result;
}

/**
* @param string $alphanum sexagesimal number to convert
* @return integer Decimal representation of sexagesimal number
*/
public static function toDecimal(string $alphanum): ?int
{
$length = strlen($alphanum);
$result = 0;

for ($i = 0; $i < $length; $i++) {
$char = $alphanum[$i];
$value = strpos(static::CHARS, $char);
if ($value === false) {
return null;
}
$result = $result * 60 + $value;
}

return $result;
}
}

0 comments on commit 7c92cc0

Please sign in to comment.