1 | <?php |
||
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() |
||
43 | |||
44 | /** |
||
45 | * Resolve a UUID instance for the configured version. |
||
46 | * |
||
47 | * @return \Ramsey\Uuid\Uuid |
||
48 | */ |
||
49 | 6 | public function resolveUuid() |
|
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() |
|
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) |
|
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) |
|
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.