Skip to content

Commit

Permalink
adding support for persian slugs
Browse files Browse the repository at this point in the history
  • Loading branch information
AliBayat authored Sep 2, 2021
1 parent 3525102 commit da836ba
Showing 1 changed file with 46 additions and 2 deletions.
48 changes: 46 additions & 2 deletions src/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,53 @@ public static function findById(int $id): self
return static::findOrFail($id);
}


/**
* Spatie sluggable options
*/
public function getSlugOptions(): SlugOptions
{
return SlugOptions::create()->generateSlugsFrom('name')->saveSlugsTo('slug');
return SlugOptions::create()
->generateSlugsFrom('name')
->saveSlugsTo('slug');
}

/**
* Generate a non unique slug for the record.
*/
protected function generateNonUniqueSlug(): string
{
if ($this->hasCustomSlugBeenUsed()) {
$slugField = $this->slugOptions->slugField;

return $this->$slugField;
}

return $this->persianSlug(
$this->getSlugSourceString(),
$this->slugOptions->slugSeparator
);
}

/**
* Generate the persian slug if necessary.
*/
protected function persianSlug($title, $separator = '-'): string
{
$title = trim($title);
$title = mb_strtolower($title, 'UTF-8');

$title = str_replace('', $separator, $title);

$title = preg_replace(
'/[^a-z0-9_\s\-اآؤئبپتثجچحخدذرزژسشصضطظعغفقكکگلمنوةيإأۀءهی۰۱۲۳۴۵۶۷۸۹٠١٢٣٤٥٦٧٨٩]/u',
'',
$title
);

$title = preg_replace('/[\s\-_]+/', ' ', $title);
$title = preg_replace('/[\s_]/', $separator, $title);
$title = trim($title, $separator);

return $title;
}
}

0 comments on commit da836ba

Please sign in to comment.