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::encodeUuid(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() : string |
53
|
|
|
{ |
54
|
|
|
return Uuid::uuid1(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public static function encodeUuid($uuid): string |
58
|
|
|
{ |
59
|
|
|
if (! Uuid::isValid($uuid)) { |
60
|
|
|
return $uuid; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
if (! $uuid instanceof Uuid) { |
64
|
|
|
$uuid = Uuid::fromString($uuid); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $uuid->getBytes(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public static function decodeUuid(string $binaryUuid): string |
71
|
|
|
{ |
72
|
|
|
if (Uuid::isValid($binaryUuid)) { |
73
|
|
|
return $binaryUuid; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return Uuid::fromBytes($binaryUuid)->toString(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function toArray() |
80
|
|
|
{ |
81
|
|
|
$uuidAttributes = $this->getUuidAttributes(); |
82
|
|
|
|
83
|
|
|
$array = parent::toArray(); |
84
|
|
|
|
85
|
|
|
if (! $this->exists || ! is_array($uuidAttributes)) { |
|
|
|
|
86
|
|
|
return $array; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
foreach ($uuidAttributes as $attributeKey) { |
90
|
|
|
if (! array_key_exists($attributeKey, $array)) { |
91
|
|
|
continue; |
92
|
|
|
} |
93
|
|
|
$uuidKey = $this->getRelatedBinaryKeyName($attributeKey); |
94
|
|
|
$array[$attributeKey] = $this->{$uuidKey}; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $array; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function getRelatedBinaryKeyName($attribute) |
101
|
|
|
{ |
102
|
|
|
$suffix = $this->getUuidSuffix(); |
103
|
|
|
|
104
|
|
|
return preg_match('/(?:uu)?id/i', $attribute) ? "{$attribute}{$suffix}" : $attribute; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function getAttribute($key) |
108
|
|
|
{ |
109
|
|
|
$uuidKey = $this->uuidTextAttribute($key); |
110
|
|
|
|
111
|
|
|
if ($uuidKey && $this->{$uuidKey} !== null) { |
|
|
|
|
112
|
|
|
return static::decodeUuid($this->{$uuidKey}); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return parent::getAttribute($key); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function setAttribute($key, $value) |
119
|
|
|
{ |
120
|
|
|
if ($this->uuidTextAttribute($key)) { |
|
|
|
|
121
|
|
|
$value = static::encodeUuid($value); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return parent::setAttribute($key, $value); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
protected function getUuidSuffix() |
128
|
|
|
{ |
129
|
|
|
return (property_exists($this, 'uuidSuffix')) ? $this->uuidSuffix : '_text'; |
|
|
|
|
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
protected function uuidTextAttribute($key) |
133
|
|
|
{ |
134
|
|
|
$uuidAttributes = $this->getUuidAttributes(); |
135
|
|
|
$suffix = $this->getUuidSuffix(); |
136
|
|
|
$offset = -(strlen($suffix)); |
137
|
|
|
|
138
|
|
|
if (substr($key, $offset) == $suffix && in_array(($uuidKey = substr($key, 0, $offset)), $uuidAttributes)) { |
139
|
|
|
return $uuidKey; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
return false; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public function getUuidAttributes() |
146
|
|
|
{ |
147
|
|
|
$uuidAttributes = []; |
148
|
|
|
|
149
|
|
|
if (property_exists($this, 'uuids') && is_array($this->uuids)) { |
150
|
|
|
$uuidAttributes = array_merge($uuidAttributes, $this->uuids); |
|
|
|
|
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
// non composite primary keys will return a string so casting required |
154
|
|
|
$key = (array) $this->getKeyName(); |
155
|
|
|
|
156
|
|
|
$uuidAttributes = array_unique(array_merge($uuidAttributes, $key)); |
157
|
|
|
|
158
|
|
|
return $uuidAttributes; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public function getUuidTextAttribute(): ?string |
162
|
|
|
{ |
163
|
|
|
$key = $this->getKeyName(); |
164
|
|
|
|
165
|
|
|
if (! $this->exists || is_array($key)) { |
166
|
|
|
return null; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
return static::decodeUuid($this->{$key}); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
public function setUuidTextAttribute(string $uuid) |
173
|
|
|
{ |
174
|
|
|
$key = $this->getKeyName(); |
175
|
|
|
|
176
|
|
|
if (is_array($key)) { |
177
|
|
|
return; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
$this->{$key} = static::encodeUuid($uuid); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
public function getQueueableId() |
184
|
|
|
{ |
185
|
|
|
return base64_encode($this->{$this->getKeyName()}); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
public function newQueryForRestoration($id) |
189
|
|
|
{ |
190
|
|
|
return $this->newQueryWithoutScopes()->whereKey(base64_decode($id)); |
|
|
|
|
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
public function getRouteKeyName() |
194
|
|
|
{ |
195
|
|
|
$suffix = $this->getUuidSuffix(); |
196
|
|
|
|
197
|
|
|
return "uuid{$suffix}"; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
public function getKeyName() |
201
|
|
|
{ |
202
|
|
|
return 'uuid'; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
public function getIncrementing() |
206
|
|
|
{ |
207
|
|
|
return false; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
public function resolveRouteBinding($value) |
211
|
|
|
{ |
212
|
|
|
return $this->withUuid($value)->first(); |
|
|
|
|
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|
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: