|
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\Exceptions\UnsupportedLanguageException; |
|
17
|
|
|
use badams\MicrosoftTranslator\Language; |
|
18
|
|
|
use badams\MicrosoftTranslator\Responses\GetTranslationsResponse; |
|
19
|
|
|
use badams\MicrosoftTranslator\TranslateOptions; |
|
20
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Class GetTranslationsArray |
|
24
|
|
|
* @package badams\MicrosoftTranslator\Methods |
|
25
|
|
|
* @link https://msdn.microsoft.com/en-us/library/ff512418.aspx |
|
26
|
|
|
*/ |
|
27
|
|
|
class GetTranslationsArray implements \badams\MicrosoftTranslator\ApiMethodInterface |
|
28
|
|
|
{ |
|
29
|
|
|
const MAX_TEXTS = 10; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @const Maximum allowable length of text |
|
33
|
|
|
*/ |
|
34
|
|
|
const TEXT_MAX_LENGTH = 10000; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var array |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $texts; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var Language |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $to; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var Language|null |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $from; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @var int |
|
53
|
|
|
*/ |
|
54
|
|
|
protected $maxTranslations; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @var TranslateOptions |
|
58
|
|
|
*/ |
|
59
|
|
|
protected $options; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* GetTranslationsArray constructor. |
|
63
|
|
|
* @param $texts |
|
64
|
|
|
* @param $to |
|
65
|
|
|
* @param $from |
|
66
|
|
|
* @param int $maxTranslations |
|
67
|
|
|
* @param TranslateOptions|null $options |
|
68
|
|
|
* @throws ArgumentException |
|
69
|
|
|
* @throws UnsupportedLanguageException |
|
70
|
|
|
*/ |
|
71
|
15 |
|
public function __construct($texts, $to, $from, $maxTranslations = 5, TranslateOptions $options = null) |
|
72
|
|
|
{ |
|
73
|
15 |
|
$this->texts = $texts; |
|
74
|
15 |
|
$this->to = new Language($to); |
|
75
|
15 |
|
$this->from = new Language($from); |
|
76
|
15 |
|
$this->maxTranslations = $maxTranslations; |
|
77
|
15 |
|
$this->options = $options ? $options : new TranslateOptions(); |
|
78
|
|
|
|
|
79
|
15 |
|
$this->assertTextsIsArray(); |
|
80
|
12 |
|
$this->assertTextsCount(); |
|
81
|
9 |
|
$this->assertTextsLength(); |
|
82
|
6 |
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @throws ArgumentException |
|
86
|
|
|
*/ |
|
87
|
15 |
|
protected function assertTextsIsArray() |
|
88
|
|
|
{ |
|
89
|
15 |
|
if (!is_array($this->texts)) { |
|
90
|
3 |
|
throw new ArgumentException('texts must be an array'); |
|
91
|
|
|
} |
|
92
|
12 |
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @throws ArgumentException |
|
96
|
|
|
*/ |
|
97
|
12 |
|
protected function assertTextsCount() |
|
98
|
|
|
{ |
|
99
|
12 |
|
if (count($this->texts) > self::MAX_TEXTS) { |
|
100
|
3 |
|
throw new ArgumentException(sprintf('Amount of texts cannot exceed %s', self::MAX_TEXTS)); |
|
101
|
|
|
} |
|
102
|
9 |
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @throws ArgumentException |
|
106
|
|
|
*/ |
|
107
|
9 |
|
protected function assertTextsLength() |
|
108
|
|
|
{ |
|
109
|
9 |
|
$textLengths = array_map('strlen', $this->texts); |
|
110
|
|
|
|
|
111
|
9 |
|
if (array_sum($textLengths) > self::TEXT_MAX_LENGTH) { |
|
112
|
3 |
|
throw new ArgumentException(sprintf('Total length of texts cannot exceed %s', self::TEXT_MAX_LENGTH)); |
|
113
|
|
|
} |
|
114
|
6 |
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @return string |
|
118
|
|
|
*/ |
|
119
|
3 |
|
public function getRequestMethod() |
|
120
|
|
|
{ |
|
121
|
3 |
|
return 'POST'; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @return array |
|
126
|
|
|
*/ |
|
127
|
3 |
|
public function getRequestOptions() |
|
128
|
|
|
{ |
|
129
|
3 |
|
return ['body' => $this->createBodyXml()]; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @return string |
|
134
|
|
|
*/ |
|
135
|
6 |
|
protected function createBodyXml() |
|
136
|
|
|
{ |
|
137
|
6 |
|
$xml = new \DOMDocument(); |
|
138
|
6 |
|
$xml->formatOutput = true; |
|
139
|
6 |
|
$root = $xml->createElement('GetTranslationsArrayRequest'); |
|
140
|
6 |
|
$xml->appendChild($root); |
|
141
|
|
|
|
|
142
|
6 |
|
$root->appendChild($xml->createElement('AppId')); |
|
143
|
6 |
|
$root->appendChild($xml->createElement('From', $this->from)); |
|
144
|
|
|
|
|
145
|
6 |
|
$options = $xml->importNode($this->options->xml('Options')->childNodes->item(0), true); |
|
146
|
|
|
|
|
147
|
6 |
|
$root->appendChild($options); |
|
148
|
|
|
|
|
149
|
|
|
|
|
150
|
6 |
|
$texts = $xml->createElement('Texts'); |
|
151
|
6 |
|
$root->appendChild($texts); |
|
152
|
|
|
|
|
153
|
6 |
|
foreach ($this->texts as $text) { |
|
154
|
6 |
|
$texts->appendChild( |
|
155
|
6 |
|
$xml->createElementNS('http://schemas.microsoft.com/2003/10/Serialization/Arrays', 'string', $text) |
|
156
|
6 |
|
); |
|
157
|
6 |
|
} |
|
158
|
|
|
|
|
159
|
6 |
|
$root->appendChild($xml->createElement('To', $this->to)); |
|
160
|
6 |
|
$root->appendChild($xml->createElement('MaxTranslations', $this->maxTranslations)); |
|
161
|
|
|
|
|
162
|
6 |
|
return $xml->saveXML(); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* @param \GuzzleHttp\Message\ResponseInterface $response |
|
167
|
|
|
* @return GetTranslationsResponse[] |
|
168
|
|
|
*/ |
|
169
|
3 |
|
public function processResponse(\GuzzleHttp\Message\ResponseInterface $response) |
|
170
|
|
|
{ |
|
171
|
3 |
|
$xml = simplexml_load_string($response->getBody()->getContents()); |
|
172
|
|
|
|
|
173
|
3 |
|
$responses = []; |
|
174
|
|
|
|
|
175
|
3 |
|
foreach ($xml->GetTranslationsResponse as $node) { |
|
176
|
3 |
|
$responses[] = GetTranslationsResponse::fromXml($node); |
|
177
|
3 |
|
} |
|
178
|
|
|
|
|
179
|
3 |
|
return $responses; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
} |