Skip to content

Commit

Permalink
By default spaces will not be replaced + slightly refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
ElForastero committed Jun 10, 2017
1 parent 474a631 commit 04ac0c9
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 66 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea/
193 changes: 128 additions & 65 deletions src/Transliteration.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,35 @@
<?php

namespace ElForastero\Transliterate;

/**
* Feel free to change it.
* Either by pull request or forking.
*
* Class Transliteration
* @package ElForastero\Transliterate
*/
class Transliteration
{
/**
* Default transliteration map
* @var string common|gost2000
*/
private static $map = 'common';

/**
* Default transliteration method
* @var string text|filename|url
*/
private static $type = 'text';

/**
* Lowercase string or not
* @var bool
*/
private static $lowercase = false;


/**
* @param $string
* @param array $params Массив с параметрами.
Expand All @@ -13,69 +40,41 @@ class Transliteration
*/
public static function make($string, array $params = [])
{
if(!isset($params['map'])) {
$map = self::getDefaultMap();
} else {
switch($params['map']) {
case 'gost2000':
$map = self::getGost2000Map();
break;
default:
$map = self::getDefaultMap();
break;
}
}
self::parseParameters($params);

$map = self::getTransliterationMap();
$chars = implode('', array_keys($map));
$clearedString = preg_replace("/[^\\s\\w$chars]/iu", '', $string);
$clearedString = preg_replace("/[^\\s\\w${chars}]/iu", '', $string);
$transliterated = str_replace(array_keys($map), array_values($map), $clearedString);

if(isset($params['type'])) {
switch($params['type']) {
case 'url':
$transliterated = preg_replace('/\s+/', '-', $transliterated);
break;
case 'filename':
$transliterated = preg_replace('/\s+/', '_', $transliterated);
break;
}
} else {
$transliterated = preg_replace('/\s+/', '-', $transliterated);
}

if(isset($params['lowercase']) && $params['lowercase']) {
$transliterated = strtolower($transliterated);
}

return trim($transliterated, '_-');
return self::applyTransformer($transliterated);
}

/**
* Транслитерация общепринятым способом
* @return array
*/
public static function getDefaultMap()
public static function getCommonMap()
{
return [
'ж' => 'zh', 'ч' => 'ch', 'щ' => 'shh', 'ш' => 'sh',
'ю' => 'yu', 'ё' => 'yo', 'я' => 'ya', 'э' => 'e',
'а' => 'a', 'б' => 'b', 'в' => 'v',
'г' => 'g', 'д' => 'd', 'е' => 'e', 'з' => 'z',
'и' => 'i', 'й' => 'y', 'к' => 'k', 'л' => 'l',
'м' => 'm', 'н' => 'n', 'о' => 'o', 'п' => 'p',
'р' => 'r', 'с' => 's', 'т' => 't', 'у' => 'u',
'ф' => 'f', 'х' => 'h', 'ц' => 'c', 'ъ' => '',
'ь' => '', 'ы' => 'i',
'ю' => 'yu', 'ё' => 'yo', 'я' => 'ya', 'э' => 'e',
'а' => 'a', 'б' => 'b', 'в' => 'v',
'г' => 'g', 'д' => 'd', 'е' => 'e', 'з' => 'z',
'и' => 'i', 'й' => 'y', 'к' => 'k', 'л' => 'l',
'м' => 'm', 'н' => 'n', 'о' => 'o', 'п' => 'p',
'р' => 'r', 'с' => 's', 'т' => 't', 'у' => 'u',
'ф' => 'f', 'х' => 'h', 'ц' => 'c', 'ъ' => '',
'ь' => '', 'ы' => 'i',

'Ж' => 'Zh', 'Ч' => 'Ch', 'Щ' => 'Shh', 'Ш' => 'Sh',
'Ю' => 'Yu', 'Ё' => 'Yo', 'Я' => 'Ya', 'Э' => 'E',
'А' => 'A', 'Б' => 'B', 'В' => 'V',
'Г' => 'G', 'Д' => 'D', 'Е' => 'E', 'З' => 'Z',
'И' => 'I', 'Й' => 'Y', 'К' => 'K', 'Л' => 'L',
'М' => 'M', 'Н' => 'N', 'О' => 'O', 'П' => 'P',
'Р' => 'R', 'С' => 'S', 'Т' => 'T', 'У' => 'U',
'Ф' => 'F', 'Х' => 'H', 'Ц' => 'C', 'Ъ' => '',
'Ь' => '', 'Ы' => 'I',
'Ю' => 'Yu', 'Ё' => 'Yo', 'Я' => 'Ya', 'Э' => 'E',
'А' => 'A', 'Б' => 'B', 'В' => 'V',
'Г' => 'G', 'Д' => 'D', 'Е' => 'E', 'З' => 'Z',
'И' => 'I', 'Й' => 'Y', 'К' => 'K', 'Л' => 'L',
'М' => 'M', 'Н' => 'N', 'О' => 'O', 'П' => 'P',
'Р' => 'R', 'С' => 'S', 'Т' => 'T', 'У' => 'U',
'Ф' => 'F', 'Х' => 'H', 'Ц' => 'C', 'Ъ' => '',
'Ь' => '', 'Ы' => 'I',
];
}

Expand All @@ -87,24 +86,88 @@ public static function getGost2000Map()
{
return [
'ж' => 'zh', 'ч' => 'ch', 'щ' => 'shh', 'ш' => 'sh',
'ю' => 'yu', 'ё' => 'yo', 'я' => 'ya', 'э' => 'e`',
'а' => 'a', 'б' => 'b', 'в' => 'v',
'г' => 'g', 'д' => 'd', 'е' => 'e', 'з' => 'z',
'и' => 'i', 'й' => 'j', 'к' => 'k', 'л' => 'l',
'м' => 'm', 'н' => 'n', 'о' => 'o', 'п' => 'p',
'р' => 'r', 'с' => 's', 'т' => 't', 'у' => 'u',
'ф' => 'f', 'х' => 'x', 'ц' => 'c', 'ъ' => '``',
'ь' => '`', 'ы' => 'y\'',
'ю' => 'yu', 'ё' => 'yo', 'я' => 'ya', 'э' => 'e`',
'а' => 'a', 'б' => 'b', 'в' => 'v',
'г' => 'g', 'д' => 'd', 'е' => 'e', 'з' => 'z',
'и' => 'i', 'й' => 'j', 'к' => 'k', 'л' => 'l',
'м' => 'm', 'н' => 'n', 'о' => 'o', 'п' => 'p',
'р' => 'r', 'с' => 's', 'т' => 't', 'у' => 'u',
'ф' => 'f', 'х' => 'x', 'ц' => 'c', 'ъ' => '``',
'ь' => '`', 'ы' => 'y\'',

'Ж' => 'Zh', 'Ч' => 'Ch', 'Щ' => 'Shh', 'Ш' => 'Sh',
'Ю' => 'Yu', 'Ё' => 'Yo', 'Я' => 'Ya', 'Э' => 'E`',
'А' => 'A', 'Б' => 'B', 'В' => 'V',
'Г' => 'G', 'Д' => 'D', 'Е' => 'E', 'З' => 'Z',
'И' => 'I', 'Й' => 'J', 'К' => 'K', 'Л' => 'L',
'М' => 'M', 'Н' => 'N', 'О' => 'O', 'П' => 'P',
'Р' => 'R', 'С' => 'S', 'Т' => 'T', 'У' => 'U',
'Ф' => 'F', 'Х' => 'X', 'Ц' => 'C', 'Ъ' => '``',
'Ь' => '`', 'Ы' => 'Y\'',
'Ю' => 'Yu', 'Ё' => 'Yo', 'Я' => 'Ya', 'Э' => 'E`',
'А' => 'A', 'Б' => 'B', 'В' => 'V',
'Г' => 'G', 'Д' => 'D', 'Е' => 'E', 'З' => 'Z',
'И' => 'I', 'Й' => 'J', 'К' => 'K', 'Л' => 'L',
'М' => 'M', 'Н' => 'N', 'О' => 'O', 'П' => 'P',
'Р' => 'R', 'С' => 'S', 'Т' => 'T', 'У' => 'U',
'Ф' => 'F', 'Х' => 'X', 'Ц' => 'C', 'Ъ' => '``',
'Ь' => '`', 'Ы' => 'Y\'',
];
}

/**
* Pareses parameters array. Just for clearance.
* @param array $parameters
*/
private static function parseParameters(array $parameters)
{
if (isset($parameters['map'])) {
if (!in_array($parameters['map'], ['common', 'gost2000'])) {
throw new \InvalidArgumentException("The 'map' parameter must be either 'common' or 'gost2000'");
}

self::$map = $parameters['map'];
}

if (isset($parameters['type'])) {
if (!in_array($parameters['type'], ['filename', 'url', 'text'])) {
throw new \InvalidArgumentException("The 'type' parameter must be one of 'filename', 'url' or 'text'");
}

self::$type = $parameters['type'];
}

if (isset($parameters['lowercase'])) {
if (!is_bool($parameters['lowercase'])) {
throw new \InvalidArgumentException("The 'lowercase' parameter must be boolean");
}

self::$lowercase = $parameters['lowercase'];
}
}

/**
* Get transliteration map
* @return array
*/
private static function getTransliterationMap()
{
if ('gost2000' === self::$map) {
return self::getGost2000Map();
}

return self::getCommonMap();
}

/**
* Applies various transformations to the string
* @param $string
* @return mixed|string
*/
private static function applyTransformer($string)
{
if ('filename' === self::$type) {
$string = preg_replace('/\s+/', '_', $string);
} else if ('url' === self::$type) {
$string = preg_replace('/\s+/', '-', $string);
}

if (true === self::$lowercase) {
$string = strtolower($string);
}

return $string;
}
}
1 change: 1 addition & 0 deletions src/TransliterationFacade.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace ElForastero\Transliterate;

use Illuminate\Support\Facades\Facade;
Expand Down
2 changes: 1 addition & 1 deletion src/TransliterationServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function boot()
*/
public function register()
{
$this->app->bind('Transliteration', function($app) {
$this->app->bind('Transliteration', function ($app) {
return new Transliteration();
});
}
Expand Down

0 comments on commit 04ac0c9

Please sign in to comment.