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
|
|
|
* @return Translation[] |
126
|
|
|
*/ |
127
|
|
|
public function getTranslations() |
128
|
|
|
{ |
129
|
|
|
return $this->translations; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param bool $importsAutoApproved |
134
|
|
|
* |
135
|
|
|
* @throws InvalidArgumentException |
136
|
|
|
* |
137
|
|
|
* @return UploadTranslation |
138
|
|
|
*/ |
139
|
|
|
public function setImportsAutoApproved($importsAutoApproved) |
140
|
|
|
{ |
141
|
|
|
if (!is_bool($importsAutoApproved)) { |
142
|
|
|
throw new InvalidArgumentException('A boolean is required.'); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
$this->areImportsAutoApproved = $importsAutoApproved; |
146
|
|
|
|
147
|
|
|
return $this; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @return bool |
152
|
|
|
*/ |
153
|
|
|
public function areImportsAutoApproved() |
154
|
|
|
{ |
155
|
|
|
return $this->areImportsAutoApproved; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @param bool $duplicatesImported |
160
|
|
|
* |
161
|
|
|
* @throws InvalidArgumentException |
162
|
|
|
* |
163
|
|
|
* @return UploadTranslation |
164
|
|
|
*/ |
165
|
|
|
public function setDuplicatesImported($duplicatesImported) |
166
|
|
|
{ |
167
|
|
|
if (!is_bool($duplicatesImported)) { |
168
|
|
|
throw new InvalidArgumentException('A boolean is required.'); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
$this->areDuplicatesImported = $duplicatesImported; |
172
|
|
|
|
173
|
|
|
return $this; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @return bool |
178
|
|
|
*/ |
179
|
|
|
public function areDuplicatesImported() |
180
|
|
|
{ |
181
|
|
|
return $this->areDuplicatesImported; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @param bool $equalSuggestionsImported |
186
|
|
|
* |
187
|
|
|
* @throws InvalidArgumentException |
188
|
|
|
* |
189
|
|
|
* @return UploadTranslation |
190
|
|
|
*/ |
191
|
|
|
public function setEqualSuggestionsImported($equalSuggestionsImported) |
192
|
|
|
{ |
193
|
|
|
if (!is_bool($equalSuggestionsImported)) { |
194
|
|
|
throw new InvalidArgumentException('A boolean is required.'); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
$this->areEqualSuggestionsImported = $equalSuggestionsImported; |
198
|
|
|
|
199
|
|
|
return $this; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @return bool |
204
|
|
|
*/ |
205
|
|
|
public function areEqualSuggestionsImported() |
206
|
|
|
{ |
207
|
|
|
return $this->areEqualSuggestionsImported; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @param string $locale |
212
|
|
|
* |
213
|
|
|
* @return UploadTranslation |
214
|
|
|
*/ |
215
|
|
|
public function setLocale($locale) |
216
|
|
|
{ |
217
|
|
|
$this->locale = $locale; |
218
|
|
|
|
219
|
|
|
return $this; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* @return string |
224
|
|
|
*/ |
225
|
|
|
public function getLocale() |
226
|
|
|
{ |
227
|
|
|
return $this->locale; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* @return string|null |
232
|
|
|
*/ |
233
|
|
|
public function getBranch() |
234
|
|
|
{ |
235
|
|
|
return $this->branch; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* @param string $branch |
240
|
|
|
* |
241
|
|
|
* @return UploadTranslation |
242
|
|
|
*/ |
243
|
|
|
public function setBranch($branch) |
244
|
|
|
{ |
245
|
|
|
$this->branch = $branch; |
246
|
|
|
|
247
|
|
|
return $this; |
248
|
|
|
} |
249
|
|
|
} |
250
|
|
|
|
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.