1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Zvermafia\Transliteration\Abstracts; |
4
|
|
|
|
5
|
|
|
use Zvermafia\Transliteration\Interfaces\TransliteratorInterface; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Base transliterator class |
9
|
|
|
* |
10
|
|
|
* This base class should be extended by all transliterators. |
11
|
|
|
* It enforces implementaion of the TransliteratorInterface interface and |
12
|
|
|
* defines various common attributes and methos that all transliterators should have. |
13
|
|
|
*/ |
14
|
|
|
abstract class TransliteratorAbstract implements TransliteratorInterface |
15
|
|
|
{ |
16
|
|
|
/** @var string|null */ |
17
|
|
|
protected $text = null; |
18
|
|
|
|
19
|
|
|
/** @var bool Determine transliteration direction is to cyrillic or not */ |
20
|
|
|
protected $is_to_cyrillic = true; |
21
|
|
|
|
22
|
|
|
/** @var string|null */ |
23
|
|
|
protected $result = null; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @inheritDoc |
27
|
|
|
*/ |
28
|
30 |
|
public function setText(string $text): TransliteratorInterface |
29
|
|
|
{ |
30
|
30 |
|
$this->text = $text; |
31
|
30 |
|
$this->clearResult(); |
32
|
|
|
|
33
|
30 |
|
return $this; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @inheritDoc |
38
|
|
|
*/ |
39
|
12 |
|
public function getText(): ?string |
40
|
|
|
{ |
41
|
12 |
|
return $this->text; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @inheritDoc |
46
|
|
|
*/ |
47
|
12 |
|
public function toLatin(): TransliteratorInterface |
48
|
|
|
{ |
49
|
12 |
|
$this->is_to_cyrillic = false; |
50
|
|
|
|
51
|
12 |
|
return $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @inheritDoc |
56
|
|
|
*/ |
57
|
6 |
|
public function isToLatin(): bool |
58
|
|
|
{ |
59
|
6 |
|
return ! $this->is_to_cyrillic; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @inheritDoc |
64
|
|
|
*/ |
65
|
18 |
|
public function toCyrillic(): TransliteratorInterface |
66
|
|
|
{ |
67
|
18 |
|
$this->is_to_cyrillic = true; |
68
|
|
|
|
69
|
18 |
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @inheritDoc |
74
|
|
|
*/ |
75
|
6 |
|
public function isToCyrillic(): bool |
76
|
|
|
{ |
77
|
6 |
|
return $this->is_to_cyrillic; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Set the transliterated text result. |
82
|
|
|
* |
83
|
|
|
* @param string $text |
84
|
|
|
* @return void |
85
|
|
|
*/ |
86
|
12 |
|
protected function setResult(string $text): void |
87
|
|
|
{ |
88
|
12 |
|
$this->result = $text; |
89
|
12 |
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @inheritDoc |
93
|
|
|
*/ |
94
|
18 |
|
public function getResult(): ?string |
95
|
|
|
{ |
96
|
18 |
|
return $this->result; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Clear result text. |
101
|
|
|
* |
102
|
|
|
* When set a new text there is not yet a result text for the new text, |
103
|
|
|
* so we have to clear the result text to avoid a wrong result. |
104
|
|
|
* |
105
|
|
|
* @return void |
106
|
|
|
*/ |
107
|
30 |
|
protected function clearResult(): void |
108
|
|
|
{ |
109
|
30 |
|
$this->result = null; |
110
|
30 |
|
} |
111
|
|
|
} |
112
|
|
|
|