|
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
|
|
|
use badams\MicrosoftTranslator\Responses\GetTranslationsResponse; |
|
18
|
|
|
use badams\MicrosoftTranslator\TranslateOptions; |
|
19
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Class GetTranslations |
|
23
|
|
|
* @package badams\MicrosoftTranslator\Methods |
|
24
|
|
|
* @link https://msdn.microsoft.com/en-us/library/ff512417.aspx |
|
25
|
|
|
*/ |
|
26
|
|
|
class GetTranslations implements \badams\MicrosoftTranslator\ApiMethodInterface |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @const Maximum allowable length of text |
|
30
|
|
|
*/ |
|
31
|
|
|
const TEXT_MAX_LENGTH = 10000; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var string |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $text; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var Language |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $to; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var Language|null |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $from; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var int |
|
50
|
|
|
*/ |
|
51
|
|
|
protected $maxTranslations; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @var TranslateOptions |
|
55
|
|
|
*/ |
|
56
|
|
|
protected $options; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Translate constructor. |
|
60
|
|
|
* @param $text |
|
61
|
|
|
* @param $to |
|
62
|
|
|
* @param null $from |
|
63
|
|
|
* @param string $contentType |
|
|
|
|
|
|
64
|
|
|
*/ |
|
65
|
12 |
|
public function __construct($text, $to, $from, $maxTranslations = 5, TranslateOptions $options = null) |
|
66
|
|
|
{ |
|
67
|
12 |
|
$this->text = $text; |
|
68
|
12 |
|
$this->to = new Language($to); |
|
69
|
9 |
|
$this->from = new Language($from); |
|
70
|
9 |
|
$this->maxTranslations = $maxTranslations; |
|
71
|
9 |
|
$this->options = $options ? $options : new TranslateOptions(); |
|
72
|
|
|
|
|
73
|
9 |
|
if (strlen($text) > GetTranslations::TEXT_MAX_LENGTH) { |
|
74
|
3 |
|
throw new ArgumentException( |
|
75
|
3 |
|
sprintf('The length of the text must not exceed %s characters.', GetTranslations::TEXT_MAX_LENGTH) |
|
76
|
3 |
|
); |
|
77
|
|
|
} |
|
78
|
6 |
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @return string |
|
82
|
|
|
*/ |
|
83
|
6 |
|
public function getRequestMethod() |
|
84
|
|
|
{ |
|
85
|
6 |
|
return 'POST'; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @return array |
|
90
|
|
|
*/ |
|
91
|
6 |
|
public function getRequestOptions() |
|
92
|
|
|
{ |
|
93
|
|
|
return [ |
|
94
|
6 |
|
'body' => $this->options->xml(), |
|
95
|
|
|
'query' => [ |
|
96
|
6 |
|
'text' => $this->text, |
|
97
|
6 |
|
'to' => (string)$this->to, |
|
98
|
6 |
|
'from' => (string)$this->from, |
|
99
|
6 |
|
'maxTranslations' => $this->maxTranslations, |
|
100
|
|
|
] |
|
101
|
6 |
|
]; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @param \GuzzleHttp\Message\ResponseInterface $response |
|
106
|
|
|
* @return GetTranslationsResponse |
|
107
|
|
|
*/ |
|
108
|
6 |
|
public function processResponse(\GuzzleHttp\Message\ResponseInterface $response) |
|
109
|
|
|
{ |
|
110
|
6 |
|
$xml = simplexml_load_string($response->getBody()->getContents()); |
|
111
|
|
|
|
|
112
|
6 |
|
return GetTranslationsResponse::fromXml($xml); |
|
113
|
|
|
} |
|
114
|
|
|
} |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.