Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Add the ability to specify how slugs are generated #277

Merged
merged 1 commit into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/Tab.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,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 @@ -49,6 +52,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 @@ -157,6 +166,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