|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\BinaryUuid; |
|
4
|
|
|
|
|
5
|
|
|
use Ramsey\Uuid\Uuid; |
|
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
7
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
8
|
|
|
|
|
9
|
|
|
trait HasBinaryUuid |
|
10
|
|
|
{ |
|
11
|
|
|
protected static function bootHasBinaryUuid() |
|
12
|
|
|
{ |
|
13
|
|
|
static::creating(function (Model $model) { |
|
14
|
|
|
if ($model->{$model->getKeyName()}) { |
|
15
|
|
|
return; |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
$model->{$model->getKeyName()} = static::generateUuid(); |
|
19
|
|
|
}); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
public static function scopeWithUuid(Builder $builder, $uuid, $field = null): Builder |
|
23
|
|
|
{ |
|
24
|
|
|
if ($field) { |
|
25
|
|
|
return static::scopeWithUuidRelation($builder, $uuid, $field); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
if ($uuid instanceof Uuid) { |
|
29
|
|
|
$uuid = (string) $uuid; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
$uuid = (array) $uuid; |
|
33
|
|
|
|
|
34
|
|
|
return $builder->whereKey(array_map(function (string $modelUuid) { |
|
35
|
|
|
return static::encodeUuid($modelUuid); |
|
36
|
|
|
}, $uuid)); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public static function scopeWithUuidRelation(Builder $builder, $uuid, string $field): Builder |
|
40
|
|
|
{ |
|
41
|
|
|
if ($uuid instanceof Uuid) { |
|
42
|
|
|
$uuid = (string) $uuid; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
$uuid = (array) $uuid; |
|
46
|
|
|
|
|
47
|
|
|
return $builder->whereIn($field, array_map(function (string $modelUuid) { |
|
48
|
|
|
return static::encodeUuid($modelUuid); |
|
49
|
|
|
}, $uuid)); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public static function generateUuid(bool $asText = false) : string |
|
53
|
|
|
{ |
|
54
|
|
|
$uuid = Uuid::uuid1(); |
|
55
|
|
|
return $asText ? (string) $uuid : static::encodeUuid($uuid); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public static function encodeUuid($uuid): string |
|
59
|
|
|
{ |
|
60
|
|
|
if (! Uuid::isValid($uuid)) { |
|
61
|
|
|
return $uuid; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
if (! $uuid instanceof Uuid) { |
|
65
|
|
|
$uuid = Uuid::fromString($uuid); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return $uuid->getBytes(); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public static function decodeUuid(string $binaryUuid): string |
|
72
|
|
|
{ |
|
73
|
|
|
if (Uuid::isValid($binaryUuid)) { |
|
74
|
|
|
return $binaryUuid; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
return Uuid::fromBytes($binaryUuid)->toString(); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function toArray() |
|
81
|
|
|
{ |
|
82
|
|
|
if (! $this->exists) { |
|
|
|
|
|
|
83
|
|
|
return parent::toArray(); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return array_merge(parent::toArray(), [$this->getKeyName() => $this->uuid_text]); |
|
|
|
|
|
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function getUuidTextAttribute(): ?string |
|
90
|
|
|
{ |
|
91
|
|
|
if (! $this->exists) { |
|
92
|
|
|
return null; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
return static::decodeUuid($this->{$this->getKeyName()}); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public function setUuidTextAttribute(string $uuid) |
|
99
|
|
|
{ |
|
100
|
|
|
$this->{$this->getKeyName()} = static::encodeUuid($uuid); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
public function getQueueableId() |
|
104
|
|
|
{ |
|
105
|
|
|
return base64_encode($this->{$this->getKeyName()}); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
public function newQueryForRestoration($id) |
|
109
|
|
|
{ |
|
110
|
|
|
return $this->newQueryWithoutScopes()->whereKey(base64_decode($id)); |
|
|
|
|
|
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
public function getRouteKeyName() |
|
114
|
|
|
{ |
|
115
|
|
|
return 'uuid_text'; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
public function getKeyName() |
|
119
|
|
|
{ |
|
120
|
|
|
return 'uuid'; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
public function getIncrementing() |
|
124
|
|
|
{ |
|
125
|
|
|
return false; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
public function resolveRouteBinding($value) |
|
129
|
|
|
{ |
|
130
|
|
|
return $this->withUuid($value)->first(); |
|
|
|
|
|
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: