Skip to content

Commit

Permalink
Merge pull request #7 from ashtokalo/namespace
Browse files Browse the repository at this point in the history
add namespaces and tests
  • Loading branch information
ashtokalo authored Sep 26, 2022
2 parents a3d348e + f18ef99 commit 8ced36c
Show file tree
Hide file tree
Showing 21 changed files with 223 additions and 67 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.idea
build
vendor
composer.lock

76 changes: 68 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,24 @@ Currently it assume conversion of Cyrillic, Georgian, Armenian and Greek
scripts into Roman (Latin). Cyrillic has sub-groups for Russian, Ukrainian,
Belarusian, Bulgarian and Kazakh languages with custom rules.

## Installation

This package can be installed through Composer:

```sh
$ composer require ashtokalo/php-translit
```

Make sure to use Composer's autoload:

```php
require __DIR__.'/../vendor/autoload.php';
```

## Usage

Main class Translit could be used as singleton or object itself. Built-in
translitaration tables could be refered by following language codes:
transliteration tables could be referred by following language codes:
* ru - Russian cyrillic chars,
* uk - Ukrainian cyrillic chars,
* mk - Macedonian cyrillic chars,
Expand All @@ -23,12 +39,25 @@ translitaration tables could be refered by following language codes:

Language codes could be combined by comma to handle more cases, e.g.

echo Translit::object()->convert('Беларусь', 'be') . ' vs ' .
Translit::object()->convert('Беларусь', 'be,latin');
```php
echo \ashtokalo\translit\Translit::object()->convert('Беларусь', 'be') . ' vs ' .
\ashtokalo\translit\Translit::object()->convert('Беларусь', 'be,latin');
```

produce output:

Bielaruś vs Bielarus
```
Bielaruś vs Bielarus
```

You can also add alternative transliteration tables through property `classes`,
which is list of language code in keys and class names in values:

```php
$translit = new \ashtokalo\translit\Translit;
$translit->classes['tlh'] = \startrek\TranslitKlingon::class;
echo $translit->convert(' ', 'tlh');
```

By default wrong language codes ignored. But this behavior could be changed by
using strict mode. For all language codes that prepended with exclamation mark
Expand All @@ -41,11 +70,42 @@ For example:
// but next code fires Exception, because strict mode have used
echo Translit::object()->convert('Привет', '!ru_ru') . PHP_EOL;

There are many sources of these tables which could be used - ISO and BGN/PCGN
standards, a lot of native standards and informal standard used by people.
For this library I assume next order of sources - native, ISO, BGN/PCGN,
informal.
## Tests

The package contains integration tests. You can run them using PHPUnit.

```sh
$ vendor/bin/phpunit
```

## Credits

All transliteration tables were created from information found at Wikipedia. The
links to these pages posted into header of each file. Please update me if any
mistakes found or you have new transliteration tables to add here.

There are many sources of these tables which could be used - ISO and BGN/PCGN
standards, a lot of native standards and informal standard used by people.
For this library I assume next order of sources - native, ISO, BGN/PCGN, informal.

## Contributing

Contributions are very welcome.

Only contributions via Pull Requests on [Github](https://github.com/ashtokalo/php-translit) is accepted:

- **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)**

- **Document any change in behaviour** - Make sure the `README.md` and any other relevant
documentation are kept up-to-date.

- **Create feature branches** - Don't ask me to pull from your master branch.

- **One pull request per feature** - If you want to do more than one thing, send multiple pull requests.

- **Send coherent history** - Make sure each individual commit in your pull request is meaningful.
If you had to make multiple intermediate commits while developing, please [squash them](http://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting.

## License

The MIT License (MIT). Refer to the [License](LICENSE) for more information.
12 changes: 11 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"keywords": ["transliteration", "romanization", "latinization", "translit"],
"description": "PHP library to convert text from one script to another.",
"license": "MIT",
"version": "0.2.0",
"author":
{
"name": "Alexey Shtokalo",
Expand All @@ -13,6 +14,15 @@
},
"require":
{
"php": ">=5.2.0"
"php": ">=7.0"
},
"require-dev":
{
"phpunit/phpunit": "~7.0"
},
"autoload": {
"psr-4": {
"ashtokalo\\translit\\": "src/"
}
}
}
28 changes: 28 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="./vendor/autoload.php"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
verbose="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="TestMonitor Test Suite">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src</directory>
</whitelist>
</filter>
<logging>
<log type="tap" target="build/report.tap"/>
<log type="junit" target="build/report.junit.xml"/>
<log type="coverage-text" target="build/coverage.txt"/>
<log type="coverage-clover" target="build/logs/clover.xml"/>
</logging>
</phpunit>
101 changes: 43 additions & 58 deletions Translit.php → src/Translit.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<?php

namespace ashtokalo\translit;

use Exception;

/**
* Translit
*
Expand Down Expand Up @@ -52,11 +56,14 @@ class Translit
public $dataPath = '';

/**
* Path to directory with transliteration classes.
*
* @var string empty string by default assumes directory with the class
* @var array list of classes used to transliterate language
*/
public $classPath = '';
public $classes = [
'be' => TranslitBe::class,
'ka' => TranslitKa::class,
'uk' => TranslitUk::class,
'ascii' => TranslitAscii::class,
];

/**
* Converts given text to the Roman (Latin) script.
Expand All @@ -72,7 +79,7 @@ class Translit
* @return string converted text
* @throws Exception if handler not available or wrong (strict mode only)
*/
public function convert($text, $code)
public function convert(string $text, string $code): string
{
foreach (explode(',', $code) as $code)
{
Expand All @@ -98,7 +105,7 @@ public function convert($text, $code)
*
* @return Translit
*/
public static function object($dataPath = '')
public static function object(string $dataPath = '')
{
static $object = null;

Expand All @@ -111,6 +118,11 @@ public static function object($dataPath = '')
return $object;
}

public function __invoke()
{
print_r(func_get_args());
}

/**
* Returns language handler - array or object.
*
Expand All @@ -123,7 +135,7 @@ public static function object($dataPath = '')
*
* @throws Exception if handler not available or wrong (strict mode only)
*/
protected function getLanguage($code)
protected function getLanguage(string $code)
{
// all language codes prepended with exclamation mark really required
$strict = false;
Expand All @@ -135,11 +147,25 @@ protected function getLanguage($code)

if ($code && !isset($this->languages[$code]))
{
$dataFile = $this->getDataPath() . $code . '.php';
$className = 'Translit' . ucfirst($code);
$classFile = $this->getClassPath() . $className . '.php';

if (file_exists($dataFile))
if (class_exists($className = isset($this->classes[$code]) ? $this->classes[$code] : ''))
{
$language = new $className;
if (method_exists($language, 'convert'))
{
if (property_exists($language, 'translit'))
{
$language->translit = $this;
}
$this->languages[$code] = $language;
}
else if ($strict)
{
throw new Exception(
sprintf('class "%s" does not have convert() method',
$className));
}
}
else if (file_exists($dataFile = $this->getDataPath() . $code . '.php'))
{
$language = include $dataFile;
if (is_array($language))
Expand All @@ -156,36 +182,10 @@ protected function getLanguage($code)
gettype($language)));
}
}
else
else if ($strict)
{
if (!@class_exists($className) && file_exists($classFile))
{
include $classFile;
}

if (@class_exists($className))
{
$language = new $className;
if (method_exists($language, 'convert'))
{
if (property_exists($language, 'translit'))
{
$language->translit = $this;
}
$this->languages[$code] = $language;
}
else if ($strict)
{
throw new Exception(
sprintf('class "%s" does not have convert() method',
$className));
}
}
else if ($strict)
{
throw new Exception(
sprintf('language "%s" does not have handlers', $code));
}
throw new Exception(
sprintf('language "%s" does not have handlers', $code));
}
}

Expand All @@ -197,32 +197,17 @@ protected function getLanguage($code)
*
* @return string
*/
protected function getDataPath()
protected function getDataPath(): string
{
if (!$this->dataPath)
{
$this->dataPath = dirname(__FILE__) . DIRECTORY_SEPARATOR .
$this->dataPath = __DIR__ . DIRECTORY_SEPARATOR .
'data' . DIRECTORY_SEPARATOR;
}

return $this->dataPath;
}

/**
* Returns path to directory with transliteration classes
*
* @return string
*/
protected function getClassPath()
{
if (!$this->classPath)
{
$this->classPath = dirname(__FILE__) . DIRECTORY_SEPARATOR;
}

return $this->classPath;
}

/**
* Cached language handlers, could be an associative array or object.
*
Expand Down
2 changes: 2 additions & 0 deletions TranslitAscii.php → src/TranslitAscii.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

namespace ashtokalo\translit;

/**
* Transliteration data to clean ASCII
*
Expand Down
2 changes: 2 additions & 0 deletions TranslitBe.php → src/TranslitBe.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

namespace ashtokalo\translit;

/**
* Transliteration data for Belarusian (BE)
*
Expand Down
2 changes: 2 additions & 0 deletions TranslitKa.php → src/TranslitKa.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

namespace ashtokalo\translit;

/**
* Transliteration data for Georgian (KA)
*
Expand Down
2 changes: 2 additions & 0 deletions TranslitUk.php → src/TranslitUk.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

namespace ashtokalo\translit;

/**
* Transliteration data for Ukrainian (UK)
*
Expand Down
File renamed without changes.
Loading

0 comments on commit 8ced36c

Please sign in to comment.