|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Zvermafia\Transliteration; |
|
4
|
|
|
|
|
5
|
|
|
use function GuzzleHttp\json_encode; |
|
6
|
|
|
use function GuzzleHttp\json_decode; |
|
7
|
|
|
use GuzzleHttp\ClientInterface; |
|
8
|
|
|
use Zvermafia\Transliteration\Contracts\TransliteratorContract; |
|
9
|
|
|
|
|
10
|
|
|
class Transliterator implements TransliteratorContract |
|
11
|
|
|
{ |
|
12
|
|
|
/** @var string */ |
|
13
|
|
|
const API_BASE_URI = 'http://www.alif.uz/api/'; |
|
14
|
|
|
|
|
15
|
|
|
/** @var \GuzzleHttp\ClientInterface */ |
|
16
|
|
|
private $transport; |
|
17
|
|
|
|
|
18
|
|
|
/** @var null|string */ |
|
19
|
|
|
private $text; |
|
20
|
|
|
|
|
21
|
|
|
/** @var string */ |
|
22
|
|
|
private $isLatToCyr = 'true'; |
|
23
|
|
|
|
|
24
|
|
|
/** @var null|string */ |
|
25
|
|
|
private $result; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Create a new instance. |
|
29
|
|
|
* |
|
30
|
|
|
* @param \GuzzleHttp\ClientInterface $client |
|
31
|
|
|
*/ |
|
32
|
21 |
|
public function __construct(ClientInterface $client) |
|
33
|
|
|
{ |
|
34
|
21 |
|
$this->transport = $client; |
|
35
|
21 |
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @{inheritDoc} |
|
39
|
|
|
*/ |
|
40
|
12 |
|
public function setText(string $text) : TransliteratorContract |
|
41
|
|
|
{ |
|
42
|
12 |
|
$this->text = $text; |
|
43
|
12 |
|
$this->result = null; |
|
44
|
|
|
|
|
45
|
12 |
|
return $this; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @{inheritDoc} |
|
50
|
|
|
*/ |
|
51
|
6 |
|
public function getText() : ?string |
|
52
|
|
|
{ |
|
53
|
6 |
|
return $this->text; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @{inheritDoc} |
|
58
|
|
|
*/ |
|
59
|
6 |
|
public function toLatin() : TransliteratorContract |
|
60
|
|
|
{ |
|
61
|
6 |
|
$this->isLatToCyr = 'false'; |
|
62
|
|
|
|
|
63
|
6 |
|
return $this; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @{inheritDoc} |
|
68
|
|
|
*/ |
|
69
|
6 |
|
public function toCyrillic() : TransliteratorContract |
|
70
|
|
|
{ |
|
71
|
6 |
|
$this->isLatToCyr = 'true'; |
|
72
|
|
|
|
|
73
|
6 |
|
return $this; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @{inheritDoc} |
|
78
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
|
79
|
|
|
*/ |
|
80
|
3 |
|
public function translit() : TransliteratorContract |
|
81
|
|
|
{ |
|
82
|
|
|
$data = [ |
|
83
|
3 |
|
'inputText' => $this->getText(), |
|
84
|
3 |
|
'isLatToCyr' => $this->isLatToCyr, |
|
85
|
|
|
]; |
|
86
|
|
|
|
|
87
|
3 |
|
$response = $this->transport |
|
88
|
3 |
|
->request('POST', 'translit', [ |
|
89
|
3 |
|
'headers' => [ |
|
90
|
|
|
'Accept' => 'application/json', |
|
91
|
|
|
'Content-Type' => 'application/json', |
|
92
|
|
|
], |
|
93
|
3 |
|
'body' => json_encode($data), |
|
94
|
|
|
]); |
|
95
|
|
|
|
|
96
|
3 |
|
$this->result = (json_decode($response->getBody()))->OutputText; |
|
97
|
|
|
|
|
98
|
3 |
|
return $this; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @{inheritDoc} |
|
103
|
|
|
*/ |
|
104
|
9 |
|
public function getResult() : ?string |
|
105
|
|
|
{ |
|
106
|
9 |
|
return $this->result; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|