-
Notifications
You must be signed in to change notification settings - Fork 0
/
NameConverterLT.php
63 lines (54 loc) · 1.53 KB
/
NameConverterLT.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
declare(strict_types=1);
class NameConverterLT
{
private array $ending = [
'ius' => 'iaus',
'ienė' => 'ienės',
'aitis' => 'aičio',
'aitė' => 'aitės',
'ė' => 'ės',
'as' => 'o',
'is' => 'io',
'a' => 'os',
];
/**
* Pakeičiamos frazės galūnės:
* direktorius Vardenis Pavardenis
* ||
* \/
* direktoriaus Vardenio Pavardenio.
*/
public function convertString(?string $string): string
{
if (is_null($string)) {
return '';
}
$phrase = explode(' ', $string);
foreach ($phrase as $item => $value) {
if (strpos($value, '-')) {
$name = explode('-', $value);
foreach ($name as $nam => $val) {
$name[$nam] = $this->changeName($val);
}
$phrase[$item] = implode('-', $name);
} else {
$phrase[$item] = $this->changeName($value);
}
}
return implode(' ', $phrase);
}
private function changeName(string $name): string
{
$word = \str_split($name);
for ($i = 5; $i > 0; --$i) {
$str = \implode('', \array_slice($word, -$i, $i));
$base = \array_slice($word, 0, -$i);
if (\array_key_exists($str, $this->ending)) {
$base[] = $this->ending[$str];
return implode('', $base);
}
}
return $name;
}
}