Skip to content

Commit

Permalink
Fixing a bug on the normalizer configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
rtconner committed Jul 6, 2019
1 parent f66d041 commit 5c11544
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
2 changes: 1 addition & 1 deletion config/tagging.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
'primary_keys_type' => 'integer', // 'string' or 'integer'

// Value of are passed through this before save of tags
'normalizer' => '\Conner\Tagging\Util::slug',
'normalizer' => '\Conner\Tagging\TaggingUtility::slug',

// Display value of tags are passed through (for front end display)
'displayer' => '\Illuminate\Support\Str::title',
Expand Down
7 changes: 7 additions & 0 deletions src/TaggingUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Conner\Tagging;

use Illuminate\Support\Str;

/**
* Utility functions to help with various tagging functionality.
*
Expand Down Expand Up @@ -44,6 +46,11 @@ public static function displayize($string)
public static function normalize($string)
{
$normalizer = config('tagging.normalizer');

if(is_string($normalizer) && Str::contains($normalizer, 'Conner\Tagging\Util')) {
$normalizer = '\Conner\Tagging\TaggingUtility::slug';
}

$normalizer = $normalizer ?: self::class.'::slug';

return call_user_func($normalizer, $string);
Expand Down
20 changes: 20 additions & 0 deletions tests/TaggingUtilityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,26 @@ public function test_normalizer()
$this->assertEquals('aaa', TaggingUtility::normalize('some string'));
}

public function test_normalizer_with_old_class()
{
$this->assertEquals(TaggingUtility::slug('Sugar Free'), TaggingUtility::normalize('Sugar Free'));
$this->assertEquals(TaggingUtility::slug('ПЧ�Цщ'), TaggingUtility::normalize('ПЧ�Цщ'));
$this->assertEquals(TaggingUtility::slug('quiénsí'), TaggingUtility::normalize('quiénsí'));

config(['tagging.normalizer' => '\Conner\Tagging\Util::slug']);
$this->assertEquals('some-string', TaggingUtility::normalize('some string'));
}

public function test_normalizer_with_new_class()
{
$this->assertEquals(TaggingUtility::slug('Sugar Free'), TaggingUtility::normalize('Sugar Free'));
$this->assertEquals(TaggingUtility::slug('ПЧ�Цщ'), TaggingUtility::normalize('ПЧ�Цщ'));
$this->assertEquals(TaggingUtility::slug('quiénsí'), TaggingUtility::normalize('quiénsí'));

config(['tagging.normalizer' => '\Conner\Tagging\TaggingUtility::slug']);
$this->assertEquals('some-string', TaggingUtility::normalize('some string'));
}

public function test_displayize()
{
$this->assertEquals('Sugar Free', TaggingUtility::displayize('sugar free'));
Expand Down

0 comments on commit 5c11544

Please sign in to comment.