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.net/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
|
|
|
$path = sprintf( |
63
|
|
|
"project/%s/upload-translation?key=%s", |
64
|
|
|
$this->client->getProjectIdentifier(), |
65
|
|
|
$this->client->getProjectApiKey() |
66
|
|
|
); |
67
|
|
|
|
68
|
|
|
$data[] = [ |
|
|
|
|
69
|
|
|
'name' => 'import_duplicates', |
70
|
|
|
'contents' => (int)$this->areDuplicatesImported |
71
|
|
|
]; |
72
|
|
|
$data[] = [ |
73
|
|
|
'name' => 'import_eq_suggestions', |
74
|
|
|
'contents' => (int)$this->areEqualSuggestionsImported |
75
|
|
|
]; |
76
|
|
|
$data[] = [ |
77
|
|
|
'name' => 'auto_approve_imported', |
78
|
|
|
'contents' => (int)$this->areImportsAutoApproved |
79
|
|
|
]; |
80
|
|
|
$data[] = [ |
81
|
|
|
'name' => 'language', |
82
|
|
|
'contents' => $this->locale |
83
|
|
|
]; |
84
|
|
|
|
85
|
|
|
if (null !== $this->branch) { |
86
|
|
|
$data[] = [ |
87
|
|
|
'name' => 'branch', |
88
|
|
|
'contents' => $this->branch |
89
|
|
|
]; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
foreach ($this->translations as $translation) { |
93
|
|
|
$data[] = [ |
94
|
|
|
'name' => 'files['.$translation->getCrowdinPath().']', |
95
|
|
|
'contents' => $this->fileReader->readTranslation($translation) |
96
|
|
|
]; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$data = ['multipart' => $data]; |
100
|
|
|
$response = $this->client->getHttpClient()->post($path, $data); |
101
|
|
|
|
102
|
|
|
return $response->getBody(); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param string $localPath |
107
|
|
|
* @param string $crowdinPath |
108
|
|
|
* @param string $exportPattern |
109
|
|
|
* @param string $title |
110
|
|
|
* |
111
|
|
|
* @return $this |
112
|
|
|
*/ |
113
|
|
View Code Duplication |
public function addTranslation($localPath, $crowdinPath, $exportPattern = null, $title = null) |
|
|
|
|
114
|
|
|
{ |
115
|
|
|
$translation = new Translation($localPath, $crowdinPath); |
116
|
|
|
$translation->setExportPattern($exportPattern); |
117
|
|
|
$translation->setTitle($title); |
118
|
|
|
|
119
|
|
|
$this->translations[] = $translation; |
120
|
|
|
|
121
|
|
|
return $this; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param Translation[] $translations |
126
|
|
|
* @return UploadTranslation |
127
|
|
|
*/ |
128
|
|
|
public function setTranslations(array $translations) |
129
|
|
|
{ |
130
|
|
|
$this->translations = $translations; |
131
|
|
|
|
132
|
|
|
return $this; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @return Translation[] |
137
|
|
|
*/ |
138
|
|
|
public function getTranslations() |
139
|
|
|
{ |
140
|
|
|
return $this->translations; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param bool $importsAutoApproved |
145
|
|
|
* |
146
|
|
|
* @throws InvalidArgumentException |
147
|
|
|
* |
148
|
|
|
* @return UploadTranslation |
149
|
|
|
*/ |
150
|
|
|
public function setImportsAutoApproved($importsAutoApproved) |
151
|
|
|
{ |
152
|
|
|
if (!is_bool($importsAutoApproved)) { |
153
|
|
|
throw new InvalidArgumentException('A boolean is required.'); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
$this->areImportsAutoApproved = $importsAutoApproved; |
157
|
|
|
|
158
|
|
|
return $this; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @return bool |
163
|
|
|
*/ |
164
|
|
|
public function areImportsAutoApproved() |
165
|
|
|
{ |
166
|
|
|
return $this->areImportsAutoApproved; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @param bool $duplicatesImported |
171
|
|
|
* |
172
|
|
|
* @throws InvalidArgumentException |
173
|
|
|
* |
174
|
|
|
* @return UploadTranslation |
175
|
|
|
*/ |
176
|
|
|
public function setDuplicatesImported($duplicatesImported) |
177
|
|
|
{ |
178
|
|
|
if (!is_bool($duplicatesImported)) { |
179
|
|
|
throw new InvalidArgumentException('A boolean is required.'); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
$this->areDuplicatesImported = $duplicatesImported; |
183
|
|
|
|
184
|
|
|
return $this; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @return bool |
189
|
|
|
*/ |
190
|
|
|
public function areDuplicatesImported() |
191
|
|
|
{ |
192
|
|
|
return $this->areDuplicatesImported; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @param bool $equalSuggestionsImported |
197
|
|
|
* |
198
|
|
|
* @throws InvalidArgumentException |
199
|
|
|
* |
200
|
|
|
* @return UploadTranslation |
201
|
|
|
*/ |
202
|
|
|
public function setEqualSuggestionsImported($equalSuggestionsImported) |
203
|
|
|
{ |
204
|
|
|
if (!is_bool($equalSuggestionsImported)) { |
205
|
|
|
throw new InvalidArgumentException('A boolean is required.'); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
$this->areEqualSuggestionsImported = $equalSuggestionsImported; |
209
|
|
|
|
210
|
|
|
return $this; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @return bool |
215
|
|
|
*/ |
216
|
|
|
public function areEqualSuggestionsImported() |
217
|
|
|
{ |
218
|
|
|
return $this->areEqualSuggestionsImported; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* @param string $locale |
223
|
|
|
* |
224
|
|
|
* @return UploadTranslation |
225
|
|
|
*/ |
226
|
|
|
public function setLocale($locale) |
227
|
|
|
{ |
228
|
|
|
$this->locale = $locale; |
229
|
|
|
|
230
|
|
|
return $this; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* @return string |
235
|
|
|
*/ |
236
|
|
|
public function getLocale() |
237
|
|
|
{ |
238
|
|
|
return $this->locale; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* @return string|null |
243
|
|
|
*/ |
244
|
|
|
public function getBranch() |
245
|
|
|
{ |
246
|
|
|
return $this->branch; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* @param string $branch |
251
|
|
|
* |
252
|
|
|
* @return UploadTranslation |
253
|
|
|
*/ |
254
|
|
|
public function setBranch($branch) |
255
|
|
|
{ |
256
|
|
|
$this->branch = $branch; |
257
|
|
|
|
258
|
|
|
return $this; |
259
|
|
|
} |
260
|
|
|
} |
261
|
|
|
|
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.