Passed
Push — master ( 5ced2d...3d121b )
by Mokhirjon
01:36
created

Transliterator::getText()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Zvermafia\Transliteration;
4
5
use GuzzleHttp\Client;
6
use Zvermafia\Transliteration\Contracts\TransliteratorContract;
7
8
class Transliterator implements TransliteratorContract
9
{
10
    /** @var string */
11
    const API_BASE_URI = 'http://www.alif.uz/api/';
12
13
    /** @var Client */
14
    private $transport;
15
16
    /** @var string */
17
    private $text;
18
19
    /** @var string */
20
    private $isLatToCyr = 'true';
21
22
    /** @var string */
23
    private $result;
24
25
    /**
26
     * Create a new instance.
27
     */
28 15
    public function __construct()
29
    {
30 15
        $this->transport = new Client([
31 15
            'base_uri' => self::API_BASE_URI,
32
        ]);
33 15
    }
34
35
    /**
36
     * @{inheritDoc}
37
     */
38 6
    public function setText(string $text) : TransliteratorContract
39
    {
40 6
        $this->text = $text;
41
42 6
        return $this;
43
    }
44
45
    /**
46
     * @{inheritDoc}
47
     */
48 3
    public function getText() : string
49
    {
50 3
        return $this->text;
51
    }
52
53
    /**
54
     * @{inheritDoc}
55
     */
56 3
    public function toLatin() : TransliteratorContract
57
    {
58 3
        $this->isLatToCyr = 'false';
59
60 3
        return $this;
61
    }
62
63
    /**
64
     * @{inheritDoc}
65
     */
66 3
    public function toCyrillic() : TransliteratorContract
67
    {
68 3
        $this->isLatToCyr = 'true';
69
70 3
        return $this;
71
    }
72
73
    /**
74
     * @{inheritDoc}
75
     */
76
    public function translit() : TransliteratorContract
77
    {
78
        $parts = str_split_unicode($this->getText());
79
        $result = "";
80
        $response = "";
81
82
        foreach ($parts as $part) {
83
            $response .= $this->transport
84
                ->request('GET', 'translit', [
85
                    'query' => [
86
                        'inputText' => $part,
87
                        'isLatToCyr' => $this->isLatToCyr,
88
                    ],
89
                ])
90
                ->getBody();
91
92
            $result = dropDoubleQuotes($response);
93
        }
94
95
        $this->result = $result;
96
97
        return $this;
98
    }
99
100
    /**
101
     * @{inheritDoc}
102
     */
103 3
    public function getResult() : string
104
    {
105 3
        return $this->result;
106
    }
107
}
108