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 Speak |
20
|
|
|
* @package badams\MicrosoftTranslator\Methods |
21
|
|
|
* @link https://msdn.microsoft.com/en-us/library/ff512420.aspx |
22
|
|
|
*/ |
23
|
|
|
class Speak implements \badams\MicrosoftTranslator\ApiMethodInterface |
24
|
|
|
{ |
25
|
|
|
const TEXT_MAX_LENGTH = 2000; |
26
|
|
|
|
27
|
|
|
const FORMAT_MP3 = 'audio/mp3'; |
28
|
|
|
const FORMAT_WAV = 'audio/wav'; |
29
|
|
|
|
30
|
|
|
const OPTION_MAX_QUALITY = 'MaxQuality'; |
31
|
|
|
const OPTION_MIN_SIZE = 'MinSize'; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var Language |
35
|
|
|
*/ |
36
|
|
|
protected $language; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
protected $text; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
protected $format; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var string |
50
|
|
|
*/ |
51
|
|
|
protected $options; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Speak constructor. |
55
|
|
|
* @param $text |
56
|
|
|
* @param $language |
57
|
|
|
* @param string $format |
58
|
|
|
* @param string $options |
59
|
|
|
*/ |
60
|
15 |
|
public function __construct($text, $language, $format = Speak::FORMAT_MP3, $options = Speak::OPTION_MAX_QUALITY) |
61
|
|
|
{ |
62
|
15 |
|
if (strlen($text) > Speak::TEXT_MAX_LENGTH) { |
63
|
3 |
|
throw new ArgumentException( |
64
|
3 |
|
sprintf('The length of the text must not exceed %s characters.', Speak::TEXT_MAX_LENGTH) |
65
|
3 |
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
12 |
|
if (!in_array($format, [Speak::FORMAT_MP3, Speak::FORMAT_WAV])) { |
69
|
3 |
|
throw new ArgumentException(sprintf('"%s" is not an accepted format.', $format)); |
70
|
|
|
} |
71
|
|
|
|
72
|
9 |
|
if (!in_array($options, [Speak::OPTION_MAX_QUALITY, Speak::OPTION_MIN_SIZE])) { |
73
|
3 |
|
throw new ArgumentException( |
74
|
3 |
|
sprintf('invalid options: "%s"', $options) |
75
|
3 |
|
); |
76
|
|
|
} |
77
|
|
|
|
78
|
6 |
|
$this->text = $text; |
79
|
6 |
|
$this->language = new Language($language); |
80
|
3 |
|
$this->format = $format; |
81
|
3 |
|
$this->options = $options; |
82
|
3 |
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return string |
86
|
|
|
*/ |
87
|
3 |
|
public function getRequestMethod() |
88
|
|
|
{ |
89
|
3 |
|
return 'GET'; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return array |
94
|
|
|
*/ |
95
|
3 |
|
public function getRequestOptions() |
96
|
|
|
{ |
97
|
|
|
return ['query' => [ |
98
|
3 |
|
'text' => $this->text, |
99
|
3 |
|
'language' => (string)$this->language, |
100
|
3 |
|
'format' => $this->format, |
101
|
3 |
|
'options' => $this->options, |
102
|
3 |
|
]]; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param \GuzzleHttp\Message\ResponseInterface $response |
107
|
|
|
* @return string |
108
|
|
|
*/ |
109
|
3 |
|
public function processResponse(\GuzzleHttp\Message\ResponseInterface $response) |
110
|
|
|
{ |
111
|
3 |
|
return base64_encode($response->getBody()->getContents()); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
|
115
|
|
|
} |