Skip to content
This repository has been archived by the owner on Jun 4, 2019. It is now read-only.

Commit

Permalink
Transliteration
Browse files Browse the repository at this point in the history
  • Loading branch information
SergioMadness committed Aug 8, 2016
1 parent 9de758d commit 99446f6
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 25 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@ PWF - helpers
- SystemHelper

## v1.1 30.05.2016
- Validator
- Validator

## v1.1.1 08.08.2016
- Transliteration
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ Classes
- invokeSoap
- StringHelpers
- hashString
- translit
- SystemHelpers
- call
- functionDI
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "professionalweb/helpers",
"version": "1.1",
"version": "1.1.1",
"description": "Helpers for web-development",
"homepage": "http://web-development.pw/",
"keywords": [
Expand Down
72 changes: 72 additions & 0 deletions src/StringHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,76 @@ public static function hashString($string, $iteration = 5)

return $result;
}

/**
* Translit ru->en
*
* @param string $string
* @return string
*/
public static function translit($string)
{
$string = strtr($string,
"абвгдежзийклмнопрстуфыэАБВГДЕЖЗИЙКЛМНОПРСТУФЫЭ",
"abvgdegziyklmnoprstufieABVGDEGZIYKLMNOPRSTUFIE"
);
$string = strtr($string,
array(
'ё' => "yo", 'х' => "h", 'ц' => "ts", 'ч' => "ch", 'ш' => "sh",
'щ' => "shch", 'ъ' => '', 'ь' => '', 'ю' => "yu", 'я' => "ya",
'Ё' => "Yo", 'Х' => "H", 'Ц' => "Ts", 'Ч' => "Ch", 'Ш' => "Sh",
'Щ' => "Shch", 'Ъ' => '', 'Ь' => '', 'Ю' => "Yu", 'Я' => "Ya",
));
return $string;
}

/**
* Translit
*
* @param string $string
* @return string
*/
public static function rus2translit($string)
{
$converter = array(
'а' => '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', 'Щ' => 'Sch',
'Ь' => '\'', 'Ы' => 'Y', 'Ъ' => '\'',
'Э' => 'E', 'Ю' => 'Yu', 'Я' => 'Ya',
);
return strtr($string, $converter);
}

/**
* String to url
*
* @param string $str
* @return string
*/
public static function str2url($str)
{
$str = self::rus2translit($str);
$str = strtolower($str);
$str = preg_replace('~[^-a-z0-9_]+~u', '-', $str);
$str = trim($str, "-");
return $str;
}
}
24 changes: 1 addition & 23 deletions tests/_support/_generated/UnitTesterActions.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php //[STAMP] cd1ed91832ba84b392dbe76282fdde49
<?php //[STAMP] f407b406803e6d1c156044aa7938ee27
namespace _generated;

// This class was automatically generated by build task
Expand Down Expand Up @@ -92,17 +92,6 @@ public function assertGreaterThan($expected, $actual, $message = null) {
}


/**
* [!] Method is generated. Documentation taken from corresponding module.
*
* @deprecated
* @see \Codeception\Module\Asserts::assertGreaterThen()
*/
public function assertGreaterThen($expected, $actual, $message = null) {
return $this->getScenario()->runStep(new \Codeception\Step\Action('assertGreaterThen', func_get_args()));
}


/**
* [!] Method is generated. Documentation taken from corresponding module.
*
Expand All @@ -118,17 +107,6 @@ public function assertGreaterThanOrEqual($expected, $actual, $message = null) {
}


/**
* [!] Method is generated. Documentation taken from corresponding module.
*
* @deprecated
* @see \Codeception\Module\Asserts::assertGreaterThenOrEqual()
*/
public function assertGreaterThenOrEqual($expected, $actual, $message = null) {
return $this->getScenario()->runStep(new \Codeception\Step\Action('assertGreaterThenOrEqual', func_get_args()));
}


/**
* [!] Method is generated. Documentation taken from corresponding module.
*
Expand Down
12 changes: 12 additions & 0 deletions tests/unit/HelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,18 @@ public function testRecursivelySetValue()
\pwf\helpers\ArrayHelper::recursivelyGetValue('key12', $arr));
}

public function testTranslit()
{
$this->assertEquals('test',
\pwf\helpers\StringHelpers::rus2translit('тест'));
}

public function testUrlTranslit()
{
$this->assertEquals('test-test',
\pwf\helpers\StringHelpers::str2url('тест-тест'));
}

public function testXml2Array()
{
$xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<root><key1>value1</key1><key2>value2</key2></root>\n";
Expand Down

0 comments on commit 99446f6

Please sign in to comment.