Completed
Push — master ( d0941c...c232a4 )
by Richard
14s
created

Uuid::generate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 1
rs 9.6666
c 0
b 0
f 0
1
<?php
2
/*
3
 You may not change or alter any portion of this comment or credits
4
 of supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit authors.
6
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
 */
11
12
namespace Xmf;
13
14
/**
15
 * Generate UUID
16
 *
17
 * @category  Xmf\Uuid
18
 * @package   Xmf
19
 * @author    Richard Griffith <[email protected]>
20
 * @copyright 2017 XOOPS Project (http://xoops.org)
21
 * @license   GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
22
 * @link      http://xoops.org
23
 */
24
class Uuid
25
{
26
    /**
27
     * generate - generate a version 4 (random) UUID
28
     *
29
     * Based on comment by pavel.volyntsev(at)gmail at http://php.net/manual/en/function.com-create-guid.php
30
     *
31
     * @return string UUID
32
     *
33
     * @throws \Exception on insufficient entropy
34
     */
35 1
    public static function generate()
36
    {
37 1
        $data = random_bytes(16);
38
39 1
        $data[6] = chr(ord($data[6]) & 0x0f | 0x40); // set version to 0100
40 1
        $data[8] = chr(ord($data[8]) & 0x3f | 0x80); // set bits 6-7 to 10
41
42 1
        return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
43
    }
44
}
45