Passed
Push — master ( 7a6456...7c7ee4 )
by Mokhirjon
01:29
created

Transliterator   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 27
c 3
b 0
f 0
dl 0
loc 96
ccs 26
cts 26
cp 1
rs 10
wmc 7

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getResult() 0 3 1
A translit() 0 19 1
A toCyrillic() 0 5 1
A setText() 0 6 1
A getText() 0 3 1
A __construct() 0 3 1
A toLatin() 0 5 1
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