Skip to content

Commit

Permalink
updated to add functions
Browse files Browse the repository at this point in the history
  • Loading branch information
abmmhasan committed Oct 27, 2023
1 parent a693911 commit d3733d7
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 74 deletions.
15 changes: 3 additions & 12 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ jobs:
runs-on: ${{ matrix.operating-system }}
strategy:
matrix:
operating-system: [ ubuntu-latest, windows-latest, macOS-latest ]
php-versions: [ '7.1','7.2','7.3','7.4','8.0', '8.1' ]
operating-system: [ ubuntu-latest ]
php-versions: [ '7.1','7.2','7.3','7.4','8.0', '8.1' , '8.2' , '8.3' ]
name: PHP ${{ matrix.php-versions }} Test on ${{ matrix.operating-system }}
steps:
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v4

- name: Install PHP
uses: shivammathur/setup-php@v2
Expand All @@ -22,15 +22,6 @@ jobs:
- name: Validate composer.json and composer.lock
run: composer validate --strict

- name: Cache Composer packages
id: composer-cache
uses: actions/cache@v2
with:
path: vendor
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-php-
- name: Install dependencies
run: composer install --prefer-dist --no-progress

Expand Down
64 changes: 30 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,69 +28,65 @@ composer require abmmhasan/uuid
### v1

```php
/**
* Get v1 UUID (Time based)
*/
\AbmmHasan\Uuid::v1();
// Get v1 UUID (Time based)
\AbmmHasan\UUID\GenerateUuid::v1();

/**
* Get generated node, for further use
*/
$node = \AbmmHasan\Uuid::getNode();
// Get generated node, for further use
$node = \AbmmHasan\UUID\GenerateUuid::getNode();

/**
* Pass your pre-generated node (for node specific UUID)
*/
\AbmmHasan\Uuid::v1($node);
// Pass your pre-generated node (for node specific UUID)
\AbmmHasan\UUID\GenerateUuid::v1($node);

// alternatively can also use
\AbmmHasan\UUID\uuid1();
```

### v3

```php
/**
* Get v3 UUID for 'TestString' (default X500 namespace)
*/
\AbmmHasan\Uuid::v3('TestString');
// Get v3 UUID for 'TestString' (default X500 namespace)
\AbmmHasan\UUID\GenerateUuid::v3('TestString');

/**
* Get v3 UUID for an URL & pre-defined namespace
* You can pass X500, URL, OID, DNS (check RFC4122 #Appendix C)
*/
\AbmmHasan\Uuid::v3('abmmhasan.github.io','url');
\AbmmHasan\UUID\GenerateUuid::v3('abmmhasan.github.io','url');

/**
* You can generate a random UUID & use as namespace as well
*/
\AbmmHasan\Uuid::v3('abmmhasan.github.io','fa1700dd-828c-4d1b-8e6d-a6104807da90');
// You can generate a random UUID & use as namespace as well
\AbmmHasan\UUID\GenerateUuid::v3('abmmhasan.github.io','fa1700dd-828c-4d1b-8e6d-a6104807da90');

// alternatively can also use
\AbmmHasan\UUID\uuid3();
```

### v4

```php
/**
* Get v4 UUID (completely random)
*/
\AbmmHasan\Uuid::v4();
// Get v4 UUID (completely random)
\AbmmHasan\UUID\GenerateUuid::v4();

// alternatively can also use
\AbmmHasan\UUID\uuid4();
```

### v5

```php
/**
* Get v5 UUID for 'TestString' (default X500 namespace)
*/
\AbmmHasan\Uuid::v5('TestString');
// Get v5 UUID for 'TestString' (default X500 namespace)
\AbmmHasan\UUID\GenerateUuid::v5('TestString');

/**
* Get v5 UUID for an URL & pre-defined namespace
* You can pass X500, URL, OID, DNS (check RFC4122 #Appendix C)
*/
\AbmmHasan\Uuid::v5('abmmhasan.github.io','url');
\AbmmHasan\UUID\GenerateUuid::v5('abmmhasan.github.io','url');

/**
* You can generate a random UUID & use as namespace as well
*/
\AbmmHasan\Uuid::v5('abmmhasan.github.io','fa1700dd-828c-4d1b-8e6d-a6104807da90');
// You can generate a random UUID & use as namespace as well
\AbmmHasan\UUID\GenerateUuid::v5('abmmhasan.github.io','fa1700dd-828c-4d1b-8e6d-a6104807da90');

// alternatively can also use
\AbmmHasan\UUID\uuid5();
```

### v2
Expand Down
7 changes: 5 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@
"authors": [
{
"name": "abmmhasan",
"email": "abmmhasan346@gmail.com"
"email": "abmmhasan@gmail.com"
}
],
"autoload": {
"files": [
"src/functions.php"
],
"psr-4": {
"AbmmHasan\\": "src/"
"AbmmHasan\\UUID\\": "src/"
}
},
"minimum-stability": "stable",
Expand Down
66 changes: 40 additions & 26 deletions src/Uuid.php → src/GenerateUuid.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

namespace AbmmHasan;
namespace AbmmHasan\UUID;

use Exception;

class Uuid
class GenerateUuid
{
private static $nsList = [
'dns' => 0,
Expand All @@ -16,20 +16,21 @@ class Uuid
private static $node;

/**
* Generate UUID v1 string
* Generates a version 1 UUID.
*
* @param string|null $node
* @param string|null $node The node identifier. Defaults to null.
* @return string
* @throws Exception
*/
public static function v1(string $node = null): string
{
$time = microtime(false);
$time = substr($time, 11) . substr($time, 2, 7);
$time = str_pad(dechex($time + 0x01b21dd213814000), 16, '0', STR_PAD_LEFT);
$time = str_pad(dechex((int)$time + 0x01b21dd213814000), 16, '0', STR_PAD_LEFT);
$clockSeq = random_int(0, 0x3fff);
$node = $node ?? self::getNode();
return sprintf('%08s-%04s-1%03s-%04x-%012s',
return sprintf(
'%08s-%04s-1%03s-%04x-%012s',
substr($time, -8),
substr($time, -12, 4),
substr($time, -15, 3),
Expand All @@ -39,10 +40,10 @@ public static function v1(string $node = null): string
}

/**
* Generate UUID v3 string
* Generates the v3 hash for a given string using the specified namespace.
*
* @param string $string
* @param string $namespace
* @param string $string The string to generate the hash for.
* @param string $namespace The namespace to use for the hash generation. Defaults to 'x500'.
* @return string
* @throws Exception
*/
Expand All @@ -57,9 +58,9 @@ public static function v3(string $string, string $namespace = 'x500'): string
}

/**
* Generate UUID v4 Random string
* Generates a version 4 UUID string.
*
* @return string
* @return string A version 4 UUID string.
* @throws Exception
*/
public static function v4(): string
Expand All @@ -69,10 +70,10 @@ public static function v4(): string
}

/**
* Generate UUID v5 string
* Generate a v5 UUID.
*
* @param string $string
* @param string $namespace
* @param string $string The string to generate the UUID from.
* @param string $namespace The namespace to use for the UUID generation. Default is 'x500'.
* @return string
* @throws Exception
*/
Expand All @@ -87,39 +88,50 @@ public static function v5(string $string, string $namespace = 'x500'): string
}

/**
* Get generated Node (for v1)
* Generate a unique hexadecimal node (for v1).
*
* @return string
* @return string The generated hexadecimal node.
* @throws Exception
*/
public static function getNode(): string
{
if (self::$node) {
return self::$node;
}
return self::$node = sprintf('%06x%06x',
return self::$node = sprintf(
'%06x%06x',
random_int(0, 0xffffff) | 0x010000,
random_int(0, 0xffffff)
);
}

/**
* @param int $version
* @param string $string
* @return string
* Generates a formatted string based on the given version and string.
*
* @param int $version The version number.
* @param string $string The input string.
* @return string The formatted string.
*/
private static function output(int $version, string $string): string
{
$string = str_split($string, 4);
return sprintf("%08s-%04s-{$version}%03s-%04x-%012s",
$string[0] . $string[1], $string[2],
return sprintf(
"%08s-%04s-$version%03s-%04x-%012s",
$string[0] . $string[1],
$string[2],
substr($string[3], 1, 3),
hexdec($string[4]) & 0x3fff | 0x8000,
$string[5] . $string[6] . $string[7]
);
}

private static function nsResolve($namespace)
/**
* Resolves the given namespace.
*
* @param string $namespace The namespace to be resolved.
* @return mixed The resolved namespace or false if it cannot be resolved.
*/
private static function nsResolve(string $namespace)
{
if (self::isValid($namespace)) {
return str_replace('-', '', $namespace);
Expand All @@ -132,11 +144,13 @@ private static function nsResolve($namespace)
}

/**
* @param $uuid
* Check if UUID is valid
*
* @param string $uuid The UUID to be checked
* @return bool
*/
private static function isValid($uuid): bool
public static function isValid(string $uuid): bool
{
return (bool)preg_match('{^[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$}Di', $uuid);
}
}
}
62 changes: 62 additions & 0 deletions src/functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace AbmmHasan\UUID;

use Exception;

if (!function_exists('AbmmHasan\UUID\uuid1')) {
/**
* Generates a version 1 UUID
*
* @param string|null $node The node value to use in the UUID.
* @return string The generated UUID.
* @throws Exception
*/
function uuid1(string $node = null): string
{
return GenerateUuid::v1($node);
}
}

if (!function_exists('AbmmHasan\UUID\uuid3')) {
/**
* Generate a Version 3 UUID.
*
* @param string $string The string to generate the UUID from.
* @param string $namespace The namespace to use for the UUID generation. Defaults to 'x500'.
* @return string The generated UUID.
* @throws Exception
*/
function uuid3(string $string, string $namespace = 'x500'): string
{
return GenerateUuid::v3($string, $namespace);
}
}

if (!function_exists('AbmmHasan\UUID\uuid4')) {
/**
* Generates a version 4 UUID.
*
* @return string The generated UUID.
* @throws Exception
*/
function uuid4(): string
{
return GenerateUuid::v4();
}
}

if (!function_exists('AbmmHasan\UUID\uuid5')) {
/**
* Generate a Version 5 UUID.
*
* @param string $string The string to generate the UUID from.
* @param string $namespace The namespace to use for the UUID generation. Defaults to 'x500'.
* @return string The generated UUID.
* @throws Exception
*/
function uuid5(string $string, string $namespace = 'x500'): string
{
return GenerateUuid::v5($string, $namespace);
}
}

0 comments on commit d3733d7

Please sign in to comment.