Uuid   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
dl 0
loc 97
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A source() 0 3 1
A generate() 0 10 1
A setNamespace() 0 7 1
A random() 0 5 1
A separateUuid() 0 9 1
A __construct() 0 4 1
1
<?php declare(strict_types=1);
2
3
namespace Uuid;
4
5
/**
6
 * Class Uuid
7
 *
8
 * @package Uuid
9
 */
10
final class Uuid
11
{
12
    public const NAMESPACE_DNS  = '6ba7b810-9dad-11d1-80b4-00c04fd430c8';
13
    public const NAMESPACE_URL  = '6ba7b811-9dad-11d1-80b4-00c04fd430c8';
14
    public const NAMESPACE_OID  = '6ba7b812-9dad-11d1-80b4-00c04fd430c8';
15
    public const NAMESPACE_X500 = '6ba7b814-9dad-11d1-80b4-00c04fd430c8';
16
17
    private const BYTES_LENGTH  = 16;
18
    private const VERSION       = 0x50;
19
    private const CLEAR_VARIANT = 0x3F;
20
    private const RFC_VARIANT   = 0x80;
21
    private const CLEAR_VERSION = 0x0F;
22
23
    /**
24
     * @var string
25
     */
26
    private $source;
27
28
    /**
29
     * @var string
30
     */
31
    private $namespace;
32
33
    /**
34
     * Uuid constructor.
35
     *
36
     * @param string $source
37
     */
38
    private function __construct(string $source)
39
    {
40
        $this->source = $source;
41
        $this->setNamespace(self::NAMESPACE_DNS);
42
    }
43
44
    /**
45
     * @param string $namespace
46
     *
47
     * @return Uuid
48
     */
49
    public function setNamespace(string $namespace): Uuid
50
    {
51
        $namespace       = preg_replace('/^urn:uuid:/is', '', $namespace);
52
        $namespace       = preg_replace('/[^a-f0-9]/is', '', $namespace);
53
        $this->namespace = $namespace;
54
55
        return $this;
56
    }
57
58
    /**
59
     * @param string $source
60
     *
61
     * @return Uuid
62
     */
63
    public static function source(string $source): Uuid
64
    {
65
        return new self($source);
66
    }
67
68
    /**
69
     * @return Uuid
70
     */
71
    public static function random(): Uuid
72
    {
73
        $source = random_bytes(self::BYTES_LENGTH);
74
75
        return new self($source);
76
    }
77
78
    /**
79
     * @return string
80
     */
81
    public function generate(): string
82
    {
83
        $binary_namespace = pack('H*', $this->namespace);
84
        $hash             = sha1($binary_namespace . $this->source, true);
85
        $uuid             = substr($hash, 0, self::BYTES_LENGTH);
86
        $uuid[6]          = chr(ord($uuid[6]) & self::CLEAR_VERSION | self::VERSION);
87
        $uuid[8]          = chr(ord($uuid[8]) & self::CLEAR_VARIANT | self::RFC_VARIANT);
88
        $uuid             = $this->separateUuid($uuid);
89
90
        return $uuid;
91
    }
92
93
    /**
94
     * @param string $uuid
95
     *
96
     * @return string
97
     */
98
    private function separateUuid(string $uuid): string
99
    {
100
        $result = bin2hex(substr($uuid, 0, 4));
101
        $result .= '-' . bin2hex(substr($uuid, 4, 2));
102
        $result .= '-' . bin2hex(substr($uuid, 6, 2));
103
        $result .= '-' . bin2hex(substr($uuid, 8, 2));
104
        $result .= '-' . bin2hex(substr($uuid, 10, 6));
105
106
        return $result;
107
    }
108
}
109