Skip to content

Commit

Permalink
Work in progress
Browse files Browse the repository at this point in the history
Change API, add ukrainean map
  • Loading branch information
ElForastero committed Dec 4, 2018
1 parent 4371cc1 commit f34b764
Show file tree
Hide file tree
Showing 15 changed files with 409 additions and 106 deletions.
8 changes: 8 additions & 0 deletions Dockerfile
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/*
9 changes: 8 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@
},
"require": {
"php": ">=7.1",
"ext-iconv": "*",
"illuminate/support": "~5"
},
"require-dev": {
"phpunit/phpunit": "^7.4",
"orchestra/testbench": "^3.7",
"friendsofphp/php-cs-fixer": "^2.13"
"friendsofphp/php-cs-fixer": "^2.13",
"brainmaestro/composer-git-hooks": "^2.6"
},
"extra": {
"laravel": {
Expand All @@ -37,6 +39,11 @@
"aliases": {
"Transliteration": "ElForastero\\Transliterate\\Facade"
}
},
"hooks": {
"pre-commit": [
"php-cs-fixer fix . --rules=@Symfony"
]
}
},
"scripts": {
Expand Down
72 changes: 70 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions docker-compose.yml
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"

15 changes: 15 additions & 0 deletions src/Map.php
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
{

}
2 changes: 1 addition & 1 deletion src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function register()
$this->mergeConfigFrom($configPath, 'transliterate');

$this->app->bind('Transliteration', function ($app) {
return new Transliteration();
return new Transliterator();
});
}
}
78 changes: 0 additions & 78 deletions src/Transliteration.php

This file was deleted.

106 changes: 106 additions & 0 deletions src/Transliterator.php
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;
}
}
Loading

0 comments on commit f34b764

Please sign in to comment.