47 lines
970 B
PHP
47 lines
970 B
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Shared\Helpers;
|
|
|
|
use Ramsey\Uuid\Rfc4122\FieldsInterface as Rfc4122FieldsInterface;
|
|
use Ramsey\Uuid\UuidInterface;
|
|
|
|
final readonly class UuidHelper
|
|
{
|
|
private function __construct()
|
|
{
|
|
// Static class
|
|
}
|
|
|
|
public static function isV4(UuidInterface $uuid): bool
|
|
{
|
|
return self::version($uuid) === 4;
|
|
}
|
|
|
|
public static function isV7(UuidInterface $uuid): bool
|
|
{
|
|
return self::version($uuid) === 7;
|
|
}
|
|
|
|
public static function version(UuidInterface $uuid): int
|
|
{
|
|
$fields = $uuid->getFields();
|
|
|
|
if ($fields instanceof Rfc4122FieldsInterface) {
|
|
$version = $fields->getVersion();
|
|
|
|
if ($version !== null) {
|
|
return $version;
|
|
}
|
|
}
|
|
|
|
$uuidString = $uuid->toString();
|
|
|
|
if (strlen($uuidString) >= 15) {
|
|
return (int) hexdec($uuidString[14]);
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
}
|