1 | <?php |
||
8 | trait HasSlug |
||
9 | { |
||
10 | /** @var \Spatie\Sluggable\SlugOptions */ |
||
11 | protected $slugOptions; |
||
12 | |||
13 | /** |
||
14 | * Get the options for generating the slug. |
||
15 | */ |
||
16 | abstract public function getSlugOptions(): SlugOptions; |
||
17 | |||
18 | /** |
||
19 | * Boot the trait. |
||
20 | */ |
||
21 | protected static function bootHasSlug() |
||
31 | |||
32 | /** |
||
33 | * Handle adding slug on model creation. |
||
34 | */ |
||
35 | protected function generateSlugOnCreate() |
||
45 | |||
46 | /** |
||
47 | * Handle adding slug on model update. |
||
48 | */ |
||
49 | protected function generateSlugOnUpdate() |
||
59 | |||
60 | /** |
||
61 | * Handle setting slug on explicit request. |
||
62 | */ |
||
63 | public function generateSlug() |
||
69 | |||
70 | /** |
||
71 | * Add the slug to the model. |
||
72 | */ |
||
73 | protected function addSlug() |
||
87 | |||
88 | /** |
||
89 | * Generate a non unique slug for this record. |
||
90 | */ |
||
91 | protected function generateNonUniqueSlug(): string |
||
101 | |||
102 | /** |
||
103 | * Determine if a custom slug has been saved. |
||
104 | */ |
||
105 | protected function hasCustomSlugBeenUsed(): bool |
||
111 | |||
112 | /** |
||
113 | * Get the string that should be used as base for the slug. |
||
114 | */ |
||
115 | protected function getSlugSourceString(): string |
||
131 | |||
132 | protected function relationships(string $fieldNames) |
||
145 | |||
146 | /** |
||
147 | * Make the given slug unique. |
||
148 | */ |
||
149 | protected function makeSlugUnique(string $slug): string |
||
160 | |||
161 | /** |
||
162 | * Determine if a record exists with the given slug. |
||
163 | */ |
||
164 | protected function otherRecordExistsWithSlug(string $slug): bool |
||
177 | |||
178 | /** |
||
179 | * This function will throw an exception when any of the options is missing or invalid. |
||
180 | */ |
||
181 | protected function guardAgainstInvalidSlugOptions() |
||
195 | } |
||
196 |
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.