Skip to content

Commit

Permalink
use intl to transliterate filename
Browse files Browse the repository at this point in the history
  • Loading branch information
linniksa committed Mar 19, 2019
1 parent 011ef92 commit 90499c3
Showing 1 changed file with 3 additions and 68 deletions.
71 changes: 3 additions & 68 deletions Services/FileUploader.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class FileUploader
private $path;
private $allowedTypes;
private $router;
private $translator;

/**
* Constructor
Expand All @@ -35,6 +36,7 @@ public function __construct(
$this->path = $path;
$this->allowedTypes = $allowedTypes;
$this->router = $router;
$this->translator = \Transliterator::create('Any-Latin;Latin-ASCII;Lower;[\u0080-\u7fff] remove');
}

/**
Expand Down Expand Up @@ -84,7 +86,7 @@ public function generateNameByOriginal($originalName)
preg_replace(
'/\s+/',
'-',
$this->translit($originalName)
$this->translator->transliterate($originalName)
)
);
}
Expand Down Expand Up @@ -162,73 +164,6 @@ public function listFiles()
return $files;
}

/**
* Encode windows to utf-8
*
* @param object $s string for encoding
*
* @return string
*/
private static function win2utf($s)
{
return iconv('cp1251', 'utf-8//IGNORE', $s);
}

/**
* Encode utf-8 to windows
*
* @param string $s string for encoding
*
* @return string
*/
private static function utf2win($s)
{
return iconv('utf-8', 'cp1251//IGNORE', $s);
}

/**
* Return ru-en translited string
*
* @param string $var message string for transliteration
*
* @return string
*/
public function translit($var)
{
$var = strtolower(trim(self::utf2win($var)));

$cyr = array_map(
function ($elem) {
return self::utf2win($elem);
},
[
'а', 'б', 'в', 'г', 'д', 'е', 'ё', 'ж', 'з', 'и', 'й', 'к', 'л', 'м',
'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'х', 'ц', 'ч', 'ш', 'щ', 'ъ',
'ы', 'ь', 'э', 'ю', 'я', 'А', 'Б', 'В', 'Г', 'Д', 'Е', 'Ё', 'Ж', 'З',
'И', 'Й', 'К', 'Л', 'М', 'Н', 'О', 'П', 'Р', 'С', 'Т', 'У', 'Ф', 'Х',
'Ц', 'Ч', 'Ш', 'Щ', 'Ъ', 'Ы', 'Ь', 'Э', 'Ю', 'Я'
]
);

$lat = [
'a', 'b', 'v', 'g', 'd', 'e', 'e', 'zh', 'z', 'i', 'y', 'k', 'l', 'm',
'n', 'o', 'p', 'r', 's', 't', 'u', 'f', 'h', 'c', 'ch', 'sh', 'sch', '',
'y', '', 'e', 'yu', 'ya', 'a', 'b', 'v', 'g', 'd', 'e', 'e', 'zh', 'z',
'i', 'y', 'k', 'l', 'm', 'n', 'o', 'p', 'r', 's', 't', 'u', 'f', 'h',
'c', 'ch', 'sh', 'sz', '', 'y', '', 'e', 'yu', 'ya'
];

$var = str_replace($cyr, $lat, $var);
$var = str_replace('-', ' ', $var);
$var = str_replace('/', ' ', $var);

$var = preg_replace('/[^a-z0-9-\.]+/', ' ', $var);
$var = preg_replace('/(\s+)/', '-', trim($var));
$var = self::win2utf($var);

return $var;
}

public function getFilesystem()
{
return $this->filesystem;
Expand Down

0 comments on commit 90499c3

Please sign in to comment.