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 isBinary($uuidStr) |
84
|
|
|
{ |
85
|
|
|
return preg_match('~[^\x20-\x7E\t\r\n]~', $uuidStr) > 0; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function getUuidKeys() |
89
|
|
|
{ |
90
|
|
|
return (property_exists($this, 'uuids') && is_array($this->uuids)) ? $this->uuids : []; |
|
|
|
|
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function toArray() |
94
|
|
|
{ |
95
|
|
|
$uuidAttributes = $this->getUuidAttributes(); |
96
|
|
|
|
97
|
|
|
$array = parent::toArray(); |
98
|
|
|
$pivotUuids = []; |
|
|
|
|
99
|
|
|
|
100
|
|
|
if (! $this->exists || ! is_array($uuidAttributes)) { |
|
|
|
|
101
|
|
|
return $array; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
foreach ($uuidAttributes as $attributeKey) { |
105
|
|
|
if (! array_key_exists($attributeKey, $array)) { |
106
|
|
|
continue; |
107
|
|
|
} |
108
|
|
|
$uuidKey = $this->getRelatedBinaryKeyName($attributeKey); |
109
|
|
|
$array[$attributeKey] = $this->{$uuidKey}; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
if (isset($array['pivot'])) { |
113
|
|
|
$pivotUuids = $array['pivot']; |
114
|
|
|
|
115
|
|
|
if (! is_array($pivotUuids)) { |
116
|
|
|
$pivotUuids = get_object_vars($pivotUuids); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
foreach ($pivotUuids as $key => $uuid) { |
120
|
|
|
if ($this->isBinary($uuid)) { |
121
|
|
|
$pivotUuids[$key] = $this->decodeUuid($uuid); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
$array['pivot'] = $pivotUuids; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return $array; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function getRelatedBinaryKeyName($attribute): string |
132
|
|
|
{ |
133
|
|
|
$suffix = $this->getUuidSuffix(); |
134
|
|
|
|
135
|
|
|
return preg_match('/(?:uu)?id/i', $attribute) ? "{$attribute}{$suffix}" : $attribute; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function getAttribute($key) |
139
|
|
|
{ |
140
|
|
|
$uuidKey = $this->uuidTextAttribute($key); |
141
|
|
|
|
142
|
|
|
if ($uuidKey && $this->{$uuidKey} !== null) { |
|
|
|
|
143
|
|
|
return static::decodeUuid($this->{$uuidKey}); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
return parent::getAttribute($key); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
public function setAttribute($key, $value) |
150
|
|
|
{ |
151
|
|
|
if (($uuidKey = $this->uuidTextAttribute($key)) !== false) { |
152
|
|
|
$value = static::encodeUuid($uuidKey); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
return parent::setAttribute($key, $value); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
protected function getUuidSuffix() |
159
|
|
|
{ |
160
|
|
|
return (property_exists($this, 'uuidSuffix')) ? $this->uuidSuffix : '_text'; |
|
|
|
|
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
protected function uuidTextAttribute($key) |
164
|
|
|
{ |
165
|
|
|
$uuidAttributes = $this->getUuidAttributes(); |
166
|
|
|
$suffix = $this->getUuidSuffix(); |
167
|
|
|
$offset = -(strlen($suffix)); |
168
|
|
|
|
169
|
|
|
if (substr($key, $offset) == $suffix && in_array(($uuidKey = substr($key, 0, $offset)), $uuidAttributes)) { |
170
|
|
|
return $uuidKey; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
return false; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
public function getUuidAttributes() |
177
|
|
|
{ |
178
|
|
|
$uuidKeys = $this->getUuidKeys(); |
179
|
|
|
|
180
|
|
|
// non composite primary keys will return a string so casting required |
181
|
|
|
$key = (array) $this->getKeyName(); |
182
|
|
|
|
183
|
|
|
$uuidAttributes = array_unique(array_merge($uuidKeys, $key)); |
184
|
|
|
|
185
|
|
|
return $uuidAttributes; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
public function getUuidTextAttribute(): ?string |
189
|
|
|
{ |
190
|
|
|
$key = $this->getKeyName(); |
191
|
|
|
|
192
|
|
|
if (! $this->exists || is_array($key)) { |
193
|
|
|
return null; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
return static::decodeUuid($this->{$key}); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
public function setUuidTextAttribute(string $uuid) |
200
|
|
|
{ |
201
|
|
|
$key = $this->getKeyName(); |
202
|
|
|
|
203
|
|
|
if (! is_string($key)) { |
204
|
|
|
throw new Exception('composite keys not allowed for attribute mutation'); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
$this->{$key} = static::encodeUuid($uuid); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
public function getQueueableId() |
211
|
|
|
{ |
212
|
|
|
return base64_encode($this->{$this->getKeyName()}); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
public function newQueryForRestoration($id) |
216
|
|
|
{ |
217
|
|
|
return $this->newQueryWithoutScopes()->whereKey(base64_decode($id)); |
|
|
|
|
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
public function newEloquentBuilder($query) |
221
|
|
|
{ |
222
|
|
|
return new Builder($query); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
public function getRouteKeyName() |
226
|
|
|
{ |
227
|
|
|
$suffix = $this->getUuidSuffix(); |
228
|
|
|
|
229
|
|
|
return "uuid{$suffix}"; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
public function getKeyName() |
233
|
|
|
{ |
234
|
|
|
return (property_exists($this, 'primaryKey') && $this->primaryKey === 'id') ? 'uuid' : $this->primaryKey; |
|
|
|
|
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
public function getIncrementing() |
238
|
|
|
{ |
239
|
|
|
return false; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
public function resolveRouteBinding($value) |
243
|
|
|
{ |
244
|
|
|
return $this->withUuid($value)->first(); |
|
|
|
|
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
private function decodeIdArray($ids) |
|
|
|
|
248
|
|
|
{ |
249
|
|
|
foreach ($ids as $key => $id) { |
250
|
|
|
$ids[$key] = base64_decode($id); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
return $ids; |
254
|
|
|
} |
255
|
|
|
} |
256
|
|
|
|
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: