Skip to content

Commit

Permalink
Merge pull request #456 from darron1217/issue#455
Browse files Browse the repository at this point in the history
Fix for Issue #455 (Adding support for UTF-8 slug)
  • Loading branch information
nWidart authored Feb 27, 2018
2 parents 8bb3cdc + 8e8bc6d commit d72663e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
21 changes: 21 additions & 0 deletions Modules/Tag/Tests/Integration/TaggableTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Modules\Tag\Tests\Integration;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use Modules\Page\Entities\Page;
use Modules\Page\Repositories\PageRepository;
use Modules\Tag\Repositories\TagRepository;
Expand Down Expand Up @@ -132,6 +133,26 @@ public function it_gets_all_tags_for_a_namespace()
$this->assertCount(3, Page::allTags()->get());
}

/** @test */
public function it_generates_slug_like_original_str_slug()
{
$page = $this->createPage();

$this->assertEquals(Str::slug('hello world'), $page->generateTagSlug('hello world'));
$this->assertEquals(Str::slug('hello world'), $page->generateTagSlug('hello-world'));
$this->assertEquals(Str::slug('hello_world'), $page->generateTagSlug('hello_world'));
$this->assertEquals(Str::slug('hello_world', '_'), $page->generateTagSlug('hello_world', '_'));
$this->assertEquals(Str::slug('user@host'), $page->generateTagSlug('user@host'));
}

/** @test */
public function it_gets_pages_with_non_latin_tags()
{
$this->createPage(['한글 태그']);

$this->assertCount(1, Page::whereTag(['한글-태그'])->get());
}

private function createPage(array $tags = [])
{
return $this->page->create([
Expand Down
18 changes: 16 additions & 2 deletions Modules/Tag/Traits/TaggableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,22 @@ protected function getEntityClassName()
/**
* {@inheritdoc}
*/
protected function generateTagSlug($name)
public function generateTagSlug($name, $separator = '-')
{
return str_slug($name);
// Convert all dashes/underscores into separator
$flip = $separator == '-' ? '_' : '-';

$name = preg_replace('!['.preg_quote($flip).']+!u', $separator, $name);

// Replace @ with the word 'at'
$name = str_replace('@', $separator.'at'.$separator, $name);

// Remove all characters that are not the separator, letters, numbers, or whitespace.
$name = preg_replace('![^'.preg_quote($separator).'\pL\pN\s]+!u', '', mb_strtolower($name));

// Replace all separator characters and whitespace by a single separator
$name = preg_replace('!['.preg_quote($separator).'\s]+!u', $separator, $name);

return trim($name, $separator);
}
}

0 comments on commit d72663e

Please sign in to comment.