1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GinoPane\PHPolyglot\API\Supplemental\Yandex; |
4
|
|
|
|
5
|
|
|
use GinoPane\NanoRest\Request\RequestContext; |
6
|
|
|
use GinoPane\NanoRest\Response\JsonResponseContext; |
7
|
|
|
use GinoPane\PHPolyglot\Supplemental\Language\Language; |
8
|
|
|
use GinoPane\NanoRest\Exceptions\RequestContextException; |
9
|
|
|
use GinoPane\PHPolyglot\Exception\BadResponseContextException; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Trait YandexApiErrorsTrait |
13
|
|
|
* |
14
|
|
|
* Handles Yandex-specific errors |
15
|
|
|
* |
16
|
|
|
* @author Sergey <Gino Pane> Karavay |
17
|
|
|
*/ |
18
|
|
|
trait YandexApiTrait |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* API key required for calls |
22
|
|
|
* |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $apiKey = ''; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Custom status messages for error statuses |
29
|
|
|
* |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
private static $customStatusMessages = [ |
33
|
|
|
YandexApiStatusesInterface::STATUS_INVALID_API_KEY => "Invalid API key", |
34
|
|
|
YandexApiStatusesInterface::STATUS_BLOCKED_API_KEY => "Blocked API key", |
35
|
|
|
YandexApiStatusesInterface::STATUS_REQUEST_AMOUNT_LIMIT_EXCEEDED => |
36
|
|
|
"Exceeded the daily limit on the number of requests", |
37
|
|
|
YandexApiStatusesInterface::STATUS_TEXT_AMOUNT_LIMIT_EXCEEDED => |
38
|
|
|
"Exceeded the daily limit on the amount of translated text", |
39
|
|
|
YandexApiStatusesInterface::STATUS_TEXT_SIZE_LIMIT_EXCEEDED => "Exceeded the maximum text size", |
40
|
|
|
YandexApiStatusesInterface::STATUS_TEXT_CANNOT_BE_TRANSLATED => "The text cannot be translated", |
41
|
|
|
YandexApiStatusesInterface::STATUS_TRANSLATION_DIRECTION_INVALID => |
42
|
|
|
"The specified translation direction is not supported" |
43
|
|
|
]; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Checks for Yandex-specific errors and throws exception if error detected |
47
|
|
|
* |
48
|
|
|
* @param array $responseArray |
49
|
|
|
* |
50
|
|
|
* @throws BadResponseContextException |
51
|
|
|
*/ |
52
|
|
|
public function filterYandexSpecificErrors(array $responseArray): void |
53
|
|
|
{ |
54
|
|
|
if (isset($responseArray['code'])) { |
55
|
|
|
if (($responseArray['code'] !== YandexApiStatusesInterface::STATUS_SUCCESS) && |
56
|
|
|
isset(self::$customStatusMessages[$responseArray['code']]) |
57
|
|
|
) { |
58
|
|
|
$errorMessage = $responseArray['message'] |
59
|
|
|
?? self::$customStatusMessages[$responseArray['code']]; |
60
|
|
|
|
61
|
|
|
throw new BadResponseContextException($errorMessage, $responseArray['code']); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param RequestContext $requestContext |
68
|
|
|
* |
69
|
|
|
* @throws RequestContextException |
70
|
|
|
* |
71
|
|
|
* @return RequestContext |
72
|
|
|
*/ |
73
|
|
|
private function fillGeneralRequestSettings(RequestContext $requestContext): RequestContext |
74
|
|
|
{ |
75
|
|
|
$requestContext |
76
|
|
|
->setContentType(RequestContext::CONTENT_TYPE_FORM_URLENCODED) |
77
|
|
|
->setResponseContextClass(JsonResponseContext::class); |
78
|
|
|
|
79
|
|
|
return $requestContext; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Get auth part of the request data |
84
|
|
|
* |
85
|
|
|
* @return array |
86
|
|
|
*/ |
87
|
|
|
private function getAuthData(): array |
88
|
|
|
{ |
89
|
|
|
return ['key' => $this->apiKey]; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param Language $languageTo |
94
|
|
|
* @param Language $languageFrom |
95
|
|
|
* |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
|
|
private function getLanguageString(Language $languageTo, Language $languageFrom): string |
99
|
|
|
{ |
100
|
|
|
return implode("-", array_filter([$languageFrom->getCode(), $languageTo->getCode()])); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|