Skip to content

Commit

Permalink
Merge pull request #277 from RVxLab/master
Browse files Browse the repository at this point in the history
Feature: Add the ability to specify how slugs are generated
  • Loading branch information
marcfil authored Sep 9, 2024
2 parents 2880bdf + 6d3fa6c commit 2d17ad1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
13 changes: 13 additions & 0 deletions src/Tab.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ class Tab implements TabContract, JsonSerializable, Arrayable

protected $position;

/** @var null|callable(\Eminiarts\Tabs\Contracts\TabContract $tab): string */
protected static $createSlugUsing = null;

public function __construct($title, array $fields, $position = 0)
{
$this->title = $title;
Expand All @@ -56,6 +59,12 @@ public static function make($title, array $fields): self
return new static($title, $fields);
}

/** @param callable(\Eminiarts\Tabs\Contracts\TabContract $tab): string $createSlugUsingFunction */
public static function createSlugUsing(callable $createSlugUsingFunction): void
{
self::$createSlugUsing = $createSlugUsingFunction;
}

public function position(int $position): self
{
$this->position = $position;
Expand Down Expand Up @@ -180,6 +189,10 @@ public function getName(): string

public function getSlug(): string
{
if (null !== $makeSlug = static::$createSlugUsing) {
return $makeSlug($this);
}

return Str::slug($this->getName());
}

Expand Down
18 changes: 16 additions & 2 deletions tests/Feature/TabTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Eminiarts\Tabs\Tab;
use PHPUnit\Framework\TestCase;
use Illuminate\Support\Str;

class TabTest extends TestCase
{
Expand Down Expand Up @@ -126,16 +127,29 @@ public function testBodyClass(): void
], $tab->toArray()['bodyClass']);
}

public function testCanSetCustomSlugGenerationFunction(): void
{
Tab::createSlugUsing(fn (Tab $tab) => strrev(Str::slug($tab->getTitle())));

$tab = Tab::make('Test tab', []);

self::assertSame('bat-tset', $tab->getSlug());
}

/**
* @link https://github.com/eminiarts/nova-tabs/issues/145
* @link https://github.com/eminiarts/nova-tabs/issues/167
* @link https://github.com/eminiarts/nova-tabs/issues/275
*
* @dataProvider multibyteTitleProvider
*/
public function testDoesNotCrashWithMultibyteCharactersAsTitle(string $title): void
public function testCanHandleSlugsInNonAsciiCharacters(string $title): void
{
Tab::createSlugUsing(fn (Tab $tab) => $tab->getTitle());

$tab = Tab::make($title, []);

self::assertEmpty($tab->getSlug());
self::assertSame($title, $tab->getSlug());
}

/**
Expand Down

0 comments on commit 2d17ad1

Please sign in to comment.