diff --git a/README.md b/README.md index 4739c5f..d3148b0 100644 --- a/README.md +++ b/README.md @@ -212,6 +212,20 @@ $model->slug = 'my-custom-url'; $model->save(); //slug is now "my-custom-url"; ``` +## Prevents slugs from being generated on some conditions + +If you don't want to create the slug when the model has a state, you can use the `skipGenerateWhen` function. + +```php +public function getSlugOptions() : SlugOptions +{ + return SlugOptions::create() + ->generateSlugsFrom('name') + ->saveSlugsTo('slug') + ->skipGenerateWhen(fn () => $this->state === 'draft'); +} +``` + ### Prevent slugs from being generated on creation If you don't want to create the slug when the model is initially created you can set use the `doNotGenerateSlugsOnCreate()` function. diff --git a/src/HasSlug.php b/src/HasSlug.php index 5586c7a..6368f5b 100644 --- a/src/HasSlug.php +++ b/src/HasSlug.php @@ -27,6 +27,10 @@ protected function generateSlugOnCreate(): void { $this->slugOptions = $this->getSlugOptions(); + if ($this->slugOptions->skipGenerate) { + return; + } + if (! $this->slugOptions->generateSlugsOnCreate) { return; } @@ -44,6 +48,10 @@ protected function generateSlugOnUpdate(): void { $this->slugOptions = $this->getSlugOptions(); + if ($this->slugOptions->skipGenerate) { + return; + } + if (! $this->slugOptions->generateSlugsOnUpdate) { return; } diff --git a/src/SlugOptions.php b/src/SlugOptions.php index a14d09c..a0b136d 100644 --- a/src/SlugOptions.php +++ b/src/SlugOptions.php @@ -16,6 +16,8 @@ class SlugOptions public int $maximumLength = 250; + public bool $skipGenerate = false; + public bool $generateSlugsOnCreate = true; public bool $generateSlugsOnUpdate = true; @@ -74,6 +76,13 @@ public function slugsShouldBeNoLongerThan(int $maximumLength): self return $this; } + public function skipGenerateWhen(callable $callable): self + { + $this->skipGenerate = $callable() === true; + + return $this; + } + public function doNotGenerateSlugsOnCreate(): self { $this->generateSlugsOnCreate = false; diff --git a/tests/HasSlugTest.php b/tests/HasSlugTest.php index 1244c34..d9b2377 100644 --- a/tests/HasSlugTest.php +++ b/tests/HasSlugTest.php @@ -156,8 +156,32 @@ public function getSlugOptions(): SlugOptions expect($model->url)->toEqual('this-is-an-other-1'); }); +it('has a method that prevents a slug being generated on condition', function () { + $model = new class () extends TestModel { + public function getSlugOptions(): SlugOptions + { + return parent::getSlugOptions() + ->skipGenerateWhen(fn () => $this->name === 'draft'); + } + }; + + $model->name = 'draft'; + $model->save(); + + expect($model->url)->toBeNull(); + + $model->other_field = 'Spatie'; + $model->save(); + + expect($model->url)->toBeNull(); + + $model->name = 'this is not a draft'; + $model->save(); + + expect($model->url)->toEqual('this-is-not-a-draft'); +}); -it('has an method that prevents a slug being generated on creation', function () { +it('has a method that prevents a slug being generated on creation', function () { $model = new class () extends TestModel { public function getSlugOptions(): SlugOptions { @@ -171,7 +195,7 @@ public function getSlugOptions(): SlugOptions expect($model->url)->toBeNull(); }); -it('has an method that prevents a slug being generated on update', function () { +it('has a method that prevents a slug being generated on update', function () { $model = new class () extends TestModel { public function getSlugOptions(): SlugOptions {