-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
15 changed files
with
409 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
FROM php:7.1 | ||
|
||
RUN apt-get update && \ | ||
apt-get install -y git zip unzip && \ | ||
php -r "readfile('http://getcomposer.org/installer');" | php -- --install-dir=/usr/bin/ --filename=composer && \ | ||
apt-get -y autoremove && \ | ||
apt-get clean && \ | ||
rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
version: "3" | ||
|
||
services: | ||
php: | ||
build: . | ||
volumes: | ||
- ./:/srv/app | ||
working_dir: /srv/app | ||
command: bash -c "composer install && composer test" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: elforastero | ||
* Date: 12/3/18 | ||
* Time: 10:08 PM | ||
*/ | ||
|
||
namespace ElForastero\Transliterate; | ||
|
||
|
||
class Map | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ElForastero\Transliterate; | ||
|
||
/** | ||
* Feel free to change it. | ||
* Either by pull request or forking. | ||
* | ||
* Class Transliteration | ||
* | ||
* @author Eugene Dzhumak <elforastero@ya.ru> | ||
* | ||
* @version 2.0.0 | ||
*/ | ||
class Transliterator | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $lang; | ||
/** | ||
* @var | ||
*/ | ||
private $map; | ||
|
||
/** | ||
* @param string $lang | ||
* @return Transliterator | ||
*/ | ||
public function from(string $lang): self | ||
{ | ||
$this->lang = $lang; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @param string $map | ||
* @return Transliterator | ||
*/ | ||
public function useMap(string $map): self | ||
{ | ||
$this->map = $map; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Transliterate the given string. | ||
* | ||
* @param string $text | ||
* | ||
* @return string | ||
*/ | ||
public function make(string $text): string | ||
{ | ||
$map = $this->getMap(); | ||
$transliterated = str_replace(array_keys($map), array_values($map), $text); | ||
|
||
if (config('transliterate.remove_accents', false) === true) { | ||
$transliterated = iconv('UTF-8', 'ASCII//IGNORE//TRANSLIT', $transliterated); | ||
} | ||
|
||
return self::applyTransformers($transliterated); | ||
} | ||
|
||
/** | ||
* Get map array according to config file. | ||
* | ||
* @return array | ||
*/ | ||
private function getMap(): array | ||
{ | ||
$map = $this->map ?? config('transliterate.default_map'); | ||
$lang = $this->lang ?? config('transliterate.default_lang'); | ||
$customMaps = config('transliterate.custom_maps'); | ||
|
||
$vendorMapsPath = __DIR__ . DIRECTORY_SEPARATOR . 'maps' . DIRECTORY_SEPARATOR; | ||
$path = $customMaps[$lang][$map] ?? $vendorMapsPath . $lang . DIRECTORY_SEPARATOR . $map . '.php'; | ||
|
||
if (!file_exists($path)) { | ||
throw new \InvalidArgumentException("The transliteration map '${path}' doesn't exist"); | ||
} | ||
|
||
/** @noinspection PhpIncludeInspection */ | ||
return require $path; | ||
} | ||
|
||
/** | ||
* Apply a series of transformations defined as closures in the configuration file. | ||
* | ||
* @param string $string | ||
* | ||
* @return string | ||
*/ | ||
private function applyTransformers(string $string): string | ||
{ | ||
foreach (Transformer::getAll() as $transformer) { | ||
$string = $transformer($string); | ||
} | ||
|
||
return $string; | ||
} | ||
} |
Oops, something went wrong.