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 find($id, $columns = ['*']) |
23
|
|
|
{ |
24
|
|
|
if (\is_array($id) || $id instanceof Arrayable) { |
|
|
|
|
25
|
|
|
return static::findMany($id, $columns); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
return static::withUuid($id)->first($columns); |
|
|
|
|
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public static function findMany($ids, $columns = ['*']) |
32
|
|
|
{ |
33
|
|
|
if (empty($ids)) { |
34
|
|
|
return static::newModelInstance()->newCollection(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
return static::withUuid($ids)->get($columns); |
|
|
|
|
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public static function scopeWithUuid(Builder $builder, $uuid, $field = null): Builder |
41
|
|
|
{ |
42
|
|
|
if ($field) { |
43
|
|
|
return static::scopeWithUuidRelation($builder, $uuid, $field); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
if ($uuid instanceof Uuid) { |
47
|
|
|
$uuid = (string) $uuid; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$uuid = (array) $uuid; |
51
|
|
|
|
52
|
|
|
return $builder->whereKey(array_map(function (string $modelUuid) { |
53
|
|
|
return static::encodeUuid($modelUuid); |
54
|
|
|
}, $uuid)); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public static function scopeWithUuidRelation(Builder $builder, $uuid, string $field): Builder |
58
|
|
|
{ |
59
|
|
|
if ($uuid instanceof Uuid) { |
60
|
|
|
$uuid = (string) $uuid; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$uuid = (array) $uuid; |
64
|
|
|
|
65
|
|
|
return $builder->whereIn($field, array_map(function (string $modelUuid) { |
66
|
|
|
return static::encodeUuid($modelUuid); |
67
|
|
|
}, $uuid)); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public static function encodeUuid($uuid): string |
71
|
|
|
{ |
72
|
|
|
if (! Uuid::isValid($uuid)) { |
73
|
|
|
return $uuid; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
if (! $uuid instanceof Uuid) { |
77
|
|
|
$uuid = Uuid::fromString($uuid); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $uuid->getBytes(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public static function decodeUuid(string $binaryUuid): string |
84
|
|
|
{ |
85
|
|
|
if (Uuid::isValid($binaryUuid)) { |
86
|
|
|
return $binaryUuid; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return Uuid::fromBytes($binaryUuid)->toString(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function toArray() |
93
|
|
|
{ |
94
|
|
|
$uuidAttributes = $this->getUuidAttributes(); |
95
|
|
|
|
96
|
|
|
$array = parent::toArray(); |
97
|
|
|
|
98
|
|
|
if (! $this->exists || ! is_array($uuidAttributes)) { |
|
|
|
|
99
|
|
|
return $array; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
foreach ($uuidAttributes as $attributeKey) { |
103
|
|
|
if (! array_key_exists($attributeKey, $array)) { |
104
|
|
|
continue; |
105
|
|
|
} |
106
|
|
|
$uuidKey = $this->getRelatedBinaryKeyName($attributeKey); |
107
|
|
|
$array[$attributeKey] = $this->{$uuidKey}; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $array; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function getRelatedBinaryKeyName($attribute) |
114
|
|
|
{ |
115
|
|
|
$suffix = $this->getUuidSuffix(); |
116
|
|
|
|
117
|
|
|
return preg_match('/(?:uu)?id/i', $attribute) ? "{$attribute}{$suffix}" : $attribute; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function getAttribute($key) |
121
|
|
|
{ |
122
|
|
|
$uuidKey = $this->uuidTextAttribute($key); |
123
|
|
|
|
124
|
|
|
if ($uuidKey && $this->{$uuidKey} !== null) { |
|
|
|
|
125
|
|
|
return static::decodeUuid($this->{$uuidKey}); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return parent::getAttribute($key); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function setAttribute($key, $value) |
132
|
|
|
{ |
133
|
|
|
if ($this->uuidTextAttribute($key)) { |
|
|
|
|
134
|
|
|
$value = static::encodeUuid($value); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return parent::setAttribute($key, $value); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
protected function getUuidSuffix() |
141
|
|
|
{ |
142
|
|
|
return (property_exists($this, 'uuidSuffix')) ? $this->uuidSuffix : '_text'; |
|
|
|
|
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
protected function uuidTextAttribute($key) |
146
|
|
|
{ |
147
|
|
|
$uuidAttributes = $this->getUuidAttributes(); |
148
|
|
|
$suffix = $this->getUuidSuffix(); |
149
|
|
|
$offset = -(strlen($suffix)); |
150
|
|
|
|
151
|
|
|
if (substr($key, $offset) == $suffix && in_array(($uuidKey = substr($key, 0, $offset)), $uuidAttributes)) { |
152
|
|
|
return $uuidKey; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
return false; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
public function getUuidAttributes() |
159
|
|
|
{ |
160
|
|
|
$uuidAttributes = []; |
161
|
|
|
|
162
|
|
|
if (property_exists($this, 'uuids') && is_array($this->uuids)) { |
163
|
|
|
$uuidAttributes = array_merge($uuidAttributes, $this->uuids); |
|
|
|
|
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
// non composite primary keys will return a string so casting required |
167
|
|
|
$key = (array) $this->getKeyName(); |
168
|
|
|
|
169
|
|
|
$uuidAttributes = array_unique(array_merge($uuidAttributes, $key)); |
170
|
|
|
|
171
|
|
|
return $uuidAttributes; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
public function getUuidTextAttribute(): ?string |
175
|
|
|
{ |
176
|
|
|
$key = $this->getKeyName(); |
177
|
|
|
|
178
|
|
|
if (! $this->exists || is_array($key)) { |
179
|
|
|
return null; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
return static::decodeUuid($this->{$key}); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
public function setUuidTextAttribute(string $uuid) |
186
|
|
|
{ |
187
|
|
|
$key = $this->getKeyName(); |
188
|
|
|
|
189
|
|
|
if (is_array($key)) { |
190
|
|
|
return; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
$this->{$key} = static::encodeUuid($uuid); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
public function getQueueableId() |
197
|
|
|
{ |
198
|
|
|
return base64_encode($this->{$this->getKeyName()}); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
public function newQueryForRestoration($id) |
202
|
|
|
{ |
203
|
|
|
return $this->newQueryWithoutScopes()->whereKey(base64_decode($id)); |
|
|
|
|
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
public function getRouteKeyName() |
207
|
|
|
{ |
208
|
|
|
$suffix = $this->getUuidSuffix(); |
209
|
|
|
|
210
|
|
|
return "uuid{$suffix}"; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
public function getKeyName() |
214
|
|
|
{ |
215
|
|
|
return 'uuid'; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
public function getIncrementing() |
219
|
|
|
{ |
220
|
|
|
return false; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
public function resolveRouteBinding($value) |
224
|
|
|
{ |
225
|
|
|
return $this->withUuid($value)->first(); |
|
|
|
|
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
|
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.