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

[11.x] slug() with alphanumeric separator #54446

Closed
wants to merge 3 commits into from
Closed
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
7 changes: 7 additions & 0 deletions src/Illuminate/Support/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Closure;
use Illuminate\Support\Traits\Macroable;
use InvalidArgumentException;
use JsonException;
use League\CommonMark\Environment\Environment;
use League\CommonMark\Extension\GithubFlavoredMarkdownExtension;
Expand Down Expand Up @@ -1460,9 +1461,15 @@ public static function singular($value)
* @param string|null $language
* @param array<string, string> $dictionary
* @return string
*
* @throws InvalidArgumentException
*/
public static function slug($title, $separator = '-', $language = 'en', $dictionary = ['@' => 'at'])
{
if (ctype_alnum($separator)) {
throw new InvalidArgumentException('The separator must not be alphanumeric.');
}

$title = $language ? static::ascii($title, $language) : $title;

// Convert all dashes/underscores into separator
Expand Down
13 changes: 13 additions & 0 deletions tests/Support/SupportStringableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Support\Collection;
use Illuminate\Support\HtmlString;
use Illuminate\Support\Stringable;
use InvalidArgumentException;
use League\CommonMark\Environment\EnvironmentBuilderInterface;
use League\CommonMark\Extension\ExtensionInterface;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -741,6 +742,18 @@ public function testSlug()
$this->assertSame('', (string) $this->stringable('')->slug());
}

public function testSlugDoesNotSupportAlphaSeparator()
{
$this->expectException(InvalidArgumentException::class);
$this->stringable('hello world')->slug('a');
}

public function testSlugDoesNotSupportNumericSeparator()
{
$this->expectException(InvalidArgumentException::class);
$this->stringable('hello world')->slug('2');
}

public function testSquish()
{
$this->assertSame('words with spaces', (string) $this->stringable(' words with spaces ')->squish());
Expand Down