Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
15 / 15 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
| Uuid | |
100.00% |
15 / 15 |
|
100.00% |
4 / 4 |
5 | |
100.00% |
1 / 1 |
| v4 | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| v4LinuxAvailable | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| v4Linux | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| v7 | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Pop PHP Framework (https://www.popphp.org/) |
| 4 | * |
| 5 | * @link https://github.com/popphp/popphp-framework |
| 6 | * @author Nick Sagona, III <dev@noladev.com> |
| 7 | * @copyright Copyright (c) 2009-2026 NOLA Interactive, LLC. |
| 8 | * @license https://www.popphp.org/license New BSD License |
| 9 | */ |
| 10 | |
| 11 | /** |
| 12 | * @namespace |
| 13 | */ |
| 14 | namespace Pop\Utils; |
| 15 | |
| 16 | /** |
| 17 | * Pop utils UUID helper class |
| 18 | * |
| 19 | * @category Pop |
| 20 | * @package Pop\Utils |
| 21 | * @author Nick Sagona, III <dev@noladev.com> |
| 22 | * @copyright Copyright (c) 2009-2026 NOLA Interactive, LLC. |
| 23 | * @license https://www.popphp.org/license New BSD License |
| 24 | * @version 2.3.0 |
| 25 | */ |
| 26 | class Uuid |
| 27 | { |
| 28 | |
| 29 | /** |
| 30 | * Generate a v4 UUID (random) |
| 31 | * |
| 32 | * @return string |
| 33 | */ |
| 34 | public static function v4(): string |
| 35 | { |
| 36 | $bytes = random_bytes(16); |
| 37 | $bytes[6] = chr(ord($bytes[6]) & 0x0f | 0x40); |
| 38 | $bytes[8] = chr(ord($bytes[8]) & 0x3f | 0x80); |
| 39 | |
| 40 | return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($bytes), 4)); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Check if the Linux random/uuid file is available |
| 45 | * |
| 46 | * @param string $file |
| 47 | * @return bool |
| 48 | */ |
| 49 | public static function v4LinuxAvailable(string $file = '/proc/sys/kernel/random/uuid'): bool |
| 50 | { |
| 51 | return file_exists($file); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Generate a v4 UUID (random) using the Linux random/uuid file |
| 56 | * |
| 57 | * @param string $file |
| 58 | * @return string |
| 59 | */ |
| 60 | public static function v4Linux(string $file = '/proc/sys/kernel/random/uuid'): string |
| 61 | { |
| 62 | if (!static::v4LinuxAvailable($file)) { |
| 63 | throw new Exception('Error: The Linux random UUID file is not available.'); |
| 64 | } |
| 65 | |
| 66 | return trim(file_get_contents($file)); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Generate a v7 UUID (time-based) |
| 71 | * |
| 72 | * @return string |
| 73 | */ |
| 74 | public static function v7(): string |
| 75 | { |
| 76 | $ms = (int)(microtime(true) * 1000); |
| 77 | $time = str_pad(dechex($ms), 12, '0', STR_PAD_LEFT); |
| 78 | $random = bin2hex(random_bytes(9)); |
| 79 | $uuid = substr($time, 0, 8) . '-' . substr($time, 8, 4) . '-7' . substr($random, 0, 3); |
| 80 | $variant = dechex(hexdec($random[3]) & 0x3 | 0x8); |
| 81 | $uuid .= '-' . $variant . substr($random, 4, 3) . '-' . substr($random, 7); |
| 82 | |
| 83 | return $uuid; |
| 84 | } |
| 85 | |
| 86 | } |