Skip to content

Commit

Permalink
Add maximumLength(250) option
Browse files Browse the repository at this point in the history
  • Loading branch information
bpocallaghan committed Jul 7, 2017
1 parent b4d03c4 commit e3fa295
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## 1.2.0 - 2017-07-07
- add `maximumLength(255)`

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

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class YourEloquentModel extends Model

## Config

You do not have to add the method in you model (the above will be used as default). It is only needed when you want to change the default behavior.
You do not have to add the method in you model (the above will be used as default). It is only needed when you want to change the default behaviour.

By default it will generate a slug from the `name` and save to the `slug` column.

Expand Down
10 changes: 6 additions & 4 deletions src/HasSlug.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ protected function createSlug()
*/
protected function generateNonUniqueSlug()
{
return str_slug($this->getSlugSourceString(), $this->slugOptions->slugSeparator);
$slug = $this->getSlugSourceString();

return str_slug($slug, $this->slugOptions->slugSeparator);
}

/**
Expand All @@ -114,7 +116,7 @@ protected function getSlugSourceString()
if (is_callable($this->slugOptions->generateSlugFrom)) {
$slug = call_user_func($this->slugOptions->generateSlugFrom, $this);

return $slug;
return substr($slug, 0, $this->slugOptions->maximumLength);
}

// concatenate on the fields and implode on seperator
Expand All @@ -123,7 +125,7 @@ protected function getSlugSourceString()
return $this->$fieldName;
})->implode($this->slugOptions->slugSeparator);

return $slug;
return substr($slug, 0, $this->slugOptions->maximumLength);
}

/**
Expand Down Expand Up @@ -203,7 +205,7 @@ private function checkUpdatingSlug($slug)
}
}

// new unique slug needed
// unique slug needed
return false;
}
}
15 changes: 15 additions & 0 deletions src/SlugOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ class SlugOptions
/** @var bool */
public $generateUniqueSlug = true;

/** @var int */
public $maximumLength = 250;

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

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

/**
* Set the maximum length
* @param int $maximumLength
* @return $this
*/
public function maximumLength($maximumLength = 250)
{
$this->maximumLength = $maximumLength;

return $this;
}

/**
* If we need to generate a slug on create
* @param bool $onCreate
Expand Down

0 comments on commit e3fa295

Please sign in to comment.