2019-09-19 19:18:28 -04:00
|
|
|
<?php namespace BookStack\Entities;
|
|
|
|
|
2020-06-25 12:08:13 -04:00
|
|
|
use Illuminate\Support\Str;
|
|
|
|
|
2019-09-19 19:18:28 -04:00
|
|
|
class SlugGenerator
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate a fresh slug for the given entity.
|
|
|
|
* The slug will generated so it does not conflict within the same parent item.
|
|
|
|
*/
|
2020-11-21 18:20:54 -05:00
|
|
|
public function generate(Entity $entity): string
|
2019-09-19 19:18:28 -04:00
|
|
|
{
|
2020-11-21 18:20:54 -05:00
|
|
|
$slug = $this->formatNameAsSlug($entity->name);
|
|
|
|
while ($this->slugInUse($slug, $entity)) {
|
2019-09-19 19:18:28 -04:00
|
|
|
$slug .= '-' . substr(md5(rand(1, 500)), 0, 3);
|
|
|
|
}
|
|
|
|
return $slug;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Format a name as a url slug.
|
|
|
|
*/
|
|
|
|
protected function formatNameAsSlug(string $name): string
|
|
|
|
{
|
2020-06-25 12:08:13 -04:00
|
|
|
$slug = Str::slug($name);
|
2019-09-19 19:18:28 -04:00
|
|
|
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.
|
|
|
|
*/
|
2020-11-21 18:20:54 -05:00
|
|
|
protected function slugInUse(string $slug, Entity $entity): bool
|
2019-09-19 19:18:28 -04:00
|
|
|
{
|
2020-11-21 18:20:54 -05:00
|
|
|
$query = $entity->newQuery()->where('slug', '=', $slug);
|
2019-09-19 19:18:28 -04:00
|
|
|
|
2020-11-21 18:20:54 -05:00
|
|
|
if ($entity instanceof BookChild) {
|
|
|
|
$query->where('book_id', '=', $entity->book_id);
|
2019-09-19 19:18:28 -04:00
|
|
|
}
|
|
|
|
|
2020-11-21 18:20:54 -05:00
|
|
|
if ($entity->id) {
|
|
|
|
$query->where('id', '!=', $entity->id);
|
2019-09-19 19:18:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return $query->count() > 0;
|
|
|
|
}
|
2019-10-05 07:55:01 -04:00
|
|
|
}
|