| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Spatie\BinaryUuid; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Ramsey\Uuid\Uuid; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Illuminate\Database\Eloquent\Model; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Illuminate\Database\Eloquent\Builder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | trait HasBinaryUuid | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     protected static function bootHasBinaryUuid() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |         static::creating(function (Model $model) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |             if ($model->{$model->getKeyName()}) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |                 return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |             $model->{$model->getKeyName()} = static::encodeUuid(Uuid::uuid1()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     public static function scopeWithUuid(Builder $builder, $uuid, $field = null): Builder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         if ($field) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |             return static::scopeWithUuidRelation($builder, $uuid, $field); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         if ($uuid instanceof Uuid) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |             $uuid = (string) $uuid; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         $uuid = (array) $uuid; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         return $builder->whereKey(array_map(function (string $modelUuid) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |             return static::encodeUuid($modelUuid); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         }, $uuid)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     public static function scopeWithUuidRelation(Builder $builder, $uuid, string $field): Builder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         if ($uuid instanceof Uuid) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |             $uuid = (string) $uuid; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         $uuid = (array) $uuid; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         return $builder->whereIn($field, array_map(function (string $modelUuid) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             return static::encodeUuid($modelUuid); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         }, $uuid)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     public static function encodeUuid($uuid): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         if (! Uuid::isValid($uuid)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |             return $uuid; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         if (! $uuid instanceof Uuid) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             $uuid = Uuid::fromString($uuid); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         return $uuid->getBytes(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     public static function decodeUuid(string $binaryUuid): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         if (Uuid::isValid($binaryUuid)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             return $binaryUuid; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         return Uuid::fromBytes($binaryUuid)->toString(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     public function getUuidTextAttribute(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         return static::decodeUuid($this->{$this->getKeyName()}); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     public function setUuidTextAttribute(string $uuid) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         $this->{$this->getKeyName()} = static::encodeUuid($uuid); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     public function getQueueableId() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         return base64_encode($this->{$this->getKeyName()}); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     public function newQueryForRestoration($id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         return $this->newQueryWithoutScopes()->whereKey(base64_decode($id)); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 93 |  |  |      | 
            
                                                        
            
                                    
            
            
                | 94 |  |  |     public function getRouteKeyName() | 
            
                                                        
            
                                    
            
            
                | 95 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 96 |  |  |         return 'uuid_text'; | 
            
                                                        
            
                                    
            
            
                | 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
Idableprovides a methodequalsIdthat 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.