|
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\Language; |
|
16
|
|
|
use badams\MicrosoftTranslator\Exceptions\ArgumentException; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Class Detect |
|
20
|
|
|
* @package badams\MicrosoftTranslator\Methods |
|
21
|
|
|
* @link https://msdn.microsoft.com/en-us/library/ff512411.aspx |
|
22
|
|
|
*/ |
|
23
|
|
|
class Detect implements \badams\MicrosoftTranslator\ApiMethodInterface |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @const Maximum allowable length of text |
|
27
|
|
|
*/ |
|
28
|
|
|
const TEXT_MAX_LENGTH = 10000; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $text; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Detect constructor. |
|
37
|
|
|
* @param $text |
|
38
|
|
|
*/ |
|
39
|
6 |
|
public function __construct($text) |
|
40
|
|
|
{ |
|
41
|
6 |
|
$this->text = $text; |
|
42
|
|
|
|
|
43
|
6 |
|
if (strlen($this->text) > Detect::TEXT_MAX_LENGTH) { |
|
44
|
3 |
|
throw new ArgumentException( |
|
45
|
3 |
|
sprintf('The length of the text must not exceed %s characters.', Detect::TEXT_MAX_LENGTH) |
|
46
|
3 |
|
); |
|
47
|
|
|
} |
|
48
|
3 |
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @return string |
|
52
|
|
|
*/ |
|
53
|
3 |
|
public function getRequestMethod() |
|
54
|
|
|
{ |
|
55
|
3 |
|
return 'GET'; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @return array |
|
60
|
|
|
*/ |
|
61
|
3 |
|
public function getRequestOptions() |
|
62
|
|
|
{ |
|
63
|
3 |
|
return ['query' => ['text' => $this->text]]; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param \GuzzleHttp\Message\ResponseInterface $response |
|
68
|
|
|
* @return Language |
|
69
|
|
|
*/ |
|
70
|
3 |
|
public function processResponse(\GuzzleHttp\Message\ResponseInterface $response) |
|
71
|
|
|
{ |
|
72
|
3 |
|
$xml = (array)simplexml_load_string($response->getBody()->getContents()); |
|
73
|
3 |
|
return new Language((string)$xml[0]); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|