forked from michaeldyrynda/laravel-model-uuid
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeneratesUuid.php
More file actions
219 lines (190 loc) · 5.88 KB
/
GeneratesUuid.php
File metadata and controls
219 lines (190 loc) · 5.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<?php
namespace Dyrynda\Database\Support;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Ramsey\Uuid\Exception\InvalidUuidStringException;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;
/**
* UUID generation trait.
*
* Include this trait in any Eloquent model where you wish to automatically set
* a UUID field. When saving, if the UUID field has not been set, generate a
* new UUID value, which will be set on the model and saved by Eloquent.
*
* @copyright 2017 Michael Dyrynda
* @author Michael Dyrynda <michael@dyrynda.com.au>
* @license MIT
*
* @property string $uuidVersion
*
* @method static \Illuminate\Database\Eloquent\Builder whereUuid(string|string[] $uuid, ?string $uuidColumn = null)
* @method static \Illuminate\Database\Eloquent\Builder whereNotUuid(string|string[] $uuid, ?string $uuidColumn = null)
*/
trait GeneratesUuid
{
/**
* The UUID versions.
*
* @var array
*/
protected $uuidVersions = [
'uuid1',
'uuid4',
'uuid6',
'uuid7',
'ordered',
];
/**
* Boot the trait, adding a creating observer.
*
* When persisting a new model instance, we resolve the UUID field, then set
* a fresh UUID, taking into account if we need to cast to binary or not.
*/
public static function bootGeneratesUuid(): void
{
static::creating(function ($model) {
foreach ($model->uuidColumns() as $item) {
/* @var \Illuminate\Database\Eloquent\Model|static $model */
$uuid = $model->resolveUuid();
if (isset($model->attributes[$item]) && ! is_null($model->attributes[$item])) {
/* @var \Ramsey\Uuid\Uuid $uuid */
try {
$uuid = Uuid::fromString(strtolower($model->attributes[$item]));
} catch (InvalidUuidStringException $e) {
$uuid = Uuid::fromBytes($model->attributes[$item]);
}
}
$model->{$item} = strtolower($uuid->toString());
}
});
}
/**
* The name of the column that should be used for the UUID.
*/
public function uuidColumn(): string
{
return config('model-uuid.column_name', 'uuid');
}
/**
* The names of the columns that should be used for the UUID.
*/
public function uuidColumns(): array
{
return [$this->uuidColumn()];
}
/**
* Resolve a UUID instance for the configured version.
*/
public function resolveUuid(): UuidInterface
{
return call_user_func([Uuid::class, $this->resolveUuidVersion()]);
}
public function uuidVersion(): ?string
{
return null;
}
/**
* Resolve the UUID version to use when setting the UUID value. Default to uuid4.
*/
public function resolveUuidVersion(): string
{
$uuidVersion = $this->uuidVersion() ?? config('model-uuid.uuid_version', 'uuid4');
if ($uuidVersion === 'ordered') {
$uuidVersion = 'uuid6';
}
if (in_array($uuidVersion, $this->uuidVersions)) {
return $uuidVersion;
}
return 'uuid4';
}
/**
* Scope queries to find by UUID.
*
* @param Builder $query
* @param string|array $uuid
* @param string $uuidColumn
*/
public function scopeWhereUuid($query, $uuid, $uuidColumn = null): Builder
{
$uuidColumn = $this->getUuidColumn($uuidColumn);
$uuid = $this->prepareUuid($uuid, $uuidColumn);
return $query->whereIn(
$this->qualifyColumn($uuidColumn),
Arr::wrap($uuid)
);
}
/**
* Scope queries to find by UUID.
*
* @param Builder $query
* @param string|array $uuid
* @param string $uuidColumn
*/
public function scopeWhereNotUuid($query, $uuid, $uuidColumn = null): Builder
{
$uuidColumn = $this->getUuidColumn($uuidColumn);
$uuid = $this->prepareUuid($uuid, $uuidColumn);
return $query->whereNotIn(
$this->qualifyColumn($uuidColumn),
Arr::wrap($uuid)
);
}
/**
* Convert a single UUID or array of UUIDs to bytes.
*
* @param Arrayable|array|string $uuid
*/
protected function bytesFromUuid($uuid): array
{
if (is_array($uuid) || $uuid instanceof Arrayable) {
array_walk($uuid, function (&$uuid) {
$uuid = Uuid::fromString($uuid)->getBytes();
});
return $uuid;
}
return Arr::wrap(Uuid::fromString($uuid)->getBytes());
}
/**
* Normalises a single or array of input UUIDs, filtering any invalid UUIDs.
*
* @param Arrayable|array|string $uuid
*/
protected function normaliseUuids($uuid): array
{
$uuid = array_map(function ($uuid) {
return Str::lower($uuid);
}, Arr::wrap($uuid));
$uuid = array_filter($uuid, function ($uuid) {
return Uuid::isValid($uuid);
});
return $uuid;
}
/**
* Guess UUID column based on model configurations or given uuid column
*
* @param ?string $uuidColumn
*/
protected function getUuidColumn($uuidColumn = null): string
{
return ! is_null($uuidColumn) && in_array($uuidColumn, $this->uuidColumns())
? $uuidColumn
: $this->uuidColumns()[0];
}
/**
* Prepare UUID by normalization
*
* @param string|array $uuid
* @param string $uuidColumn
*/
protected function prepareUuid($uuid, $uuidColumn): array|string
{
$uuid = $this->normaliseUuids($uuid);
if ($this->isClassCastable($uuidColumn)) {
$uuid = $this->bytesFromUuid($uuid);
}
return $uuid;
}
}