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
|
|
|
$keyName = (array) $model->getKeyName(); |
15
|
|
|
$uuidKeys = $model->getUuidKeys(); |
16
|
|
|
|
17
|
|
|
foreach ($keyName as $key) { |
18
|
|
|
if (! in_array($key, $uuidKeys) || $model->{$key}) { |
19
|
|
|
continue; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
$model->{$key} = static::encodeUuid(static::generateUuid()); |
23
|
|
|
} |
24
|
|
|
}); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public static function scopeWithUuid(Builder $builder, $uuid, $field = null): Builder |
28
|
|
|
{ |
29
|
|
|
if ($field) { |
30
|
|
|
return static::scopeWithUuidRelation($builder, $uuid, $field); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
if ($uuid instanceof Uuid) { |
34
|
|
|
$uuid = (string) $uuid; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
$uuid = (array) $uuid; |
38
|
|
|
|
39
|
|
|
return $builder->whereKey(array_map(function (string $modelUuid) { |
40
|
|
|
return static::encodeUuid($modelUuid); |
41
|
|
|
}, $uuid)); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public static function scopeWithUuidRelation(Builder $builder, $uuid, string $field): Builder |
45
|
|
|
{ |
46
|
|
|
if ($uuid instanceof Uuid) { |
47
|
|
|
$uuid = (string) $uuid; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$uuid = (array) $uuid; |
51
|
|
|
|
52
|
|
|
return $builder->whereIn($field, array_map(function (string $modelUuid) { |
|
|
|
|
53
|
|
|
return static::encodeUuid($modelUuid); |
54
|
|
|
}, $uuid)); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public static function generateUuid() : string |
58
|
|
|
{ |
59
|
|
|
return Uuid::uuid1(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public static function encodeUuid($uuid): string |
63
|
|
|
{ |
64
|
|
|
if (! Uuid::isValid($uuid)) { |
65
|
|
|
return $uuid; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
if (! $uuid instanceof Uuid) { |
69
|
|
|
$uuid = Uuid::fromString($uuid); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $uuid->getBytes(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public static function decodeUuid(string $binaryUuid): string |
76
|
|
|
{ |
77
|
|
|
if (Uuid::isValid($binaryUuid)) { |
78
|
|
|
return $binaryUuid; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return Uuid::fromBytes($binaryUuid)->toString(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function isBinary($uuidStr) |
85
|
|
|
{ |
86
|
|
|
return preg_match('~[^\x20-\x7E\t\r\n]~', $uuidStr) > 0; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function getUuidKeys() |
90
|
|
|
{ |
91
|
|
|
return (property_exists($this, 'uuids') && is_array($this->uuids)) ? $this->uuids : []; |
|
|
|
|
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function toArray() |
95
|
|
|
{ |
96
|
|
|
$uuidAttributes = $this->getUuidAttributes(); |
97
|
|
|
|
98
|
|
|
$array = parent::toArray(); |
99
|
|
|
$pivotUuids = []; |
|
|
|
|
100
|
|
|
|
101
|
|
|
if (! $this->exists || ! is_array($uuidAttributes)) { |
|
|
|
|
102
|
|
|
return $array; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
foreach ($uuidAttributes as $attributeKey) { |
106
|
|
|
if (! array_key_exists($attributeKey, $array)) { |
107
|
|
|
continue; |
108
|
|
|
} |
109
|
|
|
$uuidKey = $this->getRelatedBinaryKeyName($attributeKey); |
110
|
|
|
$array[$attributeKey] = $this->{$uuidKey}; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
if (isset($array['pivot'])) { |
114
|
|
|
$pivotUuids = $array['pivot']; |
115
|
|
|
|
116
|
|
|
if (! is_array($pivotUuids)) { |
117
|
|
|
$pivotUuids = get_object_vars($pivotUuids); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
foreach ($pivotUuids as $key => $uuid) { |
121
|
|
|
if ($this->isBinary($uuid)) { |
122
|
|
|
$pivotUuids[$key] = $this->decodeUuid($uuid); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
$array['pivot'] = $pivotUuids; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return $array; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function getRelatedBinaryKeyName($attribute): string |
133
|
|
|
{ |
134
|
|
|
$suffix = $this->getUuidSuffix(); |
135
|
|
|
|
136
|
|
|
return preg_match('/(?:uu)?id/i', $attribute) ? "{$attribute}{$suffix}" : $attribute; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function getAttribute($key) |
140
|
|
|
{ |
141
|
|
|
$uuidKey = $this->uuidTextAttribute($key); |
142
|
|
|
|
143
|
|
|
if ($uuidKey && $this->{$uuidKey} !== null) { |
|
|
|
|
144
|
|
|
return static::decodeUuid($this->{$uuidKey}); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
return parent::getAttribute($key); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
public function setAttribute($key, $value) |
151
|
|
|
{ |
152
|
|
|
if ($this->uuidTextAttribute($key)) { |
|
|
|
|
153
|
|
|
$value = static::encodeUuid($value); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
return parent::setAttribute($key, $value); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
protected function getUuidSuffix() |
160
|
|
|
{ |
161
|
|
|
return (property_exists($this, 'uuidSuffix')) ? $this->uuidSuffix : '_text'; |
|
|
|
|
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
protected function uuidTextAttribute($key) |
165
|
|
|
{ |
166
|
|
|
$uuidAttributes = $this->getUuidAttributes(); |
167
|
|
|
$suffix = $this->getUuidSuffix(); |
168
|
|
|
$offset = -(strlen($suffix)); |
169
|
|
|
|
170
|
|
|
if (substr($key, $offset) == $suffix && in_array(($uuidKey = substr($key, 0, $offset)), $uuidAttributes)) { |
171
|
|
|
return $uuidKey; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
return false; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
public function getUuidAttributes() |
178
|
|
|
{ |
179
|
|
|
$uuidKeys = $this->getUuidKeys(); |
180
|
|
|
|
181
|
|
|
// non composite primary keys will return a string so casting required |
182
|
|
|
$key = (array) $this->getKeyName(); |
183
|
|
|
|
184
|
|
|
$uuidAttributes = array_unique(array_merge($uuidKeys, $key)); |
185
|
|
|
|
186
|
|
|
return $uuidAttributes; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
public function getUuidTextAttribute(): ?string |
190
|
|
|
{ |
191
|
|
|
$key = $this->getKeyName(); |
192
|
|
|
|
193
|
|
|
if (! $this->exists || is_array($key)) { |
194
|
|
|
return null; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
return static::decodeUuid($this->{$key}); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
public function setUuidTextAttribute(string $uuid) |
201
|
|
|
{ |
202
|
|
|
$key = $this->getKeyName(); |
203
|
|
|
|
204
|
|
|
if (is_array($key)) { |
205
|
|
|
throw new Exception('composite keys not allowed for attribute mutation'); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
$this->{$key} = static::encodeUuid($uuid); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
public function getQueueableId() |
212
|
|
|
{ |
213
|
|
|
return base64_encode($this->{$this->getKeyName()}); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
public function newQueryForRestoration($id) |
217
|
|
|
{ |
218
|
|
|
return $this->newQueryWithoutScopes()->whereKey(base64_decode($id)); |
|
|
|
|
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
public function newEloquentBuilder($query) |
222
|
|
|
{ |
223
|
|
|
return new Builder($query); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
public function getRouteKeyName() |
227
|
|
|
{ |
228
|
|
|
$suffix = $this->getUuidSuffix(); |
229
|
|
|
|
230
|
|
|
return "uuid{$suffix}"; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
public function getKeyName() |
234
|
|
|
{ |
235
|
|
|
return 'uuid'; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
public function getIncrementing() |
239
|
|
|
{ |
240
|
|
|
return false; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
public function resolveRouteBinding($value) |
244
|
|
|
{ |
245
|
|
|
return $this->withUuid($value)->first(); |
|
|
|
|
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
|
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: