1 | <?php |
||
10 | trait HasSlug |
||
11 | { |
||
12 | /** |
||
13 | * The container for all the options necessary for this trait. |
||
14 | * Options can be viewed in the Neurony\Url\Options\SlugOptions file. |
||
15 | * |
||
16 | * @var SlugOptions |
||
17 | */ |
||
18 | protected $slugOptions; |
||
19 | |||
20 | /** |
||
21 | * Set the options for the HasSlug trait. |
||
22 | * |
||
23 | * @return SlugOptions |
||
24 | */ |
||
25 | abstract public function getSlugOptions(): SlugOptions; |
||
26 | |||
27 | /** |
||
28 | * Boot the trait. |
||
29 | * |
||
30 | * @return void |
||
31 | */ |
||
32 | public static function bootHasSlug(): void |
||
42 | |||
43 | /** |
||
44 | * Handle setting the slug on model creation. |
||
45 | * |
||
46 | * @return void |
||
47 | * @throws SlugException |
||
48 | */ |
||
49 | protected function generateSlugOnCreate(): void |
||
59 | |||
60 | /** |
||
61 | * Handle setting the slug on model update. |
||
62 | * |
||
63 | * @return void |
||
64 | * @throws SlugException |
||
65 | */ |
||
66 | protected function generateSlugOnUpdate(): void |
||
76 | |||
77 | /** |
||
78 | * The logic for actually setting the slug. |
||
79 | * |
||
80 | * @return void |
||
81 | * @throws SlugException |
||
82 | */ |
||
83 | public function generateSlug(): void |
||
97 | |||
98 | /** |
||
99 | * Generate a non unique slug for this record. |
||
100 | * |
||
101 | * @return string |
||
102 | */ |
||
103 | protected function generateNonUniqueSlug(): string |
||
117 | |||
118 | /** |
||
119 | * Make the given slug unique. |
||
120 | * |
||
121 | * @param string $slug |
||
122 | * @return string |
||
123 | */ |
||
124 | protected function makeSlugUnique(string $slug): string |
||
135 | |||
136 | /** |
||
137 | * Check if the $fromField slug has been supplied. |
||
138 | * If not, then skip the entire slug generation. |
||
139 | * |
||
140 | * @return bool |
||
141 | */ |
||
142 | protected function slugHasBeenSupplied(): bool |
||
156 | |||
157 | /** |
||
158 | * Determine if a custom slug has been saved. |
||
159 | * |
||
160 | * @return bool |
||
161 | */ |
||
162 | protected function slugHasChanged(): bool |
||
168 | |||
169 | /** |
||
170 | * Get the string that should be used as base for the slug. |
||
171 | * |
||
172 | * @return string |
||
173 | */ |
||
174 | protected function getSlugSource(): string |
||
184 | |||
185 | /** |
||
186 | * Check if the given slug already exists on another record. |
||
187 | * |
||
188 | * @param string $slug |
||
189 | * @return bool |
||
190 | */ |
||
191 | protected function slugAlreadyExists(string $slug): bool |
||
197 | |||
198 | /** |
||
199 | * Both instantiate the slug options as well as validate their contents. |
||
200 | * |
||
201 | * @return void |
||
202 | * @throws SlugException |
||
203 | */ |
||
204 | protected function initSlugOptions(): void |
||
212 | |||
213 | /** |
||
214 | * Check if mandatory slug options have been properly set from the model. |
||
215 | * Check if $fromField and $toField have been set. |
||
216 | * |
||
217 | * @return void |
||
218 | * @throws SlugException |
||
219 | */ |
||
220 | protected function validateSlugOptions(): void |
||
230 | } |
||
231 |
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.