1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of riesenia/pohoda package. |
5
|
|
|
* |
6
|
|
|
* Licensed under the MIT License |
7
|
|
|
* (c) RIESENIA.com |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
namespace Riesenia\Pohoda\ValueTransformer; |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* A transformer that rewrites Cyrillic script to its Latin alphabet equivalent. |
18
|
|
|
* |
19
|
|
|
* Note: This transformation is a phonetic representation and does not provide a translation of the text. |
20
|
|
|
*/ |
21
|
|
|
class CyrillicTransliterationTransformer implements ValueTransformerInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* {@inheritdoc} |
25
|
|
|
*/ |
26
|
4 |
|
public function transform(string $value): string |
27
|
|
|
{ |
28
|
4 |
|
$normalized = \Normalizer::normalize($value, \Normalizer::FORM_C); |
29
|
|
|
|
30
|
4 |
|
if (false === $normalized) { |
31
|
|
|
// @codeCoverageIgnoreStart |
32
|
|
|
// must do some error with Normalizer class |
33
|
|
|
return $value; |
34
|
|
|
} |
35
|
|
|
// @codeCoverageIgnoreEnd |
36
|
|
|
|
37
|
4 |
|
$transformer = \Transliterator::create('Any-Latin; Latin-ASCII'); |
38
|
|
|
|
39
|
4 |
|
if (is_null($transformer)) { |
40
|
|
|
// @codeCoverageIgnoreStart |
41
|
|
|
// this can happen only if there is no latin encoding installed - not happen with Pohoda which need that |
42
|
|
|
return $value; |
43
|
|
|
} |
44
|
|
|
// @codeCoverageIgnoreEnd |
45
|
|
|
|
46
|
4 |
|
$chars = \preg_split('//u', $normalized, -1, PREG_SPLIT_NO_EMPTY); |
47
|
|
|
|
48
|
4 |
|
if (false === $chars) { |
49
|
|
|
// @codeCoverageIgnoreStart |
50
|
|
|
// can happen only when something in preg_split fails |
51
|
|
|
return $value; |
52
|
|
|
} |
53
|
|
|
// @codeCoverageIgnoreEnd |
54
|
|
|
|
55
|
4 |
|
$result = ''; |
56
|
|
|
|
57
|
4 |
|
foreach ($chars as $char) { |
58
|
4 |
|
$codePoint = \mb_ord($char, 'UTF-8'); |
59
|
|
|
|
60
|
4 |
|
if ((0x00400 <= $codePoint && 0x004FF >= $codePoint) // Cyrillic Basic |
61
|
4 |
|
|| (0x00500 <= $codePoint && 0x0052F >= $codePoint) // Cyrillic Supplement |
62
|
4 |
|
|| (0x02DE0 <= $codePoint && 0x02DFF >= $codePoint) // Cyrillic Extended-A |
63
|
4 |
|
|| (0x0A640 <= $codePoint && 0x0A69F >= $codePoint) // Cyrillic Extended-B |
64
|
4 |
|
|| (0x01C80 <= $codePoint && 0x01C8F >= $codePoint) // Cyrillic Extended-C |
65
|
4 |
|
|| (0x1E030 <= $codePoint && 0x1E08F >= $codePoint) // Cyrillic Extended-D |
66
|
|
|
) { |
67
|
1 |
|
$result .= $transformer->transliterate($char); |
68
|
|
|
} else { |
69
|
4 |
|
$result .= $char; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
4 |
|
return $result; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|