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