1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Akeneo\Crowdin\Api; |
4
|
|
|
|
5
|
|
|
use \InvalidArgumentException; |
6
|
|
|
use Akeneo\Crowdin\Client; |
7
|
|
|
use Akeneo\Crowdin\FileReader; |
8
|
|
|
use Akeneo\Crowdin\Translation; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Upload existing translations to your Crowdin project. |
12
|
|
|
* |
13
|
|
|
* @author Julien Janvier <[email protected]> |
14
|
|
|
* @see https://crowdin.com/page/api/upload-translation |
15
|
|
|
*/ |
16
|
|
|
class UploadTranslation extends AbstractApi |
17
|
|
|
{ |
18
|
|
|
/** @var FileReader */ |
19
|
|
|
protected $fileReader; |
20
|
|
|
|
21
|
|
|
/** @var Translation[] */ |
22
|
|
|
protected $translations; |
23
|
|
|
|
24
|
|
|
/** @var string */ |
25
|
|
|
protected $locale; |
26
|
|
|
|
27
|
|
|
/** @var bool */ |
28
|
|
|
protected $areDuplicatesImported = false; |
29
|
|
|
|
30
|
|
|
/** @var bool */ |
31
|
|
|
protected $areEqualSuggestionsImported = false; |
32
|
|
|
|
33
|
|
|
/** @var bool */ |
34
|
|
|
protected $areImportsAutoApproved = false; |
35
|
|
|
|
36
|
|
|
/** @var string */ |
37
|
|
|
protected $branch; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param Client $client |
41
|
|
|
* @param FileReader $fileReader |
42
|
|
|
*/ |
43
|
|
|
public function __construct(Client $client, FileReader $fileReader) |
44
|
|
|
{ |
45
|
|
|
parent::__construct($client); |
46
|
|
|
$this->fileReader = $fileReader; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
|
|
public function execute() |
53
|
|
|
{ |
54
|
|
|
if (0 === count($this->translations)) { |
55
|
|
|
throw new InvalidArgumentException('There are no translations to upload.'); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if (null === $this->locale) { |
59
|
|
|
throw new InvalidArgumentException('Locale is not set.'); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$this->addUrlParameter('key', $this->client->getProjectApiKey()); |
63
|
|
|
|
64
|
|
|
$path = sprintf( |
65
|
|
|
"project/%s/upload-translation?%s", |
66
|
|
|
$this->client->getProjectIdentifier(), |
67
|
|
|
$this->getUrlQueryString() |
68
|
|
|
); |
69
|
|
|
|
70
|
|
|
$data[] = [ |
|
|
|
|
71
|
|
|
'name' => 'import_duplicates', |
72
|
|
|
'contents' => (int)$this->areDuplicatesImported |
73
|
|
|
]; |
74
|
|
|
$data[] = [ |
75
|
|
|
'name' => 'import_eq_suggestions', |
76
|
|
|
'contents' => (int)$this->areEqualSuggestionsImported |
77
|
|
|
]; |
78
|
|
|
$data[] = [ |
79
|
|
|
'name' => 'auto_approve_imported', |
80
|
|
|
'contents' => (int)$this->areImportsAutoApproved |
81
|
|
|
]; |
82
|
|
|
$data[] = [ |
83
|
|
|
'name' => 'language', |
84
|
|
|
'contents' => $this->locale |
85
|
|
|
]; |
86
|
|
|
|
87
|
|
|
if (null !== $this->branch) { |
88
|
|
|
$data[] = [ |
89
|
|
|
'name' => 'branch', |
90
|
|
|
'contents' => $this->branch |
91
|
|
|
]; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
foreach ($this->translations as $translation) { |
95
|
|
|
$data[] = [ |
96
|
|
|
'name' => 'files['.$translation->getCrowdinPath().']', |
97
|
|
|
'contents' => $this->fileReader->readTranslation($translation) |
98
|
|
|
]; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$data = ['multipart' => $data]; |
102
|
|
|
$response = $this->client->getHttpClient()->post($path, $data); |
103
|
|
|
|
104
|
|
|
return $response->getBody(); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param string $localPath |
109
|
|
|
* @param string $crowdinPath |
110
|
|
|
* @param string $exportPattern |
111
|
|
|
* @param string $title |
112
|
|
|
* |
113
|
|
|
* @return $this |
114
|
|
|
*/ |
115
|
|
View Code Duplication |
public function addTranslation($localPath, $crowdinPath, $exportPattern = null, $title = null) |
|
|
|
|
116
|
|
|
{ |
117
|
|
|
$translation = new Translation($localPath, $crowdinPath); |
118
|
|
|
$translation->setExportPattern($exportPattern); |
119
|
|
|
$translation->setTitle($title); |
120
|
|
|
|
121
|
|
|
$this->translations[] = $translation; |
122
|
|
|
|
123
|
|
|
return $this; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param Translation[] $translations |
128
|
|
|
* @return UploadTranslation |
129
|
|
|
*/ |
130
|
|
|
public function setTranslations(array $translations) |
131
|
|
|
{ |
132
|
|
|
$this->translations = $translations; |
133
|
|
|
|
134
|
|
|
return $this; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @return Translation[] |
139
|
|
|
*/ |
140
|
|
|
public function getTranslations() |
141
|
|
|
{ |
142
|
|
|
return $this->translations; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param bool $importsAutoApproved |
147
|
|
|
* |
148
|
|
|
* @throws InvalidArgumentException |
149
|
|
|
* |
150
|
|
|
* @return UploadTranslation |
151
|
|
|
*/ |
152
|
|
|
public function setImportsAutoApproved($importsAutoApproved) |
153
|
|
|
{ |
154
|
|
|
if (!is_bool($importsAutoApproved)) { |
155
|
|
|
throw new InvalidArgumentException('A boolean is required.'); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
$this->areImportsAutoApproved = $importsAutoApproved; |
159
|
|
|
|
160
|
|
|
return $this; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @return bool |
165
|
|
|
*/ |
166
|
|
|
public function areImportsAutoApproved() |
167
|
|
|
{ |
168
|
|
|
return $this->areImportsAutoApproved; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @param bool $duplicatesImported |
173
|
|
|
* |
174
|
|
|
* @throws InvalidArgumentException |
175
|
|
|
* |
176
|
|
|
* @return UploadTranslation |
177
|
|
|
*/ |
178
|
|
|
public function setDuplicatesImported($duplicatesImported) |
179
|
|
|
{ |
180
|
|
|
if (!is_bool($duplicatesImported)) { |
181
|
|
|
throw new InvalidArgumentException('A boolean is required.'); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
$this->areDuplicatesImported = $duplicatesImported; |
185
|
|
|
|
186
|
|
|
return $this; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @return bool |
191
|
|
|
*/ |
192
|
|
|
public function areDuplicatesImported() |
193
|
|
|
{ |
194
|
|
|
return $this->areDuplicatesImported; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @param bool $equalSuggestionsImported |
199
|
|
|
* |
200
|
|
|
* @throws InvalidArgumentException |
201
|
|
|
* |
202
|
|
|
* @return UploadTranslation |
203
|
|
|
*/ |
204
|
|
|
public function setEqualSuggestionsImported($equalSuggestionsImported) |
205
|
|
|
{ |
206
|
|
|
if (!is_bool($equalSuggestionsImported)) { |
207
|
|
|
throw new InvalidArgumentException('A boolean is required.'); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
$this->areEqualSuggestionsImported = $equalSuggestionsImported; |
211
|
|
|
|
212
|
|
|
return $this; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* @return bool |
217
|
|
|
*/ |
218
|
|
|
public function areEqualSuggestionsImported() |
219
|
|
|
{ |
220
|
|
|
return $this->areEqualSuggestionsImported; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* @param string $locale |
225
|
|
|
* |
226
|
|
|
* @return UploadTranslation |
227
|
|
|
*/ |
228
|
|
|
public function setLocale($locale) |
229
|
|
|
{ |
230
|
|
|
$this->locale = $locale; |
231
|
|
|
|
232
|
|
|
return $this; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* @return string |
237
|
|
|
*/ |
238
|
|
|
public function getLocale() |
239
|
|
|
{ |
240
|
|
|
return $this->locale; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* @return string|null |
245
|
|
|
*/ |
246
|
|
|
public function getBranch() |
247
|
|
|
{ |
248
|
|
|
return $this->branch; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* @param string $branch |
253
|
|
|
* |
254
|
|
|
* @return UploadTranslation |
255
|
|
|
*/ |
256
|
|
|
public function setBranch($branch) |
257
|
|
|
{ |
258
|
|
|
$this->branch = $branch; |
259
|
|
|
|
260
|
|
|
return $this; |
261
|
|
|
} |
262
|
|
|
} |
263
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.