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, ?string $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(string $uuid): string |
53
|
|
|
{ |
54
|
|
|
if (! Uuid::isValid($uuid)) { |
55
|
|
|
return $uuid; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$uuid = str_replace('-', '', (string) $uuid); |
59
|
|
|
|
60
|
|
|
return |
61
|
|
|
substr(hex2bin($uuid), 6, 2) |
62
|
|
|
.substr(hex2bin($uuid), 4, 2) |
63
|
|
|
.substr(hex2bin($uuid), 0, 4) |
64
|
|
|
.substr(hex2bin($uuid), 8, 8); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public static function decodeUuid(string $binaryUuid): string |
68
|
|
|
{ |
69
|
|
|
if (Uuid::isValid($binaryUuid)) { |
70
|
|
|
return $binaryUuid; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$uuidWithoutDashes = bin2hex( |
74
|
|
|
substr($binaryUuid, 4, 4) |
75
|
|
|
.substr($binaryUuid, 2, 2) |
76
|
|
|
.substr($binaryUuid, 0, 2) |
77
|
|
|
.substr($binaryUuid, 8, 8) |
78
|
|
|
); |
79
|
|
|
|
80
|
|
|
$uuidWithDashes = collect([8, 13, 18, 23])->reduce(function ($uuid, $position) { |
81
|
|
|
return substr_replace($uuid, '-', $position, 0); |
82
|
|
|
}, $uuidWithoutDashes); |
83
|
|
|
|
84
|
|
|
return $uuidWithDashes; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function getUuidTextAttribute(): string |
88
|
|
|
{ |
89
|
|
|
return static::decodeUuid($this->{$this->getKeyName()}); |
|
|
|
|
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function setUuidTextAttribute(string $uuid) |
93
|
|
|
{ |
94
|
|
|
$this->{$this->getKeyName()} = static::encodeUuid($uuid); |
|
|
|
|
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function getQueueableId() |
98
|
|
|
{ |
99
|
|
|
return base64_encode($this->{$this->getKeyName()}); |
|
|
|
|
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function newQueryForRestoration($id) |
103
|
|
|
{ |
104
|
|
|
return $this->newQueryWithoutScopes()->whereKey(base64_decode($id)); |
|
|
|
|
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: