From 38f7c052d9f763ee0e7469e76fa9dd4d02aa479e Mon Sep 17 00:00:00 2001 From: wapmorgan Date: Sat, 17 Nov 2018 03:17:52 +0300 Subject: [PATCH] Remove initializatio of encoding in \morphos\S. Use utf-8 by default. Fix case when mbstring is not available (Tests 99%). --- composer.json | 3 +- src/S.php | 93 +++++++++++++++++++++++++++++------------- src/initialization.php | 2 - 3 files changed, 65 insertions(+), 33 deletions(-) delete mode 100644 src/initialization.php diff --git a/composer.json b/composer.json index fee33ca..142a288 100644 --- a/composer.json +++ b/composer.json @@ -21,8 +21,7 @@ "autoload": { "files": [ "src/English/functions.php", - "src/Russian/functions.php", - "src/initialization.php" + "src/Russian/functions.php" ], "psr-4": { "morphos\\": "src/" diff --git a/src/S.php b/src/S.php index ebee11b..ec0a3a9 100644 --- a/src/S.php +++ b/src/S.php @@ -6,20 +6,31 @@ */ class S { + /** @var string Encoding used for string manipulations */ + static protected $encoding; + + static protected $cyrillicAlphabet = [ + ['Ё', 'Й', 'Ц', 'У', 'К', 'Е', 'Н', 'Г', 'Ш', 'Щ', 'З', 'Х', 'Ъ', 'Ф', 'Ы', 'В', 'А', 'П', 'Р', 'О', 'Л', 'Д', 'Ж', 'Э', 'Я', 'Ч', 'С', 'М', 'И', 'Т', 'Ь', 'Б', 'Ю'], + ['ё', 'й', 'ц', 'у', 'к', 'е', 'н', 'г', 'ш', 'щ', 'з', 'х', 'ъ', 'ф', 'ы', 'в', 'а', 'п', 'р', 'о', 'л', 'д', 'ж', 'э', 'я', 'ч', 'с', 'м', 'и', 'т', 'ь', 'б', 'ю'], + ]; + /** - * Sets encoding for using in morphos/* functions. + * Sets encoding for all operations * @param string $encoding * @return bool */ public static function setEncoding($encoding) { - if (function_exists('mb_internal_encoding')) { - mb_internal_encoding($encoding); - } elseif (function_exists('iconv_set_encoding')) { - iconv_set_encoding('internal_encoding', $encoding); - } else { - return false; - } + static::$encoding = $encoding; + } + + /** + * Returns encoding used for all operations + * @return string + */ + public static function getEncoding() + { + return static::$encoding ?: 'utf-8'; } /** @@ -30,11 +41,11 @@ public static function setEncoding($encoding) public static function length($string) { if (function_exists('mb_strlen')) { - return mb_strlen($string); + return mb_strlen($string, static::getEncoding()); } if (function_exists('iconv_strlen')) { - return iconv_strlen($string); + return iconv_strlen($string, static::getEncoding()); } return false; @@ -49,16 +60,16 @@ public static function length($string) */ public static function slice($string, $start, $end = null) { - if ($end != null) { + if ($end !== null) { $end -= $start; } if (function_exists('mb_substr')) { - return $end === null ? mb_substr($string, $start) : mb_substr($string, $start, $end); + return mb_substr($string, $start, $end, static::getEncoding()); } if (function_exists('iconv_substr')) { - return $end === null ? iconv_substr($string, $start) : iconv_substr($string, $start, $end); + return iconv_substr($string, $start, $end ?: iconv_strlen($string), static::getEncoding()); } return false; @@ -72,10 +83,10 @@ public static function slice($string, $start, $end = null) public static function lower($string) { if (function_exists('mb_strtolower')) { - return mb_strtolower($string); + return mb_strtolower($string, static::getEncoding()); } - return false; + return static::replaceByMap($string, static::$cyrillicAlphabet[0], static::$cyrillicAlphabet[1]); } /** @@ -86,26 +97,24 @@ public static function lower($string) public static function upper($string) { if (function_exists('mb_strtoupper')) { - return mb_strtoupper($string); + return mb_strtoupper($string, static::getEncoding()); } - return false; + return static::replaceByMap($string, static::$cyrillicAlphabet[1], static::$cyrillicAlphabet[0]); } /** - * Name case. (ex: Thomas, Lewis) + * Name case. (ex: Thomas, Lewis). Works properly with separated by '-' words * @param $string * @return bool|string */ public static function name($string) { - if (function_exists('mb_strtoupper')) { - return implode('-', array_map(function ($word) { - return self::upper(self::slice($word, 0, 1)).self::lower(self::slice($word, 1)); - }, explode('-', $string))); + if (strpos($string, '-') !== false) { + return implode('-', array_map([__CLASS__, __FUNCTION__], explode('-', $string))); } - return false; + return self::upper(self::slice($string, 0, 1)).self::lower(self::slice($string, 1)); } /** @@ -120,7 +129,11 @@ public static function countChars($string, array $chars) return count(mb_split('('.implode('|', $chars).')', $string)) - 1; } - return false; + $counter = 0; + foreach ($chars as $char) { + $counter += substr_count($string, $char); + } + return $counter; } /** @@ -131,7 +144,7 @@ public static function countChars($string, array $chars) public static function findFirstPosition($string, $char) { if (function_exists('mb_strpos')) { - return mb_strpos($string, $char, 0); + return mb_strpos($string, $char, 0, static::getEncoding()); } return strpos($string, $char); @@ -145,7 +158,7 @@ public static function findFirstPosition($string, $char) public static function findLastPosition($string, $char) { if (function_exists('mb_strrpos')) { - return mb_strrpos($string, $char, 0); + return mb_strrpos($string, $char, 0, static::getEncoding()); } return strrpos($string, $char); @@ -161,14 +174,14 @@ public static function findLastPositionForOneOfChars($string, array $chars) if (function_exists('mb_strrpos')) { $last_position = false; foreach ($chars as $char) { - if (($pos = mb_strrpos($string, $char)) !== false) { + if (($pos = mb_strrpos($string, $char, 0, static::getEncoding())) !== false) { if ($pos > $last_position) { $last_position = $pos; } } } if ($last_position !== false) { - return mb_substr($string, $last_position); + return mb_substr($string, $last_position, null, static::getEncoding()); } return false; } @@ -186,9 +199,31 @@ public static function findLastPositionForOneOfChars($string, array $chars) public static function indexOf($string, $substring, $caseSensetive = false, $startOffset = 0) { if (function_exists('mb_stripos')) { - return $caseSensetive ? mb_strpos($string, $substring, $startOffset) : mb_stripos($string, $substring, $startOffset); + return $caseSensetive + ? mb_strpos($string, $substring, $startOffset, static::getEncoding()) + : mb_stripos($string, $substring, $startOffset, static::getEncoding()); } return false; } + + /** + * @param $string + * @param $fromMap + * @param $toMap + * @return string + */ + private static function replaceByMap($string, $fromMap, $toMap) + { + $encoding = static::getEncoding(); + if ($encoding !== 'utf-8') + $string = iconv($encoding, 'utf-8', $string); + + $string = strtr($string, array_combine($fromMap, $toMap)); + + if ($encoding !== 'utf-8') + $string = iconv('utf-8', $encoding, $string); + + return $string; + } } diff --git a/src/initialization.php b/src/initialization.php deleted file mode 100644 index ff21068..0000000 --- a/src/initialization.php +++ /dev/null @@ -1,2 +0,0 @@ -