Ascii::isValid()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace BFunky\Ascii;
4
5
class Ascii
6
{
7
    /**
8
     * @param string $stringToCheck
9
     * @return bool
10
     */
11
    public static function isValid(string $stringToCheck): bool
12
    {
13
        return (!self::existsTransliterableChars($stringToCheck));
14
    }
15
16
    /**
17
     * @return bool
18
     */
19
    private static function existsTransliterableChars(string $stringToCheck): bool
20
    {
21
        return !preg_match('@^[a-zA-Z0-9\s\p{P}\p{S}]+$@u', $stringToCheck);
22
    }
23
24
    /**
25
     * @param string $stringToTransliterate
26
     * @return string
27
     */
28
    public static function transliterate(string $stringToTransliterate): string
29
    {
30
        $transliterated = strtr($stringToTransliterate, AsciiCorrelation::MAPPED_VALUES);
31
        if (self::isValid($transliterated)) {
32
            return $transliterated;
33
        }
34
        throw new \Exception('Error on transliterate ' . $stringToTransliterate);
35
    }
36
}
37