Browse Source

Merge pull request #464 from tonydspaniard/364-toAscii

enhance Inflector  helper with ascii function
tags/2.0.0-beta
Qiang Xue 11 years ago
parent
commit
f18e530fe0
  1. 1
      framework/CHANGELOG.md
  2. 129
      framework/yii/helpers/BaseInflector.php
  3. 2
      tests/unit/framework/helpers/InflectorTest.php

1
framework/CHANGELOG.md

@ -49,6 +49,7 @@ Yii Framework 2 Change Log
- Chg: Changed the signature of `urlCreator` and button creators for `yii\gridview\ActionColumn` (qiangxue) - Chg: Changed the signature of `urlCreator` and button creators for `yii\gridview\ActionColumn` (qiangxue)
- New #1438: [MongoDB integration](https://github.com/yiisoft/yii2-mongodb) ActiveRecord and Query (klimov-paul) - New #1438: [MongoDB integration](https://github.com/yiisoft/yii2-mongodb) ActiveRecord and Query (klimov-paul)
- New #1393: [Codeception testing framework integration](https://github.com/yiisoft/yii2-codeception) (Ragazzo) - New #1393: [Codeception testing framework integration](https://github.com/yiisoft/yii2-codeception) (Ragazzo)
- Enh #364: Improve Inflector::slug with `intl` transliteration. Improved transliteration char map. (tonydspaniard)
2.0.0 alpha, December 1, 2013 2.0.0 alpha, December 1, 2013
--------------------------- ---------------------------

129
framework/yii/helpers/BaseInflector.php

@ -215,60 +215,67 @@ class BaseInflector
'wildebeest' => 'wildebeest', 'wildebeest' => 'wildebeest',
'Yengeese' => 'Yengeese', 'Yengeese' => 'Yengeese',
]; ];
/** /**
* @var array map of special chars and its translation. This is used by [[slug()]]. * @var array map of special chars and its translation. This is used by [[slug()]].
*/ */
public static $transliteration = [ public static $transliteration = [
'/ä|æ|ǽ/' => 'ae', // Latin
'/ö|œ/' => 'oe', 'À' => 'A', 'Á' => 'A', 'Â' => 'A', 'Ã' => 'A', 'Ä' => 'A', 'Å' => 'A', 'Æ' => 'AE', 'Ç' => 'C',
'/ü/' => 'ue', 'È' => 'E', 'É' => 'E', 'Ê' => 'E', 'Ë' => 'E', 'Ì' => 'I', 'Í' => 'I', 'Î' => 'I', 'Ï' => 'I',
'/Ä/' => 'Ae', 'Ð' => 'D', 'Ñ' => 'N', 'Ò' => 'O', 'Ó' => 'O', 'Ô' => 'O', 'Õ' => 'O', 'Ö' => 'O', 'Ő' => 'O',
'/Ü/' => 'Ue', 'Ø' => 'O', 'Ù' => 'U', 'Ú' => 'U', 'Û' => 'U', 'Ü' => 'U', 'Ű' => 'U', 'Ý' => 'Y', 'Þ' => 'TH',
'/Ö/' => 'Oe', 'ß' => 'ss',
'/À|Á|Â|Ã|Å|Ǻ|Ā|Ă|Ą|Ǎ/' => 'A', 'à' => 'a', 'á' => 'a', 'â' => 'a', 'ã' => 'a', 'ä' => 'a', 'å' => 'a', 'æ' => 'ae', 'ç' => 'c',
'/à|á|â|ã|å|ǻ|ā|ă|ą|ǎ|ª/' => 'a', 'è' => 'e', 'é' => 'e', 'ê' => 'e', 'ë' => 'e', 'ì' => 'i', 'í' => 'i', 'î' => 'i', 'ï' => 'i',
'/Ç|Ć|Ĉ|Ċ|Č/' => 'C', 'ð' => 'd', 'ñ' => 'n', 'ò' => 'o', 'ó' => 'o', 'ô' => 'o', 'õ' => 'o', 'ö' => 'o', 'ő' => 'o',
'/ç|ć|ĉ|ċ|č/' => 'c', 'ø' => 'o', 'ù' => 'u', 'ú' => 'u', 'û' => 'u', 'ü' => 'u', 'ű' => 'u', 'ý' => 'y', 'þ' => 'th',
'/Ð|Ď|Đ/' => 'D', 'ÿ' => 'y',
'/ð|ď|đ/' => 'd', // Latin symbols
'/È|É|Ê|Ë|Ē|Ĕ|Ė|Ę|Ě/' => 'E', '©' => '(c)',
'/è|é|ê|ë|ē|ĕ|ė|ę|ě/' => 'e', // Greek
'/Ĝ|Ğ|Ġ|Ģ/' => 'G', 'Α' => 'A', 'Β' => 'B', 'Γ' => 'G', 'Δ' => 'D', 'Ε' => 'E', 'Ζ' => 'Z', 'Η' => 'H', 'Θ' => '8',
'/ĝ|ğ|ġ|ģ/' => 'g', 'Ι' => 'I', 'Κ' => 'K', 'Λ' => 'L', 'Μ' => 'M', 'Ν' => 'N', 'Ξ' => '3', 'Ο' => 'O', 'Π' => 'P',
'/Ĥ|Ħ/' => 'H', 'Ρ' => 'R', 'Σ' => 'S', 'Τ' => 'T', 'Υ' => 'Y', 'Φ' => 'F', 'Χ' => 'X', 'Ψ' => 'PS', 'Ω' => 'W',
'/ĥ|ħ/' => 'h', 'Ά' => 'A', 'Έ' => 'E', 'Ί' => 'I', 'Ό' => 'O', 'Ύ' => 'Y', 'Ή' => 'H', 'Ώ' => 'W', 'Ϊ' => 'I',
'/Ì|Í|Î|Ï|Ĩ|Ī|Ĭ|Ǐ|Į|İ/' => 'I', 'Ϋ' => 'Y',
'/ì|í|î|ï|ĩ|ī|ĭ|ǐ|į|ı/' => 'i', 'α' => 'a', 'β' => 'b', 'γ' => 'g', 'δ' => 'd', 'ε' => 'e', 'ζ' => 'z', 'η' => 'h', 'θ' => '8',
'/Ĵ/' => 'J', 'ι' => 'i', 'κ' => 'k', 'λ' => 'l', 'μ' => 'm', 'ν' => 'n', 'ξ' => '3', 'ο' => 'o', 'π' => 'p',
'/ĵ/' => 'j', 'ρ' => 'r', 'σ' => 's', 'τ' => 't', 'υ' => 'y', 'φ' => 'f', 'χ' => 'x', 'ψ' => 'ps', 'ω' => 'w',
'/Ķ/' => 'K', 'ά' => 'a', 'έ' => 'e', 'ί' => 'i', 'ό' => 'o', 'ύ' => 'y', 'ή' => 'h', 'ώ' => 'w', 'ς' => 's',
'/ķ/' => 'k', 'ϊ' => 'i', 'ΰ' => 'y', 'ϋ' => 'y', 'ΐ' => 'i',
'/Ĺ|Ļ|Ľ|Ŀ|Ł/' => 'L', // Turkish
'/ĺ|ļ|ľ|ŀ|ł/' => 'l', 'Ş' => 'S', 'İ' => 'I', 'Ç' => 'C', 'Ü' => 'U', 'Ö' => 'O', 'Ğ' => 'G',
'/Ñ|Ń|Ņ|Ň/' => 'N', 'ş' => 's', 'ı' => 'i', 'ç' => 'c', 'ü' => 'u', 'ö' => 'o', 'ğ' => 'g',
'/ñ|ń|ņ|ň|ʼn/' => 'n', // Russian
'/Ò|Ó|Ô|Õ|Ō|Ŏ|Ǒ|Ő|Ơ|Ø|Ǿ/' => 'O', 'А' => 'A', 'Б' => 'B', 'В' => 'V', 'Г' => 'G', 'Д' => 'D', 'Е' => 'E', 'Ё' => 'Yo', 'Ж' => 'Zh',
'/ò|ó|ô|õ|ō|ŏ|ǒ|ő|ơ|ø|ǿ|º/' => 'o', 'З' => 'Z', 'И' => 'I', 'Й' => 'J', 'К' => 'K', 'Л' => 'L', 'М' => 'M', 'Н' => 'N', 'О' => 'O',
'/Ŕ|Ŗ|Ř/' => 'R', 'П' => 'P', 'Р' => 'R', 'С' => 'S', 'Т' => 'T', 'У' => 'U', 'Ф' => 'F', 'Х' => 'H', 'Ц' => 'C',
'/ŕ|ŗ|ř/' => 'r', 'Ч' => 'Ch', 'Ш' => 'Sh', 'Щ' => 'Sh', 'Ъ' => '', 'Ы' => 'Y', 'Ь' => '', 'Э' => 'E', 'Ю' => 'Yu',
'/Ś|Ŝ|Ş|Ș|Š/' => 'S', 'Я' => 'Ya',
'/ś|ŝ|ş|ș|š|ſ/' => 's', 'а' => 'a', 'б' => 'b', 'в' => 'v', 'г' => 'g', 'д' => 'd', 'е' => 'e', 'ё' => 'yo', 'ж' => 'zh',
'/Ţ|Ț|Ť|Ŧ/' => 'T', 'з' => 'z', 'и' => 'i', 'й' => 'j', 'к' => 'k', 'л' => 'l', 'м' => 'm', 'н' => 'n', 'о' => 'o',
'/ţ|ț|ť|ŧ/' => 't', 'п' => 'p', 'р' => 'r', 'с' => 's', 'т' => 't', 'у' => 'u', 'ф' => 'f', 'х' => 'h', 'ц' => 'c',
'/Ù|Ú|Û|Ũ|Ū|Ŭ|Ů|Ű|Ų|Ư|Ǔ|Ǖ|Ǘ|Ǚ|Ǜ/' => 'U', 'ч' => 'ch', 'ш' => 'sh', 'щ' => 'sh', 'ъ' => '', 'ы' => 'y', 'ь' => '', 'э' => 'e', 'ю' => 'yu',
'/ù|ú|û|ũ|ū|ŭ|ů|ű|ų|ư|ǔ|ǖ|ǘ|ǚ|ǜ/' => 'u', 'я' => 'ya',
'/Ý|Ÿ|Ŷ/' => 'Y', // Ukrainian
'/ý|ÿ|ŷ/' => 'y', 'Є' => 'Ye', 'І' => 'I', 'Ї' => 'Yi', 'Ґ' => 'G',
'/Ŵ/' => 'W', 'є' => 'ye', 'і' => 'i', 'ї' => 'yi', 'ґ' => 'g',
'/ŵ/' => 'w', // Czech
'/Ź|Ż|Ž/' => 'Z', 'Č' => 'C', 'Ď' => 'D', 'Ě' => 'E', 'Ň' => 'N', 'Ř' => 'R', 'Š' => 'S', 'Ť' => 'T', 'Ů' => 'U',
'/ź|ż|ž/' => 'z', 'Ž' => 'Z',
'/Æ|Ǽ/' => 'AE', 'č' => 'c', 'ď' => 'd', 'ě' => 'e', 'ň' => 'n', 'ř' => 'r', 'š' => 's', 'ť' => 't', 'ů' => 'u',
'/ß/' => 'ss', 'ž' => 'z',
'/IJ/' => 'IJ', // Polish
'/ij/' => 'ij', 'Ą' => 'A', 'Ć' => 'C', 'Ę' => 'e', 'Ł' => 'L', 'Ń' => 'N', 'Ó' => 'o', 'Ś' => 'S', 'Ź' => 'Z',
'/Œ/' => 'OE', 'Ż' => 'Z',
'/ƒ/' => 'f' 'ą' => 'a', 'ć' => 'c', 'ę' => 'e', 'ł' => 'l', 'ń' => 'n', 'ó' => 'o', 'ś' => 's', 'ź' => 'z',
'ż' => 'z',
// Latvian
'Ā' => 'A', 'Č' => 'C', 'Ē' => 'E', 'Ģ' => 'G', 'Ī' => 'i', 'Ķ' => 'k', 'Ļ' => 'L', 'Ņ' => 'N',
'Š' => 'S', 'Ū' => 'u', 'Ž' => 'Z',
'ā' => 'a', 'č' => 'c', 'ē' => 'e', 'ģ' => 'g', 'ī' => 'i', 'ķ' => 'k', 'ļ' => 'l', 'ņ' => 'n',
'š' => 's', 'ū' => 'u', 'ž' => 'z'
]; ];
/** /**
@ -434,20 +441,24 @@ class BaseInflector
/** /**
* Returns a string with all spaces converted to given replacement and * Returns a string with all spaces converted to given replacement and
* non word characters removed. Maps special characters to ASCII using * non word characters removed. Maps special characters to ASCII using
* `Inflector::$transliteration` * [[$transliteration]] array.
* @param string $string An arbitrary string to convert * @param string $string An arbitrary string to convert
* @param string $replacement The replacement to use for spaces * @param string $replacement The replacement to use for spaces
* @param bool $lowercase whether to return the string in lowercase or not. Defaults to `true`.
* @return string The converted string. * @return string The converted string.
*/ */
public static function slug($string, $replacement = '-') public static function slug($string, $replacement = '-', $lowercase = true)
{ {
$map = static::$transliteration + [ if (extension_loaded('intl') === true) {
'/[^\w\s]/' => ' ', $options = 'Any-Latin; NFD; [:Nonspacing Mark:] Remove; NFC; [:Punctuation:] Remove;';
'/\\s+/' => $replacement, $string = transliterator_transliterate($options, $string);
'/(?<=[a-z])([A-Z])/' => $replacement . '\\1', $string = preg_replace('/[-\s]+/', $replacement, $string);
str_replace(':rep', preg_quote($replacement, '/'), '/^[:rep]+|[:rep]+$/') => '' } else {
]; $string = str_replace(array_keys(static::$transliteration), static::$transliteration, $string);
return preg_replace(array_keys($map), array_values($map), $string); $string = preg_replace('/[^\p{L}\p{Nd}]+/u', $replacement, $string);
}
$string = trim($string, $replacement);
return $lowercase ? strtolower($string) : $string;
} }
/** /**

2
tests/unit/framework/helpers/InflectorTest.php

@ -123,6 +123,8 @@ class InflectorTest extends TestCase
public function testSlug() public function testSlug()
{ {
$this->assertEquals("privet-hello-jii-framework-kak-dela-how-it-goes", Inflector::slug('Привет Hello Йии-- Framework !--- Как дела ? How it goes ?'));
$this->assertEquals("this-is-a-title", Inflector::slug('this is a title')); $this->assertEquals("this-is-a-title", Inflector::slug('this is a title'));
} }

Loading…
Cancel
Save