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
|
|
|
/** |
37
|
|
|
* @param Client $client |
38
|
|
|
* @param FileReader $fileReader |
39
|
|
|
*/ |
40
|
|
|
public function __construct(Client $client, FileReader $fileReader) |
41
|
|
|
{ |
42
|
|
|
parent::__construct($client); |
43
|
|
|
$this->fileReader = $fileReader; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritdoc} |
48
|
|
|
*/ |
49
|
|
|
public function execute() |
50
|
|
|
{ |
51
|
|
|
if (0 === count($this->translations)) { |
52
|
|
|
throw new InvalidArgumentException('There are no translations to upload.'); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if (null === $this->locale) { |
56
|
|
|
throw new InvalidArgumentException('Locale is not set.'); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$path = sprintf( |
60
|
|
|
"project/%s/upload-translation?key=%s", |
61
|
|
|
$this->client->getProjectIdentifier(), |
62
|
|
|
$this->client->getProjectApiKey() |
63
|
|
|
); |
64
|
|
|
|
65
|
|
|
$data[] = [ |
|
|
|
|
66
|
|
|
'name' => 'import_duplicates', |
67
|
|
|
'contents' => (int)$this->areDuplicatesImported |
68
|
|
|
]; |
69
|
|
|
$data[] = [ |
70
|
|
|
'name' => 'import_eq_suggestions', |
71
|
|
|
'contents' => (int)$this->areEqualSuggestionsImported |
72
|
|
|
]; |
73
|
|
|
$data[] = [ |
74
|
|
|
'name' => 'auto_approve_imported', |
75
|
|
|
'contents' => (int)$this->areImportsAutoApproved |
76
|
|
|
]; |
77
|
|
|
$data[] = [ |
78
|
|
|
'name' => 'language', |
79
|
|
|
'contents' => $this->locale |
80
|
|
|
]; |
81
|
|
|
|
82
|
|
|
foreach ($this->translations as $translation) { |
83
|
|
|
$data[] = [ |
84
|
|
|
'name' => 'files['.$translation->getCrowdinPath().']', |
85
|
|
|
'contents' => $this->fileReader->readTranslation($translation) |
86
|
|
|
]; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$data = ['multipart' => $data]; |
90
|
|
|
$response = $this->client->getHttpClient()->post($path, $data); |
91
|
|
|
|
92
|
|
|
return $response->getBody(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param string $localPath |
97
|
|
|
* @param string $crowdinPath |
98
|
|
|
* @param string $exportPattern |
99
|
|
|
* @param string $title |
100
|
|
|
* |
101
|
|
|
* @return $this |
102
|
|
|
*/ |
103
|
|
View Code Duplication |
public function addTranslation($localPath, $crowdinPath, $exportPattern = null, $title = null) |
|
|
|
|
104
|
|
|
{ |
105
|
|
|
$translation = new Translation($localPath, $crowdinPath); |
106
|
|
|
$translation->setExportPattern($exportPattern); |
107
|
|
|
$translation->setTitle($title); |
108
|
|
|
|
109
|
|
|
$this->translations[] = $translation; |
110
|
|
|
|
111
|
|
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param Translation[] $translations |
116
|
|
|
* @return UploadTranslation |
117
|
|
|
*/ |
118
|
|
|
public function setTranslations(array $translations) |
119
|
|
|
{ |
120
|
|
|
$this->translations = $translations; |
121
|
|
|
|
122
|
|
|
return $this; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @return Translation[] |
127
|
|
|
*/ |
128
|
|
|
public function getTranslations() |
129
|
|
|
{ |
130
|
|
|
return $this->translations; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param bool $importsAutoApproved |
135
|
|
|
* |
136
|
|
|
* @throws InvalidArgumentException |
137
|
|
|
* |
138
|
|
|
* @return UploadTranslation |
139
|
|
|
*/ |
140
|
|
|
public function setImportsAutoApproved($importsAutoApproved) |
141
|
|
|
{ |
142
|
|
|
if (!is_bool($importsAutoApproved)) { |
143
|
|
|
throw new InvalidArgumentException('A boolean is required.'); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
$this->areImportsAutoApproved = $importsAutoApproved; |
147
|
|
|
|
148
|
|
|
return $this; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @return bool |
153
|
|
|
*/ |
154
|
|
|
public function areImportsAutoApproved() |
155
|
|
|
{ |
156
|
|
|
return $this->areImportsAutoApproved; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @param bool $duplicatesImported |
161
|
|
|
* |
162
|
|
|
* @throws InvalidArgumentException |
163
|
|
|
* |
164
|
|
|
* @return UploadTranslation |
165
|
|
|
*/ |
166
|
|
|
public function setDuplicatesImported($duplicatesImported) |
167
|
|
|
{ |
168
|
|
|
if (!is_bool($duplicatesImported)) { |
169
|
|
|
throw new InvalidArgumentException('A boolean is required.'); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
$this->areDuplicatesImported = $duplicatesImported; |
173
|
|
|
|
174
|
|
|
return $this; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @return bool |
179
|
|
|
*/ |
180
|
|
|
public function areDuplicatesImported() |
181
|
|
|
{ |
182
|
|
|
return $this->areDuplicatesImported; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @param bool $equalSuggestionsImported |
187
|
|
|
* |
188
|
|
|
* @throws InvalidArgumentException |
189
|
|
|
* |
190
|
|
|
* @return UploadTranslation |
191
|
|
|
*/ |
192
|
|
|
public function setEqualSuggestionsImported($equalSuggestionsImported) |
193
|
|
|
{ |
194
|
|
|
if (!is_bool($equalSuggestionsImported)) { |
195
|
|
|
throw new InvalidArgumentException('A boolean is required.'); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
$this->areEqualSuggestionsImported = $equalSuggestionsImported; |
199
|
|
|
|
200
|
|
|
return $this; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @return bool |
205
|
|
|
*/ |
206
|
|
|
public function areEqualSuggestionsImported() |
207
|
|
|
{ |
208
|
|
|
return $this->areEqualSuggestionsImported; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* @param string $locale |
213
|
|
|
* |
214
|
|
|
* @return UploadTranslation |
215
|
|
|
*/ |
216
|
|
|
public function setLocale($locale) |
217
|
|
|
{ |
218
|
|
|
$this->locale = $locale; |
219
|
|
|
|
220
|
|
|
return $this; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* @return string |
225
|
|
|
*/ |
226
|
|
|
public function getLocale() |
227
|
|
|
{ |
228
|
|
|
return $this->locale; |
229
|
|
|
} |
230
|
|
|
} |
231
|
|
|
|
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.