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 |
|
$this->text = $text; |
69
|
12 |
|
$this->language = new Language($language); |
70
|
9 |
|
$this->format = $this->validateFormat($format); |
71
|
6 |
|
$this->options = $this->validateOptions($options); |
72
|
3 |
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param $format |
76
|
|
|
* @return mixed |
77
|
|
|
* @throws ArgumentException |
78
|
|
|
*/ |
79
|
9 |
|
public function validateFormat($format) |
80
|
|
|
{ |
81
|
9 |
|
if (!in_array($format, [Speak::FORMAT_MP3, Speak::FORMAT_WAV])) { |
82
|
3 |
|
throw new ArgumentException(sprintf('"%s" is not an accepted format.', $format)); |
83
|
|
|
} |
84
|
|
|
|
85
|
6 |
|
return $format; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param $options |
90
|
|
|
* @return mixed |
91
|
|
|
* @throws ArgumentException |
92
|
|
|
*/ |
93
|
6 |
|
public function validateOptions($options) |
94
|
|
|
{ |
95
|
6 |
|
if (!in_array($options, [Speak::OPTION_MAX_QUALITY, Speak::OPTION_MIN_SIZE])) { |
96
|
3 |
|
throw new ArgumentException( |
97
|
3 |
|
sprintf('invalid options: "%s"', $options) |
98
|
3 |
|
); |
99
|
|
|
} |
100
|
|
|
|
101
|
3 |
|
return $options; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
3 |
|
public function getRequestMethod() |
108
|
|
|
{ |
109
|
3 |
|
return 'GET'; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return array |
114
|
|
|
*/ |
115
|
3 |
|
public function getRequestOptions() |
116
|
|
|
{ |
117
|
|
|
return ['query' => [ |
118
|
3 |
|
'text' => $this->text, |
119
|
3 |
|
'language' => (string)$this->language, |
120
|
3 |
|
'format' => $this->format, |
121
|
3 |
|
'options' => $this->options, |
122
|
3 |
|
]]; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param \GuzzleHttp\Message\ResponseInterface $response |
127
|
|
|
* @return string |
128
|
|
|
*/ |
129
|
3 |
|
public function processResponse(\GuzzleHttp\Message\ResponseInterface $response) |
130
|
|
|
{ |
131
|
3 |
|
return base64_encode($response->getBody()->getContents()); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|