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
|
|
|
/** |
46
|
|
|
* Resolve a UUID instance for the configured version. |
47
|
|
|
* |
48
|
|
|
* @return \Ramsey\Uuid\Uuid |
49
|
|
|
*/ |
50
|
6 |
|
public function resolveUuid() |
51
|
|
|
{ |
52
|
6 |
|
return call_user_func("\Ramsey\Uuid\Uuid::{$this->resolveUuidVersion()}"); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Resolve the UUID version to use when setting the UUID value. Default to uuid4. |
57
|
|
|
* |
58
|
|
|
* @return string |
59
|
|
|
*/ |
60
|
11 |
|
public function resolveUuidVersion() |
61
|
|
|
{ |
62
|
11 |
|
if (property_exists($this, 'uuidVersion') && in_array($this->uuidVersion, $this->uuidVersions)) { |
63
|
4 |
|
return $this->uuidVersion; |
64
|
|
|
} |
65
|
|
|
|
66
|
7 |
|
return 'uuid4'; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Scope queries to find by UUID. |
71
|
|
|
* |
72
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
73
|
|
|
* @param string $uuid |
74
|
|
|
* |
75
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
76
|
|
|
*/ |
77
|
2 |
|
public function scopeWhereUuid($query, $uuid) |
78
|
|
|
{ |
79
|
2 |
|
if ($this->hasCast('uuid')) { |
|
|
|
|
80
|
1 |
|
return $query->where( |
81
|
1 |
|
'uuid', |
82
|
1 |
|
call_user_func("\Ramsey\Uuid\Uuid::{$this->resolveUuidVersion()}")->fromString($uuid)->getBytes() |
83
|
|
|
); |
84
|
|
|
} |
85
|
|
|
|
86
|
1 |
|
return $query->where('uuid', $uuid); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Cast an attribute to a native PHP type. |
91
|
|
|
* |
92
|
|
|
* @param string $key |
93
|
|
|
* @param mixed $value |
94
|
|
|
* @return mixed |
95
|
|
|
*/ |
96
|
2 |
|
protected function castAttribute($key, $value) |
97
|
|
|
{ |
98
|
2 |
|
if ($key == 'uuid' && ! is_null($value)) { |
99
|
2 |
|
return call_user_func("\Ramsey\Uuid\Uuid::{$this->resolveUuidVersion()}")->fromBytes($value)->toString(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return parent::castAttribute($key, $value); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
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.