X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/a6633642232efd164d4708967ab59e498fbff896..refs/pull/5280/head:/app/Entities/Tools/SlugGenerator.php diff --git a/app/Entities/Tools/SlugGenerator.php b/app/Entities/Tools/SlugGenerator.php index 7075bc72c..5df300bb0 100644 --- a/app/Entities/Tools/SlugGenerator.php +++ b/app/Entities/Tools/SlugGenerator.php @@ -1,21 +1,25 @@ -formatNameAsSlug($entity->name); - while ($this->slugInUse($slug, $entity)) { - $slug .= '-' . substr(md5(rand(1, 500)), 0, 3); + $slug = $this->formatNameAsSlug($model->name); + while ($this->slugInUse($slug, $model)) { + $slug .= '-' . Str::random(3); } + return $slug; } @@ -25,26 +29,29 @@ class SlugGenerator protected function formatNameAsSlug(string $name): string { $slug = Str::slug($name); - if ($slug === "") { + if ($slug === '') { $slug = substr(md5(rand(1, 500)), 0, 5); } + return $slug; } /** * Check if a slug is already in-use for this * type of model within the same parent. + * + * @param Sluggable&Model $model */ - protected function slugInUse(string $slug, Entity $entity): bool + protected function slugInUse(string $slug, Sluggable $model): bool { - $query = $entity->newQuery()->where('slug', '=', $slug); + $query = $model->newQuery()->where('slug', '=', $slug); - if ($entity instanceof BookChild) { - $query->where('book_id', '=', $entity->book_id); + if ($model instanceof BookChild) { + $query->where('book_id', '=', $model->book_id); } - if ($entity->id) { - $query->where('id', '!=', $entity->id); + if ($model->id) { + $query->where('id', '!=', $model->id); } return $query->count() > 0;