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
|
|
|
|
12
|
|
|
protected static function bootHasBinaryUuid() |
13
|
|
|
{ |
14
|
|
|
static::creating(function (Model $model) { |
15
|
|
|
if ($model->{$model->getKeyName()}) { |
16
|
|
|
return; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
$model->{$model->getKeyName()} = static::encodeUuid(Uuid::uuid1()); |
20
|
|
|
}); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public static function scopeWithUuid(Builder $builder, $uuid, $field = null): Builder |
24
|
|
|
{ |
25
|
|
|
if ($field) { |
26
|
|
|
return static::scopeWithUuidRelation($builder, $uuid, $field); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
if ($uuid instanceof Uuid) { |
30
|
|
|
$uuid = (string) $uuid; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
$uuid = (array) $uuid; |
34
|
|
|
|
35
|
|
|
return $builder->whereKey(array_map(function (string $modelUuid) { |
36
|
|
|
return static::encodeUuid($modelUuid); |
37
|
|
|
}, $uuid)); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public static function scopeWithUuidRelation(Builder $builder, $uuid, string $field): Builder |
41
|
|
|
{ |
42
|
|
|
if ($uuid instanceof Uuid) { |
43
|
|
|
$uuid = (string) $uuid; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$uuid = (array) $uuid; |
47
|
|
|
|
48
|
|
|
return $builder->whereIn($field, array_map(function (string $modelUuid) { |
49
|
|
|
return static::encodeUuid($modelUuid); |
50
|
|
|
}, $uuid)); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public static function encodeUuid($uuid): string |
54
|
|
|
{ |
55
|
|
|
if (! Uuid::isValid($uuid)) { |
56
|
|
|
return $uuid; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
if (! $uuid instanceof Uuid) { |
60
|
|
|
$uuid = Uuid::fromString($uuid); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return $uuid->getBytes(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public static function decodeUuid(string $binaryUuid): string |
67
|
|
|
{ |
68
|
|
|
if (Uuid::isValid($binaryUuid)) { |
69
|
|
|
return $binaryUuid; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return Uuid::fromBytes($binaryUuid)->toString(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function toArray() |
76
|
|
|
{ |
77
|
|
|
|
78
|
|
|
$uuidAttributes = $this->getUuidAttributes(); |
79
|
|
|
|
80
|
|
|
$array = parent::toArray(); |
81
|
|
|
|
82
|
|
|
// if (! $this->exists) { |
|
|
|
|
83
|
|
|
// return $array; |
84
|
|
|
// } |
85
|
|
|
|
86
|
|
|
if (is_array($uuidAttributes)) { |
87
|
|
|
|
88
|
|
|
foreach ($uuidAttributes as $attributeKey) { |
89
|
|
|
|
90
|
|
|
if (array_key_exists($attributeKey, $array)) { |
91
|
|
|
$uuidKey = $this->getRelatedBinaryKeyName($attributeKey); |
92
|
|
|
$array[$attributeKey] = $this->{$uuidKey}; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $array; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function getRelatedBinaryKeyName($attrib) |
101
|
|
|
{ |
102
|
|
|
|
103
|
|
|
$suffix = $this->getUuidTextAttributeSuffix(); |
104
|
|
|
|
105
|
|
|
return "{$attrib}{$suffix}"; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function getAttribute($key) |
109
|
|
|
{ |
110
|
|
|
|
111
|
|
|
if (($uuidKey = $this->uuidTextAttribute($key)) && $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
|
|
|
|
121
|
|
|
if ($uuidKey = $this->uuidTextAttribute($key)) { |
|
|
|
|
122
|
|
|
$value = static::encodeUuid($value); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return parent::setAttribute($key, $value); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
protected function getUuidTextAttributeSuffix() |
129
|
|
|
{ |
130
|
|
|
return (property_exists($this, 'uuidTextAttribSuffix'))? $this->uuidTextAttribSuffix : '_text'; |
|
|
|
|
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
protected function uuidTextAttribute($key) |
134
|
|
|
{ |
135
|
|
|
|
136
|
|
|
$attribute_s = $this->getKeyName(); |
137
|
|
|
$suffix = $this->getUuidTextAttributeSuffix(); |
138
|
|
|
$offset = -(strlen($suffix)); |
139
|
|
|
|
140
|
|
|
if (is_string($attribute_s)) { |
141
|
|
|
$attribute_s = [ $attribute_s ]; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
if (substr($key, $offset) == $suffix && in_array(($uuidKey = substr($key, 0, $offset)), $attribute_s)) { |
145
|
|
|
return $uuidKey; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return false; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function getUuidAttributes() |
152
|
|
|
{ |
153
|
|
|
|
154
|
|
|
$uuidAttributes = []; |
155
|
|
|
|
156
|
|
|
if (property_exists($this, 'uuidAttributes')) { |
157
|
|
|
$uuidAttributes = $this->uuidAttributes === null ? [] : $this->uuidAttributes; |
|
|
|
|
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
$key = $this->getKeyName();// ! composite primary keys will return an array |
161
|
|
|
|
162
|
|
|
if (is_string($key)) { |
163
|
|
|
$uuidAttributes = array_merge($uuidAttributes, [ $key ]); |
164
|
|
|
} else if (is_array($key)) { |
165
|
|
|
$uuidAttributes = array_merge($uuidAttributes, $key); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
return $uuidAttributes; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
|
172
|
|
|
public function getUuidTextAttribute(): ?string |
173
|
|
|
{ |
174
|
|
|
if (! $this->exists) { |
|
|
|
|
175
|
|
|
return null; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
return static::decodeUuid($this->{$this->getKeyName()}); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
public function setUuidTextAttribute(string $uuid) |
182
|
|
|
{ |
183
|
|
|
$this->{$this->getKeyName()} = static::encodeUuid($uuid); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
public function getQueueableId() |
187
|
|
|
{ |
188
|
|
|
return base64_encode($this->{$this->getKeyName()}); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
public function newQueryForRestoration($id) |
192
|
|
|
{ |
193
|
|
|
return $this->newQueryWithoutScopes()->whereKey(base64_decode($id)); |
|
|
|
|
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
public function getRouteKeyName() |
197
|
|
|
{ |
198
|
|
|
$suffix = $this->getUuidTextAttributeSuffix(); |
199
|
|
|
|
200
|
|
|
return "uuid{$suffix}"; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
public function getKeyName() |
204
|
|
|
{ |
205
|
|
|
return 'uuid'; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
public function getIncrementing() |
209
|
|
|
{ |
210
|
|
|
return false; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
public function resolveRouteBinding($value) |
214
|
|
|
{ |
215
|
|
|
return $this->withUuid($value)->first(); |
|
|
|
|
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.