1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @Author Chiarillo Massimo |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Yandex\TranslatorBundle\Http; |
8
|
|
|
|
9
|
|
|
use Guzzle\Http\ClientInterface; |
10
|
|
|
use Guzzle\Http\Exception\RequestException; |
11
|
|
|
use Yandex\TranslatorBundle\Service\Client; |
12
|
|
|
|
13
|
|
|
class Request |
14
|
|
|
{ |
15
|
|
|
const YANDEX_TRANSLATOR_VERSION = '1.5'; |
16
|
|
|
|
17
|
|
|
const PLAIN_FORMAT = 'plain'; |
18
|
|
|
const HTML_FORMAT = 'html'; |
19
|
|
|
|
20
|
|
|
protected $client; |
21
|
|
|
|
22
|
|
|
protected $key; |
23
|
|
|
|
24
|
|
|
protected $from; |
25
|
|
|
|
26
|
|
|
protected $to; |
27
|
|
|
|
28
|
|
|
protected $text; |
29
|
|
|
|
30
|
|
|
protected $format = self::PLAIN_FORMAT; |
31
|
|
|
|
32
|
|
|
public function __construct(Client $client) |
33
|
|
|
{ |
34
|
|
|
$this->client = $client; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function setClient(Client $client) |
38
|
|
|
{ |
39
|
|
|
$this->client = $client; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return ClientInterface |
44
|
|
|
*/ |
45
|
|
|
public function getClient() |
46
|
|
|
{ |
47
|
|
|
return $this->client; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return mixed |
52
|
|
|
*/ |
53
|
|
|
public function getKey() |
54
|
|
|
{ |
55
|
|
|
return $this->key; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param mixed $key |
60
|
|
|
* @return $this |
61
|
|
|
*/ |
62
|
|
|
public function setKey($key) |
63
|
|
|
{ |
64
|
|
|
$this->key = $key; |
65
|
|
|
|
66
|
|
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return mixed |
71
|
|
|
*/ |
72
|
|
|
public function getFrom() |
73
|
|
|
{ |
74
|
|
|
return $this->from; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param mixed $from |
79
|
|
|
* @return $this |
80
|
|
|
*/ |
81
|
|
|
public function setFrom($from) |
82
|
|
|
{ |
83
|
|
|
$this->from = $from; |
84
|
|
|
|
85
|
|
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return mixed |
90
|
|
|
*/ |
91
|
|
|
public function getTo() |
92
|
|
|
{ |
93
|
|
|
return $this->to; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param mixed $to |
98
|
|
|
* @return $this |
99
|
|
|
*/ |
100
|
|
|
public function setTo($to) |
101
|
|
|
{ |
102
|
|
|
$this->to = $to; |
103
|
|
|
|
104
|
|
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return mixed |
109
|
|
|
*/ |
110
|
|
|
public function getText() |
111
|
|
|
{ |
112
|
|
|
return $this->text; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param mixed $text |
117
|
|
|
* @return $this |
118
|
|
|
*/ |
119
|
|
|
public function setText($text) |
120
|
|
|
{ |
121
|
|
|
$this->text = $text; |
122
|
|
|
|
123
|
|
|
return $this; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function useHtml() |
127
|
|
|
{ |
128
|
|
|
$this->format = self::HTML_FORMAT; |
129
|
|
|
|
130
|
|
|
return $this; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function send() |
134
|
|
|
{ |
135
|
|
|
$response = $this->client |
136
|
|
|
->get( |
137
|
|
|
sprintf('/api/v%s/tr.json/translate', self::YANDEX_TRANSLATOR_VERSION), |
138
|
|
|
[], |
139
|
|
|
[ |
140
|
|
|
'query' => [ |
141
|
|
|
'key' => $this->getKey(), |
142
|
|
|
'lang' => sprintf('%s-%s', $this->getFrom(), $this->getTo()), |
143
|
|
|
'text' => $this->getText(), |
144
|
|
|
'format' => $this->format |
145
|
|
|
] |
146
|
|
|
] |
147
|
|
|
)->send(); |
148
|
|
|
|
149
|
|
|
return new Response( |
150
|
|
|
$response->getBody(true) |
151
|
|
|
); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|