Skip to content

Commit

Permalink
Remove initializatio of encoding in \morphos\S. Use utf-8 by default.…
Browse files Browse the repository at this point in the history
… Fix case when mbstring is not available (Tests 99%).
  • Loading branch information
wapmorgan committed Nov 17, 2018
1 parent b5bb9d0 commit 38f7c05
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 33 deletions.
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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/"
Expand Down
93 changes: 64 additions & 29 deletions src/S.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}

/**
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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]);
}

/**
Expand All @@ -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));
}

/**
Expand All @@ -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;
}

/**
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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;
}
Expand All @@ -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;
}
}
2 changes: 0 additions & 2 deletions src/initialization.php

This file was deleted.

0 comments on commit 38f7c05

Please sign in to comment.