1
|
|
|
<?php // Copyright ⓒ 2018 Magneds IP B.V. - All Rights Reserved |
2
|
|
|
namespace Magneds\Lokalise\TranslateString\Request; |
3
|
|
|
|
4
|
|
|
use Magneds\Lokalise\Language\Entity\Language; |
5
|
|
|
use Magneds\Lokalise\Project\Entity\ProjectID; |
6
|
|
|
use Magneds\Lokalise\RequestInterface; |
7
|
|
|
use Magneds\Lokalise\ResponseInfo; |
8
|
|
|
use Magneds\Lokalise\TranslateString\Entity\LanguageTranslateStringCollection; |
9
|
|
|
use Magneds\Lokalise\TranslateString\Entity\TranslateString; |
10
|
|
|
use Psr\Http\Message\ResponseInterface; |
11
|
|
|
|
12
|
|
|
class ListPairsByLanguageRequest implements RequestInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var ProjectID |
16
|
|
|
*/ |
17
|
|
|
protected $projectID; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var Language[] |
21
|
|
|
*/ |
22
|
|
|
protected $languages; |
23
|
|
|
|
24
|
|
|
const PLATFORM_IOS = 1; |
25
|
|
|
const PLATFORM_ANDROID = 2; |
26
|
|
|
const PLATFORM_WEB = 4; |
27
|
|
|
const PLATFORM_OTHER = 16; |
28
|
|
|
|
29
|
|
|
protected $platformMask; |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
public function __construct(ProjectID $projectID, array $languages = []) |
33
|
|
|
{ |
34
|
|
|
$this->projectID = $projectID; |
35
|
|
|
$this->languages = $languages; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Return the method to make this request in. |
40
|
|
|
* |
41
|
|
|
* @return string |
42
|
|
|
*/ |
43
|
|
|
public function getMethod() |
44
|
|
|
{ |
45
|
|
|
return 'POST'; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* This URI is appended to the route URL given to the Client object. |
50
|
|
|
* |
51
|
|
|
* @return string |
52
|
|
|
*/ |
53
|
|
|
public function getURI() |
54
|
|
|
{ |
55
|
|
|
return 'string/list'; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return array |
60
|
|
|
*/ |
61
|
|
|
public function getQueryArguments() |
62
|
|
|
{ |
63
|
|
|
return []; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return array |
68
|
|
|
*/ |
69
|
|
|
public function getBody() |
70
|
|
|
{ |
71
|
|
|
$body = [ |
72
|
|
|
'id' => $this->projectID->getID(), |
73
|
|
|
]; |
74
|
|
|
|
75
|
|
|
if (!empty($this->languages)) { |
76
|
|
|
$body['langs'] = $this->getLanguageISOCodes(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $body; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param ResponseInterface $response |
84
|
|
|
* @return mixed |
85
|
|
|
*/ |
86
|
|
|
public function handleResponse(ResponseInterface $response): ResponseInfo |
87
|
|
|
{ |
88
|
|
|
$responseData = json_decode($response->getBody()->getContents(), true); |
89
|
|
|
$responseInfo = ResponseInfo::buildFromArray($responseData['response']); |
90
|
|
|
|
91
|
|
|
if ($responseInfo->getCode() !== 200) { |
92
|
|
|
return $responseInfo; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$languageStringCollections = []; |
96
|
|
|
foreach ($responseData['strings'] as $lang => $strings) { |
97
|
|
|
|
98
|
|
|
$translateStrings = []; |
99
|
|
|
foreach ($strings as $string) { |
100
|
|
|
$translateStrings[] = TranslateString::buildFromArray($string); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$languageStringCollections[] = new LanguageTranslateStringCollection($lang, ...$translateStrings); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
// Add it to the response object. |
107
|
|
|
$responseInfo->setActionData($languageStringCollections); |
108
|
|
|
|
109
|
|
|
return $responseInfo; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
protected function getLanguageISOCodes() |
113
|
|
|
{ |
114
|
|
|
return array_map(function (Language $lang) { |
115
|
|
|
return $lang->getIso(); |
116
|
|
|
}, $this->languages); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|