1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bdelespierre\HasUuid; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Config; |
6
|
|
|
use Illuminate\Support\Str; |
7
|
|
|
use Webpatser\Uuid\Uuid; |
8
|
|
|
|
9
|
|
|
trait HasUuid |
10
|
|
|
{ |
11
|
|
|
public function __construct(array $attributes = []) |
12
|
|
|
{ |
13
|
|
|
parent::__construct($attributes); |
14
|
|
|
|
15
|
|
|
$this->setKeyType('string')->setIncrementing(false); |
|
|
|
|
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
protected static function bootHasUuid() |
19
|
|
|
{ |
20
|
|
|
static::creating(function ($model) { |
21
|
|
|
if (! $model->getAttribute($model->getKeyName())) { |
22
|
|
|
$model->setAttribute( |
23
|
|
|
$model->getKeyName(), |
24
|
|
|
$model->getUuid() |
25
|
|
|
); |
26
|
|
|
} |
27
|
|
|
}); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function resolveRouteBinding($value, $field = null) |
|
|
|
|
31
|
|
|
{ |
32
|
|
|
if (! is_string($value) || ! Uuid::validate($value)) { |
33
|
|
|
return null; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
return parent::resolveRouteBinding($value); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function getUuid(): string |
40
|
|
|
{ |
41
|
|
|
return (string) Uuid::generate( |
42
|
|
|
$this->getUuidVersion(), |
43
|
|
|
$this->getUuidNode(), |
44
|
|
|
$this->getUuidNamespace() |
45
|
|
|
); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function getUuidVersion(): int |
49
|
|
|
{ |
50
|
|
|
return $this->uuidVersion ?? 4; |
|
|
|
|
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function getUuidNode(): ?string |
54
|
|
|
{ |
55
|
|
|
if (! isset($this->uuidNode)) { |
56
|
|
|
return null; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $this->interpolate($this->uuidNode); |
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function getUuidNamespace(): ?string |
63
|
|
|
{ |
64
|
|
|
// when no explicit namespace is provided, |
65
|
|
|
// the namespace is calculated from the |
66
|
|
|
// app key. |
67
|
|
|
if (! isset($this->uuidNamespace)) { |
68
|
|
|
$key = Config::get('app.key'); |
69
|
|
|
|
70
|
|
|
$key = Str::startsWith($key, 'base64:') |
71
|
|
|
? self::hash($key) |
72
|
|
|
: self::strToHex($key); |
73
|
|
|
|
74
|
|
|
// Webpatser/UUID needs 16 bytes for namespace |
75
|
|
|
// which is *slightly* unsafe because Laravel |
76
|
|
|
// generates 32 bytes keys... |
77
|
|
|
return substr($key, 32); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $this->interpolate($this->uuidNamespace); |
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
protected function interpolate(string $value): ?string |
84
|
|
|
{ |
85
|
|
|
if (Str::startsWith($value, ':')) { |
86
|
|
|
$value = $this->getAttribute( |
|
|
|
|
87
|
|
|
Str::after($value, ':') |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $value; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
protected static function hash(string $key): string |
95
|
|
|
{ |
96
|
|
|
return bin2hex(base64_decode(Str::after($key, 'base64:'))); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
protected static function strToHex(string $string): string |
100
|
|
|
{ |
101
|
|
|
for ($i = 0, $hex = ''; $i < strlen($string); $i++) { |
102
|
|
|
$hex .= substr('0'.dechex(ord($string[$i])), -2); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return strtoupper($hex); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
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.