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