1 | <?php |
||
7 | trait HasSlug |
||
8 | { |
||
9 | /** @var \Spatie\Sluggable\SlugOptions */ |
||
10 | protected $slugOptions; |
||
11 | |||
12 | /** |
||
13 | * Get the options for generating the slug. |
||
14 | */ |
||
15 | abstract public function getSlugOptions(): SlugOptions; |
||
16 | |||
17 | /** |
||
18 | * Boot the trait. |
||
19 | */ |
||
20 | protected static function bootHasSlug() |
||
30 | |||
31 | /** |
||
32 | * Handle adding slug on model creation. |
||
33 | */ |
||
34 | protected function generateSlugOnCreate() |
||
35 | { |
||
36 | $this->slugOptions = $this->getSlugOptions(); |
||
37 | |||
38 | if (! $this->slugOptions->generateSlugsOnCreate) { |
||
39 | return; |
||
40 | } |
||
41 | |||
42 | $this->addSlug(); |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | * Handle adding slug on model update. |
||
47 | */ |
||
48 | protected function generateSlugOnUpdate() |
||
49 | { |
||
50 | $this->slugOptions = $this->getSlugOptions(); |
||
51 | |||
52 | if (! $this->slugOptions->generateSlugsOnUpdate) { |
||
53 | return; |
||
54 | } |
||
55 | |||
56 | $this->addSlug(); |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * Handle setting slug on explicit request. |
||
61 | */ |
||
62 | public function generateSlug() |
||
68 | |||
69 | /** |
||
70 | * Add the slug to the model. |
||
71 | */ |
||
72 | protected function addSlug() |
||
86 | |||
87 | /** |
||
88 | * Generate a non unique slug for this record. |
||
89 | */ |
||
90 | protected function generateNonUniqueSlug(): string |
||
100 | |||
101 | /** |
||
102 | * Determine if a custom slug has been saved. |
||
103 | */ |
||
104 | protected function hasCustomSlugBeenUsed(): bool |
||
110 | |||
111 | /** |
||
112 | * Get the string that should be used as base for the slug. |
||
113 | */ |
||
114 | protected function getSlugSourceString(): string |
||
130 | |||
131 | /** |
||
132 | * Make the given slug unique. |
||
133 | */ |
||
134 | protected function makeSlugUnique(string $slug): string |
||
145 | |||
146 | /** |
||
147 | * Determine if a record exists with the given slug. |
||
148 | */ |
||
149 | protected function otherRecordExistsWithSlug(string $slug): bool |
||
156 | |||
157 | /** |
||
158 | * This function will throw an exception when any of the options is missing or invalid. |
||
159 | */ |
||
160 | protected function guardAgainstInvalidSlugOptions() |
||
174 | } |
||
175 |
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.