1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\BinaryUuid; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Ramsey\Uuid\Uuid; |
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
8
|
|
|
|
9
|
|
|
trait HasBinaryUuid |
10
|
|
|
{ |
11
|
|
|
protected static function bootHasBinaryUuid() |
12
|
|
|
{ |
13
|
|
|
static::creating(function (Model $model) { |
14
|
|
|
$uuidAttributes = $model->getUuidAttributes(); |
15
|
|
|
|
16
|
|
|
foreach ($uuidAttributes as $key) { |
17
|
|
|
if ($model->{$key}) { |
18
|
|
|
continue; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
$model->{$key} = static::encodeUuid(static::generateUuid()); |
22
|
|
|
} |
23
|
|
|
}); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public static function scopeWithUuid(Builder $builder, $uuid, $field = null): Builder |
27
|
|
|
{ |
28
|
|
|
if ($field) { |
29
|
|
|
return static::scopeWithUuidRelation($builder, $uuid, $field); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
if ($uuid instanceof Uuid) { |
33
|
|
|
$uuid = (string) $uuid; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
$uuid = (array) $uuid; |
37
|
|
|
|
38
|
|
|
return $builder->whereKey(array_map(function (string $modelUuid) { |
39
|
|
|
return static::encodeUuid($modelUuid); |
40
|
|
|
}, $uuid)); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public static function scopeWithUuidRelation(Builder $builder, $uuid, string $field): Builder |
44
|
|
|
{ |
45
|
|
|
if ($uuid instanceof Uuid) { |
46
|
|
|
$uuid = (string) $uuid; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$uuid = (array) $uuid; |
50
|
|
|
|
51
|
|
|
return $builder->whereIn($field, array_map(function (string $modelUuid) { |
|
|
|
|
52
|
|
|
return static::encodeUuid($modelUuid); |
53
|
|
|
}, $uuid)); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public static function generateUuid() : string |
57
|
|
|
{ |
58
|
|
|
return Uuid::uuid1(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public static function encodeUuid($uuid): string |
62
|
|
|
{ |
63
|
|
|
if (! Uuid::isValid($uuid)) { |
64
|
|
|
return $uuid; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
if (! $uuid instanceof Uuid) { |
68
|
|
|
$uuid = Uuid::fromString($uuid); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $uuid->getBytes(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public static function decodeUuid(string $binaryUuid): string |
75
|
|
|
{ |
76
|
|
|
if (Uuid::isValid($binaryUuid)) { |
77
|
|
|
return $binaryUuid; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return Uuid::fromBytes($binaryUuid)->toString(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function getUuidKeys() |
84
|
|
|
{ |
85
|
|
|
return (property_exists($this, 'uuids') && is_array($this->uuids)) ? $this->uuids : []; |
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function toArray() |
89
|
|
|
{ |
90
|
|
|
$uuidAttributes = $this->getUuidAttributes(); |
91
|
|
|
|
92
|
|
|
$array = parent::toArray(); |
93
|
|
|
$pivotUuids = []; |
|
|
|
|
94
|
|
|
|
95
|
|
|
if (! $this->exists || ! is_array($uuidAttributes)) { |
|
|
|
|
96
|
|
|
return $array; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
foreach ($uuidAttributes as $attributeKey) { |
100
|
|
|
if (! array_key_exists($attributeKey, $array)) { |
101
|
|
|
continue; |
102
|
|
|
} |
103
|
|
|
$uuidKey = $this->getRelatedBinaryKeyName($attributeKey); |
104
|
|
|
$array[$attributeKey] = $this->{$uuidKey}; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
if (isset($array['pivot'])) { |
108
|
|
|
$pivotUuids = $array['pivot']; |
109
|
|
|
|
110
|
|
|
if (! is_array($pivotUuids)) { |
111
|
|
|
$pivotUuids = get_object_vars($pivotUuids); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
foreach ($pivotUuids as $key => $uuid) { |
115
|
|
|
$pivotUuids[$key] = $this->decodeUuid($uuid); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
$array['pivot'] = $pivotUuids; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return $array; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function getRelatedBinaryKeyName($attribute): string |
125
|
|
|
{ |
126
|
|
|
$suffix = $this->getUuidSuffix(); |
127
|
|
|
|
128
|
|
|
return preg_match('/(?:[a-zA-Z_]+)?id/i', $attribute) ? "{$attribute}{$suffix}" : $attribute; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function getAttribute($key) |
132
|
|
|
{ |
133
|
|
|
$uuidKey = $this->uuidTextAttribute($key); |
134
|
|
|
|
135
|
|
|
if ($uuidKey && $this->{$uuidKey} !== null) { |
|
|
|
|
136
|
|
|
return static::decodeUuid($this->{$uuidKey}); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return parent::getAttribute($key); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function setAttribute($key, $value) |
143
|
|
|
{ |
144
|
|
|
if ($this->uuidTextAttribute($key)) { |
|
|
|
|
145
|
|
|
$value = static::encodeUuid($value); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return parent::setAttribute($key, $value); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
protected function getUuidSuffix() |
152
|
|
|
{ |
153
|
|
|
return (property_exists($this, 'uuidSuffix')) ? $this->uuidSuffix : '_text'; |
|
|
|
|
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
protected function uuidTextAttribute($key) |
157
|
|
|
{ |
158
|
|
|
$uuidAttributes = $this->getUuidAttributes(); |
159
|
|
|
$suffix = $this->getUuidSuffix(); |
160
|
|
|
$offset = -(strlen($suffix)); |
161
|
|
|
|
162
|
|
|
if (substr($key, $offset) == $suffix && in_array(($uuidKey = substr($key, 0, $offset)), $uuidAttributes)) { |
163
|
|
|
return $uuidKey; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
return false; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
public function getUuidAttributes() |
170
|
|
|
{ |
171
|
|
|
$uuidKeys = $this->getUuidKeys(); |
172
|
|
|
|
173
|
|
|
// non composite primary keys will return a string so casting required |
174
|
|
|
$key = (array) $this->getKeyName(); |
175
|
|
|
|
176
|
|
|
$uuidAttributes = array_unique(array_merge($uuidKeys, $key)); |
177
|
|
|
|
178
|
|
|
return $uuidAttributes; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
public function getUuidTextAttribute(): ?string |
182
|
|
|
{ |
183
|
|
|
$key = $this->getKeyName(); |
184
|
|
|
|
185
|
|
|
if (! $this->exists || is_array($key)) { |
186
|
|
|
return null; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
return static::decodeUuid($this->{$key}); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
public function setUuidTextAttribute(string $uuid) |
193
|
|
|
{ |
194
|
|
|
$key = $this->getKeyName(); |
195
|
|
|
|
196
|
|
|
if (! is_string($key)) { |
197
|
|
|
throw new Exception('composite keys not allowed for attribute mutation'); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
$this->{$key} = static::encodeUuid($uuid); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
public function getQueueableId() |
204
|
|
|
{ |
205
|
|
|
return base64_encode($this->{$this->getKeyName()}); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
public function newQueryForRestoration($id) |
209
|
|
|
{ |
210
|
|
|
//return $this->newQueryWithoutScopes()->whereKey(base64_decode($id)); |
211
|
|
|
return is_array($id) |
212
|
|
|
? $this->newQueryWithoutScopes()->whereIn($this->getQualifiedKeyName(), $this->decodeIdArray($id)) |
|
|
|
|
213
|
|
|
: $this->newQueryWithoutScopes()->whereKey(base64_decode($id)); |
|
|
|
|
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
public function newEloquentBuilder($query) |
217
|
|
|
{ |
218
|
|
|
return new Builder($query); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
public function getRouteKeyName() |
222
|
|
|
{ |
223
|
|
|
$keyName = $this->getKeyName(); |
224
|
|
|
|
225
|
|
|
$routeKeyName = is_string($keyName) ? $this->strUuidSuffix($keyName) : array_map([&$this, 'strUuidSuffix'], $keyName); |
226
|
|
|
|
227
|
|
|
return $routeKeyName; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
public function getKeyName() |
231
|
|
|
{ |
232
|
|
|
return (! property_exists($this, 'primaryKey') || $this->primaryKey === 'id') ? 'uuid' : $this->primaryKey; |
|
|
|
|
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
public function getIncrementing() |
236
|
|
|
{ |
237
|
|
|
return false; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
public function resolveRouteBinding($value) |
241
|
|
|
{ |
242
|
|
|
$keyName = $this->getKeyName(); |
243
|
|
|
|
244
|
|
|
if (is_array($keyName)) { |
245
|
|
|
$parsed = explode(':', strval($value), count($keyName)); |
246
|
|
|
|
247
|
|
|
$value = array_combine($keyName, $parsed); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
return $this->withUuid($value)->first(); |
|
|
|
|
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
public function strUuidSuffix($str) |
254
|
|
|
{ |
255
|
|
|
$suffix = $this->getUuidSuffix(); |
256
|
|
|
|
257
|
|
|
return "{$str}{$suffix}"; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
private function decodeIdArray($ids) |
261
|
|
|
{ |
262
|
|
|
foreach ($ids as $key => $id) { |
263
|
|
|
$ids[$key] = base64_decode($id); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
return $ids; |
267
|
|
|
} |
268
|
|
|
} |
269
|
|
|
|
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: