1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the badams\MicrosoftTranslator library |
4
|
|
|
* |
5
|
|
|
* @license http://opensource.org/licenses/MIT |
6
|
|
|
* @link https://github.com/badams/microsoft-translator |
7
|
|
|
* @package badams/microsoft-translator |
8
|
|
|
* |
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
10
|
|
|
* file that was distributed with this source code. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace badams\MicrosoftTranslator\Methods; |
14
|
|
|
|
15
|
|
|
use badams\MicrosoftTranslator\Exceptions\ArgumentException; |
16
|
|
|
use badams\MicrosoftTranslator\Language; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class Translate |
20
|
|
|
* |
21
|
|
|
* @package badams\MicrosoftTranslator\Methods |
22
|
|
|
* @link https://msdn.microsoft.com/en-us/library/ff512421.aspx |
23
|
|
|
*/ |
24
|
|
|
class Translate implements \badams\MicrosoftTranslator\ApiMethodInterface |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @const Maximum allowable length of text |
28
|
|
|
*/ |
29
|
|
|
const TEXT_MAX_LENGTH = 10000; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @const Html content type, HTML needs to be well-formed. |
33
|
|
|
*/ |
34
|
|
|
const CONTENT_TYPE_HTML = 'text/html'; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @const plain text content type |
38
|
|
|
*/ |
39
|
|
|
const CONTENT_TYPE_PLAIN = 'text/plain'; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
protected $text; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var Language |
48
|
|
|
*/ |
49
|
|
|
protected $to; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var Language|null |
53
|
|
|
*/ |
54
|
|
|
protected $from; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var string |
58
|
|
|
*/ |
59
|
|
|
protected $contentType; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Translate constructor. |
63
|
|
|
* @param $text |
64
|
|
|
* @param $to |
65
|
|
|
* @param null $from |
66
|
|
|
* @param string $contentType |
67
|
|
|
*/ |
68
|
27 |
|
public function __construct($text, $to, $from = null, $contentType = Translate::CONTENT_TYPE_PLAIN) |
69
|
|
|
{ |
70
|
27 |
|
$this->text = $text; |
71
|
27 |
|
$this->to = new Language($to); |
72
|
|
|
|
73
|
24 |
|
if ($from !== null) { |
74
|
21 |
|
$this->from = new Language($from); |
75
|
21 |
|
} |
76
|
|
|
|
77
|
24 |
|
if (!in_array($contentType, [Translate::CONTENT_TYPE_PLAIN, Translate::CONTENT_TYPE_HTML])) { |
78
|
3 |
|
throw new ArgumentException(sprintf('%s is not a valid content type.', $contentType)); |
79
|
|
|
} |
80
|
|
|
|
81
|
21 |
|
if (strlen($text) > Translate::TEXT_MAX_LENGTH) { |
82
|
3 |
|
throw new ArgumentException( |
83
|
3 |
|
sprintf('The length of the text must not exceed %s characters.', Translate::TEXT_MAX_LENGTH) |
84
|
3 |
|
); |
85
|
|
|
} |
86
|
18 |
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return string |
90
|
|
|
*/ |
91
|
18 |
|
public function getRequestMethod() |
92
|
|
|
{ |
93
|
18 |
|
return 'GET'; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return array |
98
|
|
|
*/ |
99
|
18 |
|
public function getRequestOptions() |
100
|
|
|
{ |
101
|
|
|
return [ |
102
|
|
|
'query' => [ |
103
|
18 |
|
'text' => $this->text, |
104
|
18 |
|
'to' => (string)$this->to, |
105
|
18 |
|
'from' => (string)$this->from, |
106
|
18 |
|
'contentType' => $this->contentType, |
107
|
|
|
] |
108
|
18 |
|
]; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param \GuzzleHttp\Message\ResponseInterface $response |
113
|
|
|
* @return string |
114
|
|
|
*/ |
115
|
9 |
|
public function processResponse(\GuzzleHttp\Message\ResponseInterface $response) |
116
|
|
|
{ |
117
|
9 |
|
$xml = (array)simplexml_load_string($response->getBody()->getContents()); |
118
|
9 |
|
return (string)$xml[0]; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|