File: /var/www/limestate-api/Models/Helper.php
<?php
class Helper {
public static function puntoSwitcher($word, $type='R', $special=false){
$ru = array( '',
'Ё', '!', '"', '№', ';', '%', ':', '?', '*', '(', ')', '_', '+',
'Й', 'Ц', 'У', 'К', 'Е', 'Н', 'Г', 'Ш', 'Щ', 'З', 'Х', 'Ъ', '/',
'Ф', 'Ы', 'В', 'А', 'П', 'Р', 'О', 'Л', 'Д', 'Ж', 'Э',
'Я', 'Ч', 'С', 'М', 'И', 'Т', 'Ь', 'Б', 'Ю', ',',
'ё', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=',
'й', 'ц', 'у', 'к', 'е', 'н', 'г', 'ш', 'щ', 'з', 'х', 'ъ', '\\',
'ф', 'ы', 'в', 'а', 'п', 'р', 'о', 'л', 'д', 'ж', 'э',
'я', 'ч', 'с', 'м', 'и', 'т', 'ь', 'б', 'ю', '.');
$eng = array('',
'~', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+',
'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}', '|',
'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', '"',
'Z', 'X', 'C', 'V', 'B', 'N', 'M', '<', '>', '?',
'`', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=',
'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\\',
'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', '\'',
'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/');
$ruTrans = array ('',
'Ё', 'Й', 'Ц', 'У', 'К', 'Е', 'Н', 'Г', 'Ш', 'Щ', 'З', 'Х', 'Ъ',
'Ф', 'Ы', 'В', 'А', 'П', 'Р', 'О', 'Л', 'Д', 'Ж', 'Э',
'Я', 'Ч', 'С', 'М', 'И', 'Т', 'Ь', 'Б', 'Ю',
'ё', 'й', 'ц', 'у', 'к', 'е', 'н', 'г', 'ш', 'щ', 'з', 'х', 'ъ',
'ф', 'ы', 'в', 'а', 'п', 'р', 'о', 'л', 'д', 'ж', 'э',
'я', 'ч', 'с', 'м', 'и', 'т', 'ь', 'б', 'ю');
$engTrans = array ('',
'JO', 'J', 'TS', 'U', 'K', 'E', 'N', 'G', 'SH', 'SHH', 'Z', 'H', ',\'',
'F', 'Y', 'V', 'A', 'P', 'R', 'O', 'L', 'D', 'ZH', 'E-',
'YA', 'CH', 'S', 'M', 'I', 'T', '\'', 'B', 'YU',
'jo', 'j', 'ts', 'u', 'k', 'e', 'n', 'g', 'sh', 'shh', 'z', 'h', ',\'',
'f', 'y', 'v', 'a', 'p', 'r', 'o', 'l', 'd', 'zh', 'e-',
'ya', 'ch', 's', 'm', 'i', 't', '\'', 'b', 'yu');
//Проверяем каких букв больше в слове
$ruCount = 0; $engCount = 0;
mb_internal_encoding('UTF-8');
for ($i=0;$i<mb_strlen($word);$i++){
if (in_array(mb_substr($word,$i,1),$ru)) $ruCount++;
if (in_array(mb_substr($word,$i,1),$eng)) $engCount++;
}
//echo 'ru: '.$ruCount.' eng: '.$engCount.'<br />';
$newWord = '';
if (($type == 'R' && $ruCount >= $engCount) || $type == 'R->eng') { // Если раскладка в английский
for ($i=0;$i<mb_strlen($word);$i++){
if(!$special && in_array(mb_substr($word,$i,1),array('\\','/'))) continue;
$newChar = $eng[array_search(mb_substr($word,$i,1),$ru)];
if ($newChar) $newWord .= $newChar; else $newWord .= mb_substr($word,$i,1);
//echo mb_substr($word,$i,1).' -> '.array_search(mb_substr($word,$i,1),$ru).' -> '.$eng[array_search(mb_substr($word,$i,1),$ru)].'<br />';
}
} else if (($type == 'R' && $ruCount < $engCount) || $type == 'R->ru') { // Если раскладка в русский
for ($i=0;$i<mb_strlen($word);$i++){
if(!$special && in_array(mb_substr($word,$i,1),array('\\','/'))) continue;
$newChar = $ru[array_search(mb_substr($word,$i,1),$eng)];
if ($newChar) $newWord .= $newChar; else $newWord .= mb_substr($word,$i,1);
//echo mb_substr($word,$i,1).' -> '.array_search(mb_substr($word,$i,1),$eng).' -> '.$ru[array_search(mb_substr($word,$i,1),$eng)].'<br />';
}
} else if (($type == 'T' && $ruCount >= $engCount) || $type == 'T->eng'){ // Если транслитерация в английский
for ($i=0;$i<mb_strlen($word);$i++){
if(!$special && in_array(mb_substr($word,$i,1),array('\\','/'))) continue;
$newChar = $engTrans[array_search(mb_substr($word,$i,1),$ruTrans)];
if ($newChar) $newWord .= $newChar; else $newWord .= mb_substr($word,$i,1);
//echo mb_substr($word,$i,1).' -> '.array_search(mb_substr($word,$i,1),$ru).' -> '.$eng[array_search(mb_substr($word,$i,1),$ru)].'<br />';
}
} else if (($type == 'T' && $ruCount < $engCount) || $type == 'T->ru'){ // Если транслитерация в русский
for ($i=0;$i<mb_strlen($word);$i++){
$currentChar = mb_substr($word,$i,1);
if(!$special && in_array($currentChar,array('\\','/'))) continue;
if (in_array($currentChar,array('J', 'T', 'S', 'Z', 'E', 'Y', 'C', 'j', 't','s', 'y', 'z', 'e', 'c'))){
$nextChar = mb_substr($word,$i+1,1);
$nnextChar = mb_substr($word,$i+2,1);
switch ($currentChar){
case 'J' :
if ($nextChar == 'O' || $nextChar == 'o') { $newChar = 'Ё'; $i++; } break;
case 'j' :
if ($nextChar == 'O' || $nextChar == 'o') { $newChar = 'ё'; $i++; } break;
case 'T' :
if ($nextChar == 'S' || $nextChar == 's') { $newChar = 'Ц'; $i++; } break;
case 't' :
if ($nextChar == 'S' || $nextChar == 's') { $newChar = 'ц'; $i++; } break;
case 'S' :
if (($nextChar == 'H' || $nextChar == 'h') && ($nnextChar == 'H' || $nnextChar == 'h')) { $newChar = 'Щ'; $i++; $i++;}
else if ($nextChar == 'H' || $nextChar == 'h') { $newChar = 'Ш'; $i++;}
break;
case 's' :
if (($nextChar == 'H' || $nextChar == 'h') && ($nnextChar == 'H' || $nnextChar == 'h')) { $newChar = 'Ш'; $i++; $i++;}
else if ($nextChar == 'H' || $nextChar == 'h') { $newChar = 'ш'; $i++;}
break;
case 'Z' :
if ($nextChar == 'H' || $nextChar == 'h') { $newChar = 'Ж'; $i++; } break;
case 'z' :
if ($nextChar == 'H' || $nextChar == 'h') { $newChar = 'ж'; $i++; } break;
case 'E' :
if ($nextChar == '-') { $newChar = 'Э'; $i++; } break;
case 'e' :
if ($nextChar == '-') { $newChar = 'э'; $i++; } break;
case 'C' :
if ($nextChar == 'H' || $nextChar == 'h') { $newChar = 'Ч'; $i++; } break;
case 'c' :
if ($nextChar == 'H' || $nextChar == 'h') { $newChar = 'ч'; $i++; } break;
case 'Y' :
if ($nextChar == 'A' || $nextChar == 'a') { $newChar = 'Я'; $i++; }
else if ($nextChar == 'U' || $nextChar == 'u') { $newChar = 'У'; $i++; }
break;
case 'y' :
if ($nextChar == 'A' || $nextChar == 'a') { $newChar = 'я'; $i++; }
else if ($nextChar == 'U' || $nextChar == 'u') { $newChar = 'у'; $i++; }
break;
}
if ($newChar) $newWord .= $newChar;
else {
$newChar = $ruTrans[array_search($currentChar,$engTrans)];
if ($newChar) $newWord .= $newChar; else $newWord .= $currentChar;
}
} else {
$newChar = $ruTrans[array_search($currentChar,$engTrans)];
if ($newChar) $newWord .= $newChar; else $newWord .= $currentChar;
}
//echo mb_substr($word,$i,1).' -> '.array_search(mb_substr($word,$i,1),$ru).' -> '.$eng[array_search(mb_substr($word,$i,1),$ru)].'<br />';
}
}
return $newWord;
}
}