From d5cef3e2f23c19e61b5f3870507f6edee161dc95 Mon Sep 17 00:00:00 2001 From: ADmad Date: Tue, 29 Mar 2016 17:31:05 +0530 Subject: [PATCH] Add Text::defaultTransliteratorId() --- src/Utility/Text.php | 25 ++++++++++++++++++++----- tests/TestCase/Utility/TextTest.php | 12 ++++++++++++ 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/src/Utility/Text.php b/src/Utility/Text.php index 63eee63f6d1..df6089cd26b 100644 --- a/src/Utility/Text.php +++ b/src/Utility/Text.php @@ -26,9 +26,9 @@ class Text /** * Default transliterator id string. * - * @param string $defaultTransliteratorId Transliterator identifer string. + * @param string $_defaultTransliteratorId Transliterator identifer string. */ - public static $defaultTransliteratorId = 'Any-Latin; Latin-ASCII; [\u0080-\u7fff] remove'; + protected static $_defaultTransliteratorId = 'Any-Latin; Latin-ASCII; [\u0080-\u7fff] remove'; /** * Generate a random UUID version 4 @@ -860,18 +860,33 @@ public static function parseFileSize($size, $default = false) throw new InvalidArgumentException('No unit type.'); } + /** + * Get/set defautl transliterator identifer string. + * + * @param string $transliteratorId Transliterator identifer. + * @return string|null + */ + public static function defaultTransliteratorId($transliteratorId = null) + { + if ($transliteratorId === null) { + return static::$_defaultTransliteratorId; + } + + static::$_defaultTransliteratorId = $transliteratorId; + } + /** * Transliterate string. * * @param string $string String to transliterate. * @param string|null $transliteratorId Transliterator identifer. If null - * Text::$defaultTransliteratorId will be used. + * Text::$_defaultTransliteratorId will be used. * @return string * @see http://php.net/manual/en/transliterator.transliterate.php */ public static function transliterate($string, $transliteratorId = null) { - $transliteratorId = $transliteratorId ?: static::$defaultTransliteratorId; + $transliteratorId = $transliteratorId ?: static::$_defaultTransliteratorId; return transliterator_transliterate($transliteratorId, $string); } @@ -883,7 +898,7 @@ public static function transliterate($string, $transliteratorId = null) * @param array $options Valid options: * - `replacement`: Replacement string. Default '-'. * - `transliteratorId`: A valid tranliterator id string. - * If default `null` Text::$defaultTransliteratorId to be used. + * If default `null` Text::$_defaultTransliteratorId to be used. * If `false` no transliteration will be done, only non words will be removed. * - `preserve`: Specific non-word character to preserve. Default `null`. * For e.g. this option can be set to '.' to generate clean file names. diff --git a/tests/TestCase/Utility/TextTest.php b/tests/TestCase/Utility/TextTest.php index ba23acab39d..631b391e85c 100644 --- a/tests/TestCase/Utility/TextTest.php +++ b/tests/TestCase/Utility/TextTest.php @@ -1589,6 +1589,18 @@ public function filesizes() ]; } + public function testDefaultTransliteratorId() + { + $expected = 'Any-Latin; Latin-ASCII; [\u0080-\u7fff] remove'; + $this->assertEquals($expected, Text::defaultTransliteratorId()); + + $expected = 'Latin-ASCII; [\u0080-\u7fff] remove'; + Text::defaultTransliteratorId($expected); + $this->assertEquals($expected, Text::defaultTransliteratorId()); + + Text::defaultTransliteratorId('Any-Latin; Latin-ASCII; [\u0080-\u7fff] remove'); + } + /** * Data provider for testTransliterate() *