Skip to content

Commit

Permalink
UUID Generator function Added
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitexus committed Jan 23, 2024
1 parent 6d510ee commit e9f8cd6
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
4 changes: 2 additions & 2 deletions debian/changelog
@@ -1,4 +1,4 @@
php-vitexsoftware-ease-core (1.41.1) UNRELEASED; urgency=medium
php-vitexsoftware-ease-core (1.42.0) UNRELEASED; urgency=medium

[ Vítězslav Dvořák ]
* all unit tests pass
Expand Down Expand Up @@ -78,7 +78,7 @@ php-vitexsoftware-ease-core (1.41.1) UNRELEASED; urgency=medium
* phpstan
* Init Improvements

-- vitex <info@vitexsoftware.cz> Wed, 08 Nov 2023 14:13:37 +0100
-- vitex <info@vitexsoftware.cz> Tue, 23 Jan 2024 23:37:04 +0100

php-vitexsoftware-ease-core (0.12) UNRELEASED; urgency=medium

Expand Down
28 changes: 25 additions & 3 deletions src/Ease/Functions.php
Expand Up @@ -6,7 +6,7 @@
* @category Common
*
* @author Vitex <vitex@hippy.cz>
* @copyright 2019-2023 Vitex@hippy.cz (G)
* @copyright 2019-2024 Vitex@hippy.cz (G)
* @license https://opensource.org/licenses/MIT GPL-2
*
* PHP 7
Expand Down Expand Up @@ -61,7 +61,7 @@ public static function addUrlParams($url, array $addParams, $override = false)
if (array_key_exists('query', $urlParts)) {
parse_str($urlParts['query'], $queryUrlParams);
$urlParams = $override ? array_merge($queryUrlParams, $addParams) :
array_merge($addParams, $queryUrlParams);
array_merge($addParams, $queryUrlParams);
} else {
$urlParams = $addParams;
}
Expand Down Expand Up @@ -446,7 +446,7 @@ public static function formatBytes($dbytes)
*
* @return string|int|boolean|null
*/
public static function cfg(/*string*/ $constant, $cfg = null)
public static function cfg(/* string */ $constant, $cfg = null)
{
return \Ease\Shared::cfg($constant, $cfg);
}
Expand Down Expand Up @@ -507,4 +507,26 @@ public static function loadClassesInNamespace($namespace)
}
return $loaded;
}

/**
* Generates RFC 4122 compliant Version 4 UUIDs.
*
* @param string $data
*
* @return string
*/
public static function guidv4($data = null)
{
// Generate 16 bytes (128 bits) of random data or use the data passed into the function.
$data = $data ?? random_bytes(16);
assert(strlen($data) == 16);

// Set version to 0100
$data[6] = chr(ord($data[6]) & 0x0f | 0x40);
// Set bits 6-7 to 10
$data[8] = chr(ord($data[8]) & 0x3f | 0x80);

// Output the 36 character UUID.
return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
}
}
8 changes: 8 additions & 0 deletions tests/src/Ease/FunctionsTest.php
Expand Up @@ -303,4 +303,12 @@ public function testclassesInNamespace()
{
$this->assertIsArray(Functions::classesInNamespace('Ease'));
}

/**
* @covers Functions::guidv4
*/
public function testguidv4()
{
$this->assertEquals('74657374-7465-4374-b465-737474657374', \Ease\Functions::guidv4('testtesttesttest'));
}
}

0 comments on commit e9f8cd6

Please sign in to comment.