|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace BabDev\Transifex\Connector; |
|
4
|
|
|
|
|
5
|
|
|
use BabDev\Transifex\ApiConnector; |
|
6
|
|
|
use BabDev\Transifex\Exception\InvalidConfigurationException; |
|
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Transifex API Languages class. |
|
11
|
|
|
* |
|
12
|
|
|
* @link http://docs.transifex.com/api/languages/ |
|
13
|
|
|
*/ |
|
14
|
|
|
final class Languages extends ApiConnector |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* Create a language for a project. |
|
18
|
|
|
* |
|
19
|
|
|
* @param string $slug The slug for the project |
|
20
|
|
|
* @param string $langCode The language code for the new language |
|
21
|
|
|
* @param string[] $coordinators An array of coordinators for the language |
|
22
|
|
|
* @param array $options Optional additional params to send with the request |
|
23
|
|
|
* @param bool $skipInvalidUsername If true, the API call does not fail and instead will return a list of invalid usernames |
|
24
|
|
|
* |
|
25
|
|
|
* @return ResponseInterface |
|
26
|
|
|
* |
|
27
|
|
|
* @throws InvalidConfigurationException |
|
28
|
|
|
*/ |
|
29
|
3 |
|
public function createLanguage( |
|
30
|
|
|
string $slug, |
|
31
|
|
|
string $langCode, |
|
32
|
|
|
array $coordinators, |
|
33
|
|
|
array $options = [], |
|
34
|
|
|
bool $skipInvalidUsername = false |
|
35
|
|
|
): ResponseInterface { |
|
36
|
|
|
// Make sure the $coordinators array is not empty |
|
37
|
3 |
|
if (!\count($coordinators)) { |
|
38
|
1 |
|
throw new InvalidConfigurationException('The coordinators array must contain at least one username.'); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
2 |
|
$uri = $this->createUri("/api/2/project/$slug/languages/"); |
|
42
|
|
|
|
|
43
|
2 |
|
if ($skipInvalidUsername) { |
|
44
|
1 |
|
$uri = $uri->withQuery('skip_invalid_username'); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
// Build the required request data. |
|
48
|
|
|
$data = [ |
|
49
|
2 |
|
'language_code' => $langCode, |
|
50
|
2 |
|
'coordinators' => $coordinators, |
|
51
|
|
|
]; |
|
52
|
|
|
|
|
53
|
|
|
// Valid options to check |
|
54
|
2 |
|
$validOptions = ['translators', 'reviewers', 'list']; |
|
55
|
|
|
|
|
56
|
|
|
// Loop through the valid options and if we have them, add them to the request data |
|
57
|
2 |
|
foreach ($validOptions as $option) { |
|
58
|
2 |
|
if (isset($options[$option])) { |
|
59
|
1 |
|
$data[$option] = $options[$option]; |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
2 |
|
$request = $this->createRequest('POST', $uri); |
|
64
|
2 |
|
$request = $request->withBody($this->streamFactory->createStream(\json_encode($data))); |
|
65
|
|
|
|
|
66
|
2 |
|
return $this->client->sendRequest($request); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Delete a language within a project. |
|
71
|
|
|
* |
|
72
|
|
|
* @param string $project The project to retrieve details for |
|
73
|
|
|
* @param string $langCode The language code to retrieve details for |
|
74
|
|
|
* |
|
75
|
|
|
* @return ResponseInterface |
|
76
|
|
|
*/ |
|
77
|
2 |
|
public function deleteLanguage(string $project, string $langCode): ResponseInterface |
|
78
|
|
|
{ |
|
79
|
2 |
|
return $this->client->sendRequest($this->createRequest('DELETE', $this->createUri("/api/2/project/$project/language/$langCode/"))); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Get the coordinators for a language team in a project. |
|
84
|
|
|
* |
|
85
|
|
|
* @param string $project The project to retrieve details for |
|
86
|
|
|
* @param string $langCode The language code to retrieve details for |
|
87
|
|
|
* |
|
88
|
|
|
* @return ResponseInterface |
|
89
|
|
|
*/ |
|
90
|
2 |
|
public function getCoordinators(string $project, string $langCode): ResponseInterface |
|
91
|
|
|
{ |
|
92
|
2 |
|
return $this->client->sendRequest($this->createRequest('GET', $this->createUri("/api/2/project/$project/language/$langCode/coordinators/"))); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Get information about a given language in a project. |
|
97
|
|
|
* |
|
98
|
|
|
* @param string $project The project to retrieve details for |
|
99
|
|
|
* @param string $langCode The language code to retrieve details for |
|
100
|
|
|
* @param bool $details True to add the ?details fragment |
|
101
|
|
|
* |
|
102
|
|
|
* @return ResponseInterface |
|
103
|
|
|
*/ |
|
104
|
3 |
|
public function getLanguage(string $project, string $langCode, bool $details = false): ResponseInterface |
|
105
|
|
|
{ |
|
106
|
3 |
|
$uri = $this->createUri("/api/2/project/$project/language/$langCode/"); |
|
107
|
|
|
|
|
108
|
3 |
|
if ($details) { |
|
109
|
1 |
|
$uri = $uri->withQuery('details'); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
3 |
|
return $this->client->sendRequest($this->createRequest('GET', $uri)); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Get a list of languages for a specified project. |
|
117
|
|
|
* |
|
118
|
|
|
* @param string $project The project to retrieve details for |
|
119
|
|
|
* |
|
120
|
|
|
* @return ResponseInterface |
|
121
|
|
|
*/ |
|
122
|
2 |
|
public function getLanguages(string $project): ResponseInterface |
|
123
|
|
|
{ |
|
124
|
2 |
|
return $this->client->sendRequest($this->createRequest('GET', $this->createUri("/api/2/project/$project/languages/"))); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Get the reviewers for a language team in a project. |
|
129
|
|
|
* |
|
130
|
|
|
* @param string $project The project to retrieve details for |
|
131
|
|
|
* @param string $langCode The language code to retrieve details for |
|
132
|
|
|
* |
|
133
|
|
|
* @return ResponseInterface |
|
134
|
|
|
*/ |
|
135
|
2 |
|
public function getReviewers(string $project, string $langCode): ResponseInterface |
|
136
|
|
|
{ |
|
137
|
2 |
|
return $this->client->sendRequest($this->createRequest('GET', $this->createUri("/api/2/project/$project/language/$langCode/reviewers/"))); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Get the translators for a language team in a project. |
|
142
|
|
|
* |
|
143
|
|
|
* @param string $project The project to retrieve details for |
|
144
|
|
|
* @param string $langCode The language code to retrieve details for |
|
145
|
|
|
* |
|
146
|
|
|
* @return ResponseInterface |
|
147
|
|
|
*/ |
|
148
|
2 |
|
public function getTranslators(string $project, string $langCode): ResponseInterface |
|
149
|
|
|
{ |
|
150
|
2 |
|
return $this->client->sendRequest($this->createRequest('GET', $this->createUri("/api/2/project/$project/language/$langCode/translators/"))); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Update the coordinators for a language team in a project. |
|
155
|
|
|
* |
|
156
|
|
|
* @param string $project The project to retrieve details for |
|
157
|
|
|
* @param string $langCode The language code to retrieve details for |
|
158
|
|
|
* @param string[] $coordinators An array of coordinators for the language |
|
159
|
|
|
* @param bool $skipInvalidUsername If true, the API call does not fail and instead will return a list of invalid usernames |
|
160
|
|
|
* |
|
161
|
|
|
* @return ResponseInterface |
|
162
|
|
|
*/ |
|
163
|
3 |
|
public function updateCoordinators( |
|
164
|
|
|
string $project, |
|
165
|
|
|
string $langCode, |
|
166
|
|
|
array $coordinators, |
|
167
|
|
|
bool $skipInvalidUsername = false |
|
168
|
|
|
): ResponseInterface { |
|
169
|
3 |
|
return $this->updateTeam($project, $langCode, $coordinators, $skipInvalidUsername, 'coordinators'); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* Update a language within a project. |
|
174
|
|
|
* |
|
175
|
|
|
* @param string $slug The slug for the project |
|
176
|
|
|
* @param string $langCode The language code for the new language |
|
177
|
|
|
* @param string[] $coordinators An array of coordinators for the language |
|
178
|
|
|
* @param array $options Optional additional params to send with the request |
|
179
|
|
|
* |
|
180
|
|
|
* @return ResponseInterface |
|
181
|
|
|
* |
|
182
|
|
|
* @throws InvalidConfigurationException |
|
183
|
|
|
*/ |
|
184
|
3 |
|
public function updateLanguage( |
|
185
|
|
|
string $slug, |
|
186
|
|
|
string $langCode, |
|
187
|
|
|
array $coordinators, |
|
188
|
|
|
array $options = [] |
|
189
|
|
|
): ResponseInterface { |
|
190
|
|
|
// Make sure the $coordinators array is not empty |
|
191
|
3 |
|
if (!\count($coordinators)) { |
|
192
|
1 |
|
throw new InvalidConfigurationException('The coordinators array must contain at least one username.'); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
// Build the required request data. |
|
196
|
2 |
|
$data = ['coordinators' => $coordinators]; |
|
197
|
|
|
|
|
198
|
|
|
// Set the translators if present |
|
199
|
2 |
|
if (isset($options['translators'])) { |
|
200
|
1 |
|
$data['translators'] = $options['translators']; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
// Set the reviewers if present |
|
204
|
2 |
|
if (isset($options['reviewers'])) { |
|
205
|
1 |
|
$data['reviewers'] = $options['reviewers']; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
2 |
|
$request = $this->createRequest('PUT', $this->createUri("/api/2/project/$slug/language/$langCode/")); |
|
209
|
2 |
|
$request = $request->withBody($this->streamFactory->createStream(\json_encode($data))); |
|
210
|
2 |
|
$request = $request->withHeader('Content-Type', 'application/json'); |
|
211
|
|
|
|
|
212
|
2 |
|
return $this->client->sendRequest($request); |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* Update the reviewers for a language team in a project. |
|
217
|
|
|
* |
|
218
|
|
|
* @param string $project The project to retrieve details for |
|
219
|
|
|
* @param string $langCode The language code to retrieve details for |
|
220
|
|
|
* @param string[] $reviewers An array of reviewers for the language |
|
221
|
|
|
* @param bool $skipInvalidUsername If true, the API call does not fail and instead will return a list of invalid usernames |
|
222
|
|
|
* |
|
223
|
|
|
* @return ResponseInterface |
|
224
|
|
|
*/ |
|
225
|
3 |
|
public function updateReviewers(string $project, string $langCode, array $reviewers, bool $skipInvalidUsername = false): ResponseInterface |
|
226
|
|
|
{ |
|
227
|
3 |
|
return $this->updateTeam($project, $langCode, $reviewers, $skipInvalidUsername, 'reviewers'); |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
/** |
|
231
|
|
|
* Base method to update a given language team in a project. |
|
232
|
|
|
* |
|
233
|
|
|
* @param string $project The project to retrieve details for |
|
234
|
|
|
* @param string $langCode The language code to retrieve details for |
|
235
|
|
|
* @param string[] $members An array of the team members for the language |
|
236
|
|
|
* @param bool $skipInvalidUsername If true, the API call does not fail and instead will return a list of invalid usernames |
|
237
|
|
|
* @param string $team The team to update |
|
238
|
|
|
* |
|
239
|
|
|
* @return ResponseInterface |
|
240
|
|
|
* |
|
241
|
|
|
* @throws InvalidConfigurationException |
|
242
|
|
|
*/ |
|
243
|
9 |
|
protected function updateTeam( |
|
244
|
|
|
string $project, |
|
245
|
|
|
string $langCode, |
|
246
|
|
|
array $members, |
|
247
|
|
|
bool $skipInvalidUsername, |
|
248
|
|
|
string $team |
|
249
|
|
|
): ResponseInterface { |
|
250
|
|
|
// Make sure the $members array is not empty |
|
251
|
9 |
|
if (!\count($members)) { |
|
252
|
3 |
|
throw new InvalidConfigurationException('The team array must contain at least one username.'); |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
6 |
|
$uri = $this->createUri("/api/2/project/$project/language/$langCode/$team/"); |
|
256
|
|
|
|
|
257
|
6 |
|
if ($skipInvalidUsername) { |
|
258
|
3 |
|
$uri = $uri->withQuery('skip_invalid_username'); |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
6 |
|
$request = $this->createRequest('PUT', $uri); |
|
262
|
6 |
|
$request = $request->withBody($this->streamFactory->createStream(\json_encode($members))); |
|
263
|
6 |
|
$request = $request->withHeader('Content-Type', 'application/json'); |
|
264
|
|
|
|
|
265
|
6 |
|
return $this->client->sendRequest($request); |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
/** |
|
269
|
|
|
* Update the translators for a language team in a project. |
|
270
|
|
|
* |
|
271
|
|
|
* @param string $project The project to retrieve details for |
|
272
|
|
|
* @param string $langCode The language code to retrieve details for |
|
273
|
|
|
* @param string[] $translators An array of translators for the language |
|
274
|
|
|
* @param bool $skipInvalidUsername If true, the API call does not fail and instead will return a list of invalid usernames |
|
275
|
|
|
* |
|
276
|
|
|
* @return ResponseInterface |
|
277
|
|
|
*/ |
|
278
|
3 |
|
public function updateTranslators( |
|
279
|
|
|
string $project, |
|
280
|
|
|
string $langCode, |
|
281
|
|
|
array $translators, |
|
282
|
|
|
bool $skipInvalidUsername = false |
|
283
|
|
|
): ResponseInterface { |
|
284
|
3 |
|
return $this->updateTeam($project, $langCode, $translators, $skipInvalidUsername, 'translators'); |
|
285
|
|
|
} |
|
286
|
|
|
} |
|
287
|
|
|
|