From f8e05fbf4f407b612fd36c9269506c99b670d48c Mon Sep 17 00:00:00 2001 From: Alexander Makarov Date: Sun, 8 Jun 2014 05:44:31 +0400 Subject: [PATCH] Improved `yii\helpers\Inflector::slug` to support more cases for Russian, Hebrew and special characters --- framework/CHANGELOG.md | 1 + framework/helpers/BaseInflector.php | 4 ++-- tests/unit/framework/helpers/InflectorTest.php | 16 ++++++++++++++-- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 74f8abd5e2..eaedfcb3fe 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -87,6 +87,7 @@ Yii Framework 2 Change Log - Enh: Added support for using path alias with `FileDependency::fileName` (qiangxue) - Enh: Added param `hideOnSinglePage` to `yii\widgets\LinkPager` (arturf) - Enh: Added support for array attributes in `in` validator (creocoder) +- Enh: Improved `yii\helpers\Inflector::slug` to support more cases for Russian, Hebrew and special characters (samdark) - Chg #2898: `yii\console\controllers\AssetController` is now using hashes instead of timestamps (samdark) - Chg #2913: RBAC `DbManager` is now initialized via migration (samdark) - Chg #3036: Upgraded Twitter Bootstrap to 3.1.x (qiangxue) diff --git a/framework/helpers/BaseInflector.php b/framework/helpers/BaseInflector.php index 2f73db8bdf..cbf26371e4 100644 --- a/framework/helpers/BaseInflector.php +++ b/framework/helpers/BaseInflector.php @@ -467,9 +467,9 @@ class BaseInflector public static function slug($string, $replacement = '-', $lowercase = true) { if (extension_loaded('intl') === true) { - $options = 'Any-Latin; NFD; [:Nonspacing Mark:] Remove; NFC; [:Punctuation:] Remove;'; + $options = 'Any-Latin; NFKD; [:Punctuation:] Remove; [^\u0000-\u007E] Remove'; $string = transliterator_transliterate($options, $string); - $string = preg_replace('/[-\s]+/', $replacement, $string); + $string = preg_replace('/[=-\s]+/', $replacement, $string); } else { $string = str_replace(array_keys(static::$transliteration), static::$transliteration, $string); $string = preg_replace('/[^\p{L}\p{Nd}]+/u', $replacement, $string); diff --git a/tests/unit/framework/helpers/InflectorTest.php b/tests/unit/framework/helpers/InflectorTest.php index 9d1c44ad0c..08922f8a9a 100644 --- a/tests/unit/framework/helpers/InflectorTest.php +++ b/tests/unit/framework/helpers/InflectorTest.php @@ -124,9 +124,21 @@ class InflectorTest extends TestCase public function testSlug() { - $this->assertEquals("privet-hello-jii-framework-kak-dela-how-it-goes", Inflector::slug('Привет Hello Йии-- Framework !--- Как дела ? How it goes ?')); + $data = [ + 'Привет. Hello, Йии-- Framework !--- Как дела ? How it goes ?' => 'privet-hello-jii-framework-kak-dela-how-it-goes', + 'this is a title' => 'this-is-a-title', + 'недвижимость' => 'nedvizimost', + 'áàâéèêíìîóòôúùûã' => 'aaaeeeiiiooouuua', + 'Ναδάλης ṃỹṛèşưḿĕ' => 'nadales-myresume', + 'E=mc²' => 'e-mc2', + '載å¥' => 'e14a', + ]; - $this->assertEquals("this-is-a-title", Inflector::slug('this is a title')); + foreach ($data as $source => $expected) { + $this->assertEquals($expected, Inflector::slug($source)); + } + + //$this->assertEquals('this-is-my-text-', Inflector::slug('! this is my / text $## ')); } public function testClassify()