1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dyrynda\Database\Support; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* UUID generation trait. |
7
|
|
|
* |
8
|
|
|
* Include this trait in any Eloquent model where you wish to automatically set |
9
|
|
|
* a UUID field. When saving, if the UUID field has not been set, generate a |
10
|
|
|
* new UUID value, which will be set on the model and saved by Eloquent. |
11
|
|
|
* |
12
|
|
|
* @copyright 2017 Michael Dyrynda |
13
|
|
|
* @author Michael Dyrynda <[email protected]> |
14
|
|
|
* @license MIT |
15
|
|
|
*/ |
16
|
|
|
trait GeneratesUuid |
17
|
|
|
{ |
18
|
|
|
protected $uuidVersions = [ |
19
|
|
|
'uuid1', |
20
|
|
|
'uuid3', |
21
|
|
|
'uuid4', |
22
|
|
|
'uuid5', |
23
|
|
|
]; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Boot the trait, adding a creating observer. |
27
|
|
|
* |
28
|
|
|
* When persisting a new model instance, we resolve the UUID field, then set |
29
|
|
|
* a fresh UUID, taking into account if we need to cast to binary or not. |
30
|
|
|
*/ |
31
|
|
|
public static function bootGeneratesUuid() |
32
|
|
|
{ |
33
|
6 |
|
static::creating(function ($model) { |
34
|
6 |
|
$uuid = $model->resolveUuid(); |
35
|
|
|
|
36
|
6 |
|
if (isset($model->attributes['uuid']) && ! is_null($model->attributes['uuid'])) { |
37
|
4 |
|
$uuid = $uuid->fromString(strtolower($model->attributes['uuid'])); |
38
|
|
|
} |
39
|
|
|
|
40
|
6 |
|
$model->attributes['uuid'] = $model->hasCast('uuid') ? $uuid->getBytes() : $uuid->toString(); |
41
|
6 |
|
}); |
42
|
2 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Resolve a UUID instance for the configured version. |
46
|
|
|
* |
47
|
|
|
* @return \Ramsey\Uuid\Uuid |
48
|
|
|
*/ |
49
|
6 |
|
public function resolveUuid() |
50
|
|
|
{ |
51
|
6 |
|
return call_user_func("\Ramsey\Uuid\Uuid::{$this->resolveUuidVersion()}"); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Resolve the UUID version to use when setting the UUID value. Default to uuid4. |
56
|
|
|
* |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
11 |
|
public function resolveUuidVersion() |
60
|
|
|
{ |
61
|
11 |
|
if (property_exists($this, 'uuidVersion') && in_array($this->uuidVersion, $this->uuidVersions)) { |
62
|
4 |
|
return $this->uuidVersion; |
63
|
|
|
} |
64
|
|
|
|
65
|
7 |
|
return 'uuid4'; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Scope queries to find by UUID. |
70
|
|
|
* |
71
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
72
|
|
|
* @param string $uuid |
73
|
|
|
* |
74
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
75
|
|
|
*/ |
76
|
2 |
|
public function scopeWhereUuid($query, $uuid) |
77
|
|
|
{ |
78
|
2 |
|
return $this->hasCast('uuid') |
|
|
|
|
79
|
1 |
|
? $query->where('uuid', $this->resolveUuid()->fromString($uuid)->getBytes()) |
80
|
2 |
|
: $query->where('uuid', $uuid); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Cast an attribute to a native PHP type. |
85
|
|
|
* |
86
|
|
|
* @param string $key |
87
|
|
|
* @param mixed $value |
88
|
|
|
* @return mixed |
89
|
|
|
*/ |
90
|
2 |
|
protected function castAttribute($key, $value) |
91
|
|
|
{ |
92
|
2 |
|
if ($key == 'uuid' && ! is_null($value)) { |
93
|
2 |
|
return $this->resolveUuid()->fromBytes($value)->toString(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return parent::castAttribute($key, $value); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.