1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Clarkeash\Doorman\Drivers; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use Ramsey\Uuid\Uuid; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class UuidDriver |
10
|
|
|
* |
11
|
|
|
* @package \Clarkeash\Doorman\Drivers |
12
|
|
|
*/ |
13
|
|
|
class UuidDriver implements DriverInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Create an invite code. |
17
|
|
|
* |
18
|
|
|
* @return string |
19
|
|
|
*/ |
20
|
|
|
public function code(): string |
21
|
|
|
{ |
22
|
|
|
$version = config('doorman.uuid.version', 4); |
23
|
|
|
|
24
|
|
|
$method = 'createVersion' . $version . 'Uuid'; |
25
|
|
|
|
26
|
|
|
if (method_exists($this, $method)) { |
27
|
|
|
return $this->$method(); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
throw new InvalidArgumentException("Version [$version] not supported."); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
protected function createVersion1Uuid(): string |
34
|
|
|
{ |
35
|
|
|
return Uuid::uuid1()->toString(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
protected function createVersion3Uuid(): string |
39
|
|
|
{ |
40
|
|
|
throw_unless(config('doorman.uuid.namespace'), InvalidArgumentException::class, 'Namespace must be set for uuid version 3'); |
41
|
|
|
throw_unless(config('doorman.uuid.name'), InvalidArgumentException::class, 'Name must be set for uuid version 3'); |
42
|
|
|
|
43
|
|
|
return Uuid::uuid3(config('doorman.uuid.namespace'), config('doorman.uuid.namespace'))->toString(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
protected function createVersion4Uuid(): string |
47
|
|
|
{ |
48
|
|
|
return Uuid::uuid4()->toString(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
protected function createVersion5Uuid(): string |
52
|
|
|
{ |
53
|
|
|
throw_unless(config('doorman.uuid.namespace'), InvalidArgumentException::class, 'Namespace must be set for uuid version 5'); |
54
|
|
|
throw_unless(config('doorman.uuid.name'), InvalidArgumentException::class, 'Name must be set for uuid version 5'); |
55
|
|
|
|
56
|
|
|
return Uuid::uuid5(config('doorman.uuid.namespace'), config('doorman.uuid.namespace'))->toString(); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|