|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace NilPortugues\Uuid; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Class Uuid |
|
7
|
|
|
* @package NilPortugues\Uuid |
|
8
|
|
|
*/ |
|
9
|
|
|
class Uuid implements UuidInterface |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* When this namespace is specified, the name string is a fully-qualified domain name. |
|
13
|
|
|
* @link http://tools.ietf.org/html/rfc4122#appendix-C |
|
14
|
|
|
*/ |
|
15
|
|
|
const NAMESPACE_DNS = '6ba7b810-9dad-11d1-80b4-00c04fd430c8'; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* When this namespace is specified, the name string is a URL. |
|
19
|
|
|
* @link http://tools.ietf.org/html/rfc4122#appendix-C |
|
20
|
|
|
*/ |
|
21
|
|
|
const NAMESPACE_URL = '6ba7b811-9dad-11d1-80b4-00c04fd430c8'; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* When this namespace is specified, the name string is an ISO OID. |
|
25
|
|
|
* @link http://tools.ietf.org/html/rfc4122#appendix-C |
|
26
|
|
|
*/ |
|
27
|
|
|
const NAMESPACE_OID = '6ba7b812-9dad-11d1-80b4-00c04fd430c8'; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* When this namespace is specified, the name string is an X.500 DN in DER or a text output format. |
|
31
|
|
|
* @link http://tools.ietf.org/html/rfc4122#appendix-C |
|
32
|
|
|
*/ |
|
33
|
|
|
const NAMESPACE_X500 = '6ba7b814-9dad-11d1-80b4-00c04fd430c8'; |
|
34
|
|
|
|
|
35
|
|
|
const UUID4 = 'uuid4'; |
|
36
|
|
|
const UUID5 = 'uuid5'; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var string |
|
40
|
|
|
*/ |
|
41
|
|
|
private static $namespaceLess = self::UUID4; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var string |
|
45
|
|
|
*/ |
|
46
|
|
|
private static $usesNamespace = self::UUID5; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var array |
|
50
|
|
|
*/ |
|
51
|
|
|
private static $uuidMap = [ |
|
52
|
|
|
self::UUID4 => '\NilPortugues\Uuid\Versions\Uuid4::create', |
|
53
|
|
|
self::UUID5 => '\NilPortugues\Uuid\Versions\Uuid5::create', |
|
54
|
|
|
]; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Returns an Uuid identifier. |
|
58
|
|
|
* |
|
59
|
|
|
* @param string|null $namespace |
|
60
|
|
|
* @param string|null $name |
|
61
|
|
|
* |
|
62
|
|
|
* @return string |
|
63
|
|
|
*/ |
|
64
|
|
|
public static function create($namespace = null, $name = null) |
|
65
|
|
|
{ |
|
66
|
|
|
if (null === $namespace) { |
|
67
|
|
|
return self::createNamespacelessUuid(); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
return self::createNamespacedUuid($namespace, $name); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Returns an Uuid1, Uuid3 or Uuid5 identifier. |
|
75
|
|
|
* |
|
76
|
|
|
* @return string |
|
77
|
|
|
*/ |
|
78
|
|
|
private static function createNamespacelessUuid() |
|
79
|
|
|
{ |
|
80
|
|
|
return self::generate(self::$namespaceLess, []); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Call a Uuid generator and returns an Uuid identifier. |
|
85
|
|
|
* |
|
86
|
|
|
* @param string $uuidKey |
|
87
|
|
|
* @param array $arguments |
|
88
|
|
|
* |
|
89
|
|
|
* @return string |
|
90
|
|
|
*/ |
|
91
|
|
|
private static function generate($uuidKey, array $arguments) |
|
92
|
|
|
{ |
|
93
|
|
|
$classAndMethod = \explode('::', self::$uuidMap[$uuidKey]); |
|
94
|
|
|
|
|
95
|
|
|
return \call_user_func_array($classAndMethod, $arguments); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Returns an Uuid identifier using namespaces. |
|
100
|
|
|
* |
|
101
|
|
|
* @param $namespace |
|
102
|
|
|
* @param $name |
|
103
|
|
|
* |
|
104
|
|
|
* @return string |
|
105
|
|
|
*/ |
|
106
|
|
|
private static function createNamespacedUuid($namespace, $name) |
|
107
|
|
|
{ |
|
108
|
|
|
return self::generate(self::$usesNamespace, [$namespace, $name]); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|