Completed
Push — master ( eb030a...fe6b07 )
by Jelly
02:28
created

Translation::getConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace JellyBool\Translug;
4
5
use GuzzleHttp\Client;
6
use JellyBool\Translug\Exceptions\TranslationErrorException;
7
8
/**
9
 * Class Translation
10
 *
11
 * @package JellyBool\Translug
12
 */
13
class Translation
14
{
15
16
    /**
17
     * Youdao api url
18
     *
19
     * @var string
20
     */
21
    protected $api = 'http://fanyi.youdao.com/openapi.do?type=data&doctype=json&version=1.1&';
22
    /**
23
     * @var Client
24
     */
25
    protected $http;
26
27
    /**
28
     * @var array
29
     */
30
    protected $config = [];
31
32
    /**
33
     * Translation constructor.
34
     *
35
     * @param Client $http
36
     * @param array $config
37
     */
38 6
    public function __construct(Client $http, array $config = [])
39
    {
40 6
        $this->http = $http;
41 6
        $this->config = $config;
42 6
    }
43
44
    /**
45
     * @param array $config
46
     */
47 1
    public function setConfig(array $config)
48
    {
49 1
        $this->config = $config;
50 1
    }
51
52
    /**
53
     * @return array
54
     */
55 1
    public function getConfig()
56
    {
57 1
        return $this->config;
58
    }
59
60
    /**
61
     * @param $text
62
     * @return mixed
63
     */
64 3
    public function translate($text)
65
    {
66 3
        return $this->getTranslatedText($text);
67
    }
68
69
    /**
70
     * @param $text
71
     * @return string
72
     */
73 1
    public function translug($text)
74
    {
75 1
        return str_slug($this->getTranslatedText($text));
76
    }
77
78
    /**
79
     * @param $text
80
     * @return mixed
81
     */
82 4
    private function getTranslatedText($text)
83
    {
84 4
        if ($this->isEnglish($text)) {
85 4
            return $text;
86
        }
87
        $text = $this->removeSegment($text);
88
        $url = $this->getTranslateUrl($text);
89
        $response = $this->http->get($url);
90
91
        return $this->getTranslation(json_decode($response->getBody(), true));
92
    }
93
94
    /**
95
     * @param array $translateResponse
96
     * @return mixed
97
     */
98
    private function getTranslation(array $translateResponse)
99
    {
100
        if ($translateResponse['errorCode'] === 0) {
101
            return $this->getTranslatedTextFromResponse($translateResponse);
102
        }
103
104
        throw new TranslationErrorException('Translate error, error_code : ' . $translateResponse['errorCode'] . '. Refer url: http://fanyi.youdao.com/openapi?path=data-mode');
105
    }
106
107
    /**
108
     * @param array $translateResponse
109
     * @return mixed
110
     */
111
    private function getTranslatedTextFromResponse(array $translateResponse)
112
    {
113
        return $translateResponse['translation'][0];
114
    }
115
116
    /**
117
     * @param $text
118
     * @return string
119
     */
120
    private function getTranslateUrl($text)
121
    {
122
        if (count($this->config) > 1) {
123
            $query = http_build_query($this->config);
124
            return $this->api . $query . '&q=' . $text;
125
        }
126
        return $this->api . 'keyfrom=' . config('services.youdao.from') . '&key=' . config('services.youdao.key') . '&q=' . $text;
127
    }
128
129
    /**
130
     * @param $text
131
     * @return bool
132
     */
133 4
    private function isEnglish($text)
134
    {
135 4
        if (preg_match("/\p{Han}+/u", $text)) {
136
            return false;
137
        }
138
139 4
        return true;
140
    }
141
142
    /**
143
     * Remove segment #
144
     *
145
     * @param $text
146
     * @return mixed
147
     */
148
    private function removeSegment($text)
149
    {
150
        return str_replace('#', '', ltrim($text));
151
    }
152
153
}