Completed
Branch master (655e5b)
by Jelly
02:58 queued 33s
created

Translation::setConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
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 5
    public function __construct(Client $http, array $config = [])
39
    {
40 5
        $this->http = $http;
41 5
        $this->config = $config;
42 5
    }
43
44
    /**
45
     * @param $text
46
     * @return mixed
47
     */
48 3
    public function translate($text)
49
    {
50 3
        return $this->getTranslatedText($text);
51
    }
52
53
    /**
54
     * @param $text
55
     * @return string
56
     */
57 1
    public function translug($text)
58
    {
59 1
        return str_slug($this->getTranslatedText($text));
60
    }
61
62
    /**
63
     * @param $text
64
     * @return mixed
65
     */
66 4
    private function getTranslatedText($text)
67
    {
68 4
        if ($this->isEnglish($text)) {
69 4
            return $text;
70
        }
71
        $text = $this->removeSegment($text);
72
        $url = $this->getTranslateUrl($text);
73
        $response = $this->http->get($url);
74
75
        return $this->getTranslation(json_decode($response->getBody(), true));
76
    }
77
78
    /**
79
     * @param array $translateResponse
80
     * @return mixed
81
     */
82
    private function getTranslation(array $translateResponse)
83
    {
84
        if ($translateResponse['errorCode'] === 0) {
85
            return $this->getTranslatedTextFromResponse($translateResponse);
86
        }
87
88
        throw new TranslationErrorException('Translate error, error_code : ' . $translateResponse['errorCode'] . '. Refer url: http://fanyi.youdao.com/openapi?path=data-mode');
89
    }
90
91
    /**
92
     * @param array $translateResponse
93
     * @return mixed
94
     */
95
    private function getTranslatedTextFromResponse(array $translateResponse)
96
    {
97
        return $translateResponse['translation'][0];
98
    }
99
100
    /**
101
     * @param $text
102
     * @return string
103
     */
104
    private function getTranslateUrl($text)
105
    {
106
        if (count($this->config) > 1) {
107
            $query = http_build_query($this->config);
108
            return $this->api . $query . '&q=' . $text;
109
        }
110
        return $this->api . 'keyfrom=' . config('services.youdao.from') . '&key=' . config('services.youdao.key') . '&q=' . $text;
111
    }
112
113
    /**
114
     * @param $text
115
     * @return bool
116
     */
117 4
    private function isEnglish($text)
118
    {
119 4
        if (preg_match("/\p{Han}+/u", $text)) {
120
            return false;
121
        }
122
123 4
        return true;
124
    }
125
126
    /**
127
     * Remove segment #
128
     *
129
     * @param $text
130
     * @return mixed
131
     */
132
    private function removeSegment($text)
133
    {
134
        return str_replace('#', '', ltrim($text));
135
    }
136
137
}