Browse Source

Add Text::transliterate()

ADmad 10 years ago
parent
commit
6addb5d3df
2 changed files with 78 additions and 2 deletions
  1. 16 2
      src/Utility/Text.php
  2. 62 0
      tests/TestCase/Utility/TextTest.php

+ 16 - 2
src/Utility/Text.php

@@ -861,6 +861,20 @@ class Text
     }
 
     /**
+     * Transliterate string.
+     *
+     * @param string $string String to transliterate.
+     * @param string|null $transliteratorId Transliterator identifer. If null
+     *   Text::$defaultTransliteratorId will be used.
+     * @return string
+     */
+    public static function transliterate($string, $transliteratorId = null)
+    {
+        $transliteratorId = $transliteratorId ?: static::$defaultTransliteratorId;
+        return transliterator_transliterate($transliteratorId, $string);
+    }
+
+    /**
      * Returns a string with all spaces converted to dashes (by default), accented
      * characters converted to non-accented characters, and non word characters removed.
      *
@@ -872,10 +886,10 @@ class Text
     {
         $options += [
             'replacement' => '-',
-            'transliteratorId' => static::$defaultTransliteratorId
+            'transliteratorId' => null
         ];
 
-        $string = transliterator_transliterate($options['transliteratorId'], $string);
+        $string = static::transliterate($string, $options['transliteratorId']);
 
         $quotedReplacement = preg_quote($options['replacement'], '/');
         $map = [

+ 62 - 0
tests/TestCase/Utility/TextTest.php

@@ -1589,6 +1589,68 @@ podeís adquirirla.</span></p>
         ];
     }
 
+    /**
+     * Data provider for testTransliterate()
+     *
+     * @return array
+     */
+    public function transliterateInputProvider()
+    {
+        return [
+            [
+                'Foo Bar: Not just for breakfast any-more', null,
+                'Foo Bar: Not just for breakfast any-more'
+            ],
+            [
+                'A æ Übérmensch på høyeste nivå! И я люблю PHP! есть. fi ¦', null,
+                'A ae Ubermensch pa hoyeste niva! И я люблю PHP! есть. fi ¦'
+            ],
+            [
+                'Äpfel Über Öl grün ärgert groß öko', null,
+                'Aepfel Ueber Oel gruen aergert gross oeko'
+            ],
+            [
+                'La langue française est un attribut de souveraineté en France', null,
+                'La langue francaise est un attribut de souverainete en France'
+            ],
+            [
+                '!@$#exciting stuff! - what !@-# was that?', null,
+                '!@$#exciting stuff! - what !@-# was that?'
+            ],
+            [
+                'controller/action/りんご/1', null,
+                'controller/action/りんご/1'
+            ],
+            [
+                'の話が出たので大丈夫かなあと', null,
+                'の話が出たので大丈夫かなあと'
+            ],
+            [
+                'posts/view/한국어/page:1/sort:asc', null,
+                'posts/view/한국어/page:1/sort:asc'
+            ],
+            [
+                "non\xc2\xa0breaking\xc2\xa0space", null,
+                'non breaking space'
+            ]
+        ];
+    }
+
+    /**
+     * testTransliterate method
+     *
+     * @param string $string String
+     * @param string $transliteratorId Transliterator Id
+     * @param String $expected Exepected string
+     * @return void
+     * @dataProvider transliterateInputProvider
+     */
+    public function testTransliterate($string, $transliteratorId, $expected)
+    {
+        $result = Text::transliterate($string, $transliteratorId);
+        $this->assertEquals($expected, $result);
+    }
+
     public function slugInputProvider()
     {
         return [