Skip to content

Commit

Permalink
Options to disable generate clug on create and update
Browse files Browse the repository at this point in the history
  • Loading branch information
bpocallaghan committed Jul 7, 2017
1 parent 8406027 commit b4d03c4
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# Changelog

## 1.1.0 - 2017-07-07
- add `generateSlugOnCreate(false)` and `generateSlugOnUpdate(false)`

## 1.0.0 - 2017-07-07
- Initial release
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ You can use multiple fields as the source of the slug `generateSlugFrom(['firstn

You can also pass a `callable` function to `generateSlugFrom()`.

Have a look [here for the options](https://github.com/bpocallaghan/sluggable/src/SlugOptions.php) and available config functions.
Have a look [here for the options](https://github.com/bpocallaghan/sluggable/blob/master/src/SlugOptions.php) and available config functions.

## Change log

Expand Down
15 changes: 15 additions & 0 deletions src/HasSlug.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ protected function generateSlugOnCreate()
{
$this->slugOptions = $this->getSlugOptions();

if (!$this->slugOptions->generateSlugOnCreate) {
return;
}

$this->createSlug();
}

Expand All @@ -48,6 +52,10 @@ protected function generateSlugOnUpdate()
{
$this->slugOptions = $this->getSlugOptions();

if (!$this->slugOptions->generateSlugOnUpdate) {
return;
}

// check updating
$slugNew = $this->generateNonUniqueSlug();
$slugCurrent = $this->attributes[$this->slugOptions->slugField];
Expand Down Expand Up @@ -102,6 +110,13 @@ protected function generateNonUniqueSlug()
*/
protected function getSlugSourceString()
{
// if callback given
if (is_callable($this->slugOptions->generateSlugFrom)) {
$slug = call_user_func($this->slugOptions->generateSlugFrom, $this);

return $slug;
}

// concatenate on the fields and implode on seperator
$slug = collect($this->slugOptions->generateSlugFrom)
->map(function ($fieldName = '') {
Expand Down
32 changes: 31 additions & 1 deletion src/SlugOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class SlugOptions
{
/** @var string|array */
/** @var string|array|callable */
public $generateSlugFrom = 'name';

/** @var string */
Expand All @@ -13,6 +13,12 @@ class SlugOptions
/** @var bool */
public $generateUniqueSlug = true;

/** @var bool */
public $generateSlugOnCreate = true;

/** @var bool */
public $generateSlugOnUpdate = true;

/** @var string */
public $slugSeparator = '-';

Expand Down Expand Up @@ -61,6 +67,30 @@ public function makeSlugUnique($unique = true)
return $this;
}

/**
* If we need to generate a slug on create
* @param bool $onCreate
* @return $this
*/
public function generateSlugOnCreate($onCreate = true)
{
$this->generateSlugOnCreate = $onCreate;

return $this;
}

/**
* If we need to generate a slug on update
* @param bool $onUpdate
* @return $this
*/
public function generateSlugOnUpdate($onUpdate = true)
{
$this->generateSlugOnUpdate = $onUpdate;

return $this;
}

/**
* Set the slug seperator
* @param string $separator
Expand Down

0 comments on commit b4d03c4

Please sign in to comment.