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