CyrillicTransliterationTransformer   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 17
eloc 23
c 2
b 0
f 0
dl 0
loc 53
ccs 19
cts 19
cp 1
rs 10

1 Method

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