1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GinoPane\PHPolyglot\API\Implementation\Translate; |
4
|
|
|
|
5
|
|
|
use GinoPane\NanoRest\Request\RequestContext; |
6
|
|
|
use GinoPane\NanoRest\Exceptions\TransportException; |
7
|
|
|
use GinoPane\PHPolyglot\API\Implementation\ApiAbstract; |
8
|
|
|
use GinoPane\NanoRest\Response\ResponseContextAbstract; |
9
|
|
|
use GinoPane\PHPolyglot\Supplemental\Language\Language; |
10
|
|
|
use GinoPane\NanoRest\Exceptions\ResponseContextException; |
11
|
|
|
use GinoPane\PHPolyglot\Exception\BadResponseContextException; |
12
|
|
|
use GinoPane\PHPolyglot\Exception\MethodDoesNotExistException; |
13
|
|
|
use GinoPane\PHPolyglot\API\Response\Translate\TranslateResponse; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class TranslateApiAbstract |
17
|
|
|
* |
18
|
|
|
* @author Sergey <Gino Pane> Karavay |
19
|
|
|
*/ |
20
|
|
|
abstract class TranslateApiAbstract extends ApiAbstract implements TranslateApiInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @param string $text |
24
|
|
|
* @param Language $languageTo |
25
|
|
|
* @param Language $languageFrom |
26
|
|
|
* |
27
|
|
|
* @throws TransportException |
28
|
|
|
* @throws ResponseContextException |
29
|
|
|
* @throws BadResponseContextException |
30
|
|
|
* @throws MethodDoesNotExistException |
31
|
|
|
* |
32
|
|
|
* @return TranslateResponse |
33
|
|
|
*/ |
34
|
|
|
public function translate(string $text, Language $languageTo, Language $languageFrom): TranslateResponse |
35
|
|
|
{ |
36
|
|
|
/** @var TranslateResponse $response */ |
37
|
|
|
$response = $this->callApi(__FUNCTION__, func_get_args()); |
38
|
|
|
|
39
|
|
|
return $response; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param array $text |
44
|
|
|
* @param Language $languageTo |
45
|
|
|
* @param Language $languageFrom |
46
|
|
|
* |
47
|
|
|
* @throws TransportException |
48
|
|
|
* @throws ResponseContextException |
49
|
|
|
* @throws BadResponseContextException |
50
|
|
|
* @throws MethodDoesNotExistException |
51
|
|
|
* |
52
|
|
|
* @return TranslateResponse |
53
|
|
|
*/ |
54
|
|
|
public function translateBulk(array $text, Language $languageTo, Language $languageFrom): TranslateResponse |
55
|
|
|
{ |
56
|
|
|
/** @var TranslateResponse $response */ |
57
|
|
|
$response = $this->callApi(__FUNCTION__, func_get_args()); |
58
|
|
|
|
59
|
|
|
return $response; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Create request context for translate request |
64
|
|
|
* |
65
|
|
|
* @param string $text |
66
|
|
|
* @param Language $languageTo |
67
|
|
|
* @param Language $languageFrom |
68
|
|
|
* |
69
|
|
|
* @return RequestContext |
70
|
|
|
*/ |
71
|
|
|
abstract protected function createTranslateContext( |
72
|
|
|
string $text, |
73
|
|
|
Language $languageTo, |
74
|
|
|
Language $languageFrom |
75
|
|
|
): RequestContext; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Process response of translate request and prepare valid response |
79
|
|
|
* |
80
|
|
|
* @param ResponseContextAbstract $context |
81
|
|
|
* |
82
|
|
|
* @return TranslateResponse |
83
|
|
|
*/ |
84
|
|
|
abstract protected function prepareTranslateResponse(ResponseContextAbstract $context): TranslateResponse; |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Create request context for bulk translate request |
88
|
|
|
* |
89
|
|
|
* @param array $texts |
90
|
|
|
* @param Language $languageTo |
91
|
|
|
* @param Language $languageFrom |
92
|
|
|
* |
93
|
|
|
* @return RequestContext |
94
|
|
|
*/ |
95
|
|
|
abstract protected function createTranslateBulkContext( |
96
|
|
|
array $texts, |
97
|
|
|
Language $languageTo, |
98
|
|
|
Language $languageFrom |
99
|
|
|
): RequestContext; |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Process response of bulk translate request and prepare valid response |
103
|
|
|
* |
104
|
|
|
* @param ResponseContextAbstract $context |
105
|
|
|
* |
106
|
|
|
* @return TranslateResponse |
107
|
|
|
*/ |
108
|
|
|
abstract protected function prepareTranslateBulkResponse(ResponseContextAbstract $context): TranslateResponse; |
109
|
|
|
} |
110
|
|
|
|