1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Unicity; |
5
|
|
|
|
6
|
|
|
use Unicity\Interfaces\GUID as GUIDInterface; |
7
|
|
|
use Unicity\Interfaces\GUIDFactory as GUIDFactoryInterface; |
8
|
|
|
|
9
|
|
|
class GUIDFactory implements GUIDFactoryInterface |
10
|
|
|
{ |
11
|
|
|
/** @var int */ |
12
|
|
|
private $nTimeBytes; |
13
|
|
|
|
14
|
|
|
/** @var int */ |
15
|
|
|
private $nRandomBytes; |
16
|
|
|
|
17
|
|
|
/** @var int */ |
18
|
|
|
private $expectedLength; |
19
|
|
|
|
20
|
|
|
public function __construct(int $nTimeBytes = 7, int $nRandomBytes = 9) |
21
|
|
|
{ |
22
|
|
|
$this->nTimeBytes = \max(4, \min(7, $nTimeBytes)); |
23
|
|
|
$this->nRandomBytes = \max(2, \min(9, $nRandomBytes)); |
24
|
|
|
$this->expectedLength = $this->nTimeBytes + $this->nRandomBytes; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @return GUID|GUIDInterface |
29
|
|
|
*/ |
30
|
|
|
public function create(): GUIDInterface |
31
|
|
|
{ |
32
|
|
|
return GUID::create($this->nTimeBytes, $this->nRandomBytes); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param string $binStr |
37
|
|
|
* @return GUID|GUIDInterface |
38
|
|
|
*/ |
39
|
|
|
public function fromBinaryString(string $binStr): GUIDInterface |
40
|
|
|
{ |
41
|
|
|
return GUID::fromBinaryString($binStr, $this->expectedLength); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param string $hexStr |
46
|
|
|
* @return GUID|GUIDInterface |
47
|
|
|
*/ |
48
|
|
|
public function fromHexString(string $hexStr): GUIDInterface |
49
|
|
|
{ |
50
|
|
|
return GUID::fromHexString($hexStr, $this->expectedLength); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param string $b64Str |
55
|
|
|
* @return GUID|GUIDInterface |
56
|
|
|
*/ |
57
|
|
|
public function fromBase64String(string $b64Str): GUIDInterface |
58
|
|
|
{ |
59
|
|
|
return GUID::fromBase64String($b64Str, $this->expectedLength); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param string $b64Str |
64
|
|
|
* @return GUID|GUIDInterface |
65
|
|
|
*/ |
66
|
|
|
public function fromBase64UrlString(string $b64Str): GUIDInterface |
67
|
|
|
{ |
68
|
|
|
return GUID::fromBase64UrlString($b64Str, $this->expectedLength); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|