1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nip\Records\Traits\HasUuid; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use Exception; |
7
|
|
|
use Nip\Records\EventManager\Events\Event; |
8
|
|
|
use Nip\Records\RecordManager; |
9
|
|
|
use Nip\Utility\Uuid; |
10
|
|
|
use Ramsey\Uuid\Exception\InvalidUuidStringException; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Trait HasUuidRecordManagerTrait |
14
|
|
|
* @package Nip\Records\Traits\HasUuid |
15
|
|
|
*/ |
16
|
|
|
trait HasUuidRecordManagerTrait |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* The UUID versions. |
20
|
|
|
* |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
protected $uuidVersions = [ |
24
|
|
|
'1', |
25
|
|
|
'3', |
26
|
|
|
'4', |
27
|
|
|
'5', |
28
|
|
|
'6', |
29
|
|
|
'ordered', |
30
|
|
|
]; |
31
|
|
|
|
32
|
3 |
|
public function bootHasUuidRecordManagerTrait() |
33
|
|
|
{ |
34
|
|
|
static::creating(function (Event $event) { |
35
|
1 |
|
$record = $event->getRecord(); |
36
|
|
|
/** @var static|RecordManager $manager */ |
37
|
1 |
|
$manager = $event->getManager(); |
38
|
1 |
|
$columns = $manager->uuidColumns(); |
|
|
|
|
39
|
1 |
|
foreach ($columns as $column) { |
40
|
|
|
/* @var \Ramsey\Uuid\Uuid $uuid */ |
41
|
1 |
|
$uuid = $manager->generateUuid(); |
|
|
|
|
42
|
|
|
|
43
|
1 |
|
if (isset($record->{$column}) && !is_null($record->{$column})) { |
44
|
|
|
try { |
45
|
1 |
|
$uuid = $uuid->fromString(strtolower($record->{$column})); |
46
|
|
|
} catch (InvalidUuidStringException $e) { |
47
|
|
|
$uuid = $uuid->fromBytes($record->{$column}); |
|
|
|
|
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
1 |
|
$record->{$column} = strtolower($uuid->toString()); |
52
|
|
|
} |
53
|
3 |
|
}); |
54
|
2 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* The name of the column that should be used for the UUID. |
58
|
|
|
* |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
1 |
|
public function uuidColumn(): string |
62
|
|
|
{ |
63
|
1 |
|
return 'uuid'; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* The names of the columns that should be used for the UUID. |
68
|
|
|
* |
69
|
|
|
* @return array |
70
|
|
|
*/ |
71
|
1 |
|
public function uuidColumns(): array |
72
|
|
|
{ |
73
|
1 |
|
return [$this->uuidColumn()]; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Resolve the UUID version to use when setting the UUID value. Default to uuid4. |
78
|
|
|
* |
79
|
|
|
* @return string |
80
|
|
|
*/ |
81
|
1 |
|
public function resolveUuidVersion(): string |
82
|
|
|
{ |
83
|
1 |
|
if (property_exists($this, 'uuidVersion') && in_array($this->uuidVersion, $this->uuidVersions)) { |
84
|
|
|
return $this->uuidVersion; |
85
|
|
|
} |
86
|
1 |
|
if (method_exists($this, 'uuidVersion')) { |
87
|
|
|
$version = $this->uuidVersion(); |
88
|
|
|
if (in_array($version, $this->uuidVersions)) { |
89
|
|
|
return $version; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
1 |
|
return 4; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return \Ramsey\Uuid\UuidInterface |
98
|
|
|
* @throws Exception |
99
|
|
|
*/ |
100
|
1 |
|
protected function generateUuid() |
101
|
|
|
{ |
102
|
1 |
|
$version = $this->resolveUuidVersion(); |
103
|
|
|
|
104
|
|
|
switch ($version) { |
105
|
1 |
|
case 4: |
106
|
1 |
|
return Uuid::v4(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
throw new Exception("UUID version [{$version}] not supported."); |
110
|
|
|
} |
111
|
|
|
} |