|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Nip\Records\Traits\HasUuid; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use Nip\Records\EventManager\Events\Event; |
|
7
|
|
|
use Nip\Records\AbstractModels\RecordManager; |
|
8
|
|
|
use Nip\Utility\Uuid; |
|
9
|
|
|
use Ramsey\Uuid\Exception\InvalidUuidStringException; |
|
10
|
|
|
use Ramsey\Uuid\UuidInterface; |
|
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
|
1 |
|
$this->bootUuidRecordColumn($manager, $record, $column); |
|
41
|
|
|
} |
|
42
|
3 |
|
}); |
|
43
|
2 |
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* The name of the column that should be used for the UUID. |
|
47
|
|
|
* |
|
48
|
|
|
* @return string |
|
49
|
|
|
*/ |
|
50
|
1 |
|
public function uuidColumn(): string |
|
51
|
|
|
{ |
|
52
|
1 |
|
return 'uuid'; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* The names of the columns that should be used for the UUID. |
|
57
|
|
|
* |
|
58
|
|
|
* @return array |
|
59
|
|
|
*/ |
|
60
|
1 |
|
public function uuidColumns(): array |
|
61
|
|
|
{ |
|
62
|
1 |
|
return [$this->uuidColumn()]; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Resolve the UUID version to use when setting the UUID value. Default to uuid4. |
|
67
|
|
|
* |
|
68
|
|
|
* @return string |
|
69
|
|
|
*/ |
|
70
|
1 |
|
public function resolveUuidVersion(): string |
|
71
|
|
|
{ |
|
72
|
1 |
|
if (property_exists($this, 'uuidVersion') && in_array($this->uuidVersion, $this->uuidVersions)) { |
|
73
|
|
|
return $this->uuidVersion; |
|
74
|
|
|
} |
|
75
|
1 |
|
if (method_exists($this, 'uuidVersion')) { |
|
76
|
|
|
$version = $this->uuidVersion(); |
|
77
|
|
|
if (in_array($version, $this->uuidVersions)) { |
|
78
|
|
|
return $version; |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
1 |
|
return 4; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @param $manager |
|
87
|
|
|
* @param $record |
|
88
|
|
|
* @param $column |
|
89
|
|
|
*/ |
|
90
|
1 |
|
protected function bootUuidRecordColumn($manager, $record, $column) |
|
91
|
|
|
{ |
|
92
|
|
|
/* @var \Ramsey\Uuid\Uuid $uuid */ |
|
93
|
1 |
|
$uuid = $manager->generateUuid(); |
|
94
|
|
|
|
|
95
|
1 |
|
if (isset($record->{$column}) && !is_null($record->{$column})) { |
|
96
|
|
|
try { |
|
97
|
1 |
|
$uuid = Uuid::fromString(strtolower($record->{$column})); |
|
98
|
|
|
} catch (InvalidUuidStringException $exception) { |
|
99
|
|
|
$uuid = Uuid::fromBytes($record->{$column}); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
1 |
|
$record->{$column} = strtolower($uuid->toString()); |
|
104
|
1 |
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @return UuidInterface |
|
108
|
|
|
* @throws Exception |
|
109
|
|
|
*/ |
|
110
|
1 |
|
protected function generateUuid() |
|
111
|
|
|
{ |
|
112
|
1 |
|
$version = $this->resolveUuidVersion(); |
|
113
|
|
|
|
|
114
|
|
|
switch ($version) { |
|
115
|
1 |
|
case 4: |
|
116
|
1 |
|
return Uuid::v4(); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
throw new Exception("UUID version [{$version}] not supported."); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|