Fixes #3939: \yii\Inflector::slug() improvements:

- Added protected `\yii\Inflector::transliterate()` that could be replaced with custom translit implementation.
- Added proper tests for both intl-based slug and PHP fallback.
- Removed character maps for non-latin languages.
- Improved overall slug results.
- Added note about the fact that intl is required for non-latin languages to requirements checker.
This commit is contained in:
Alexander Makarov
2014-06-19 18:55:07 +04:00
parent c84b3e0efd
commit c3de3450a7
5 changed files with 127 additions and 77 deletions

View File

@@ -0,0 +1,21 @@
<?php
namespace yiiunit\framework\helpers;
use yii\helpers\BaseInflector;
/**
* Forces Inflector::slug to use PHP even if intl is available
*/
class FallbackInflector extends BaseInflector
{
/**
* @inheritdoc
*/
protected static function hasIntl()
{
return false;
}
}

View File

@@ -122,16 +122,57 @@ class InflectorTest extends TestCase
$this->assertEquals("customer_tables", Inflector::tableize('customerTable'));
}
public function testSlug()
public function testSlugCommons()
{
$data = [
'Привет. Hello, Йии-- Framework !--- Как дела ? How it goes ?' => 'privet-hello-jii-framework-kak-dela-how-it-goes',
'this is a title' => 'this-is-a-title',
'недвижимость' => 'nedvizimost',
'' => '',
'hello world' => 'hello-world',
'remove.!?[]{}…symbols' => 'removesymbols',
'minus-sign' => 'minus-sign',
'mdash—sign' => 'mdash-sign',
'ndashsign' => 'ndash-sign',
'áàâéèêíìîóòôúùûã' => 'aaaeeeiiiooouuua',
'Ναδάλης ṃỹṛèşưḿĕ' => 'nadales-myresume',
'E=mc²' => 'e-mc2',
'載å¥' => 'e14a',
'älä lyö ääliö ööliä läikkyy' => 'ala-lyo-aalio-oolia-laikkyy',
];
foreach ($data as $source => $expected) {
if (extension_loaded('intl')) {
$this->assertEquals($expected, FallbackInflector::slug($source));
}
$this->assertEquals($expected, Inflector::slug($source));
}
}
public function testSlugIntl()
{
if (!extension_loaded('intl')) {
$this->markTestSkipped('intl extension is required.');
}
// Some test strings are from https://github.com/bergie/midgardmvc_helper_urlize. Thank you, Henri Bergius!
$data = [
// Korean
'해동검도' => 'haedong-geomdo',
// Hiragana
'ひらがな' => 'hiragana',
// Georgian
'საქართველო' => 'sakartvelo',
// Arabic
'العربي' => 'alrby',
'عرب' => 'rb',
// Hebrew
'עִבְרִית' => 'iberiyt',
// Turkish
'Sanırım hepimiz aynı şeyi düşünüyoruz.' => 'sanrm-hepimiz-ayn-seyi-dusunuyoruz',
// Russian
'недвижимость' => 'nedvizimost',
'Контакты' => 'kontakty',
];
foreach ($data as $source => $expected) {
@@ -139,6 +180,17 @@ class InflectorTest extends TestCase
}
}
public function testSlugPhp()
{
$data = [
'we have недвижимость' => 'we-have',
];
foreach ($data as $source => $expected) {
$this->assertEquals($expected, FallbackInflector::slug($source));
}
}
public function testClassify()
{
$this->assertEquals("CustomerTable", Inflector::classify('customer_tables'));