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