|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Akeneo\Crowdin\Api; |
|
4
|
|
|
|
|
5
|
|
|
use \InvalidArgumentException; |
|
6
|
|
|
use Akeneo\Crowdin\Client; |
|
7
|
|
|
use Akeneo\Crowdin\FileReader; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Upload existing translations to your Crowdin project. |
|
11
|
|
|
* |
|
12
|
|
|
* @author Julien Janvier <[email protected]> |
|
13
|
|
|
* @see https://crowdin.net/page/api/upload-translation |
|
14
|
|
|
*/ |
|
15
|
|
|
class UploadTranslation extends AbstractApi |
|
16
|
|
|
{ |
|
17
|
|
|
/** @var FileReader */ |
|
18
|
|
|
protected $fileReader; |
|
19
|
|
|
|
|
20
|
|
|
/** @var array */ |
|
21
|
|
|
protected $translations; |
|
22
|
|
|
|
|
23
|
|
|
/** @var string */ |
|
24
|
|
|
protected $locale; |
|
25
|
|
|
|
|
26
|
|
|
/** @var bool */ |
|
27
|
|
|
protected $areDuplicatesImported = false; |
|
28
|
|
|
|
|
29
|
|
|
/** @var bool */ |
|
30
|
|
|
protected $areEqualSuggestionsImported = false; |
|
31
|
|
|
|
|
32
|
|
|
/** @var bool */ |
|
33
|
|
|
protected $areImportsAutoApproved = false; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param Client $client |
|
37
|
|
|
* @param FileReader $fileReader |
|
38
|
|
|
*/ |
|
39
|
|
|
public function __construct(Client $client, FileReader $fileReader) |
|
40
|
|
|
{ |
|
41
|
|
|
parent::__construct($client); |
|
42
|
|
|
$this->fileReader = $fileReader; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* {@inheritdoc} |
|
47
|
|
|
*/ |
|
48
|
|
|
public function execute() |
|
49
|
|
|
{ |
|
50
|
|
|
if (0 === count($this->translations)) { |
|
51
|
|
|
throw new InvalidArgumentException('There are no translations to upload.'); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
if (null === $this->locale) { |
|
55
|
|
|
throw new InvalidArgumentException('Locale is not set.'); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
$path = sprintf( |
|
59
|
|
|
"project/%s/upload-translation?key=%s", |
|
60
|
|
|
$this->client->getProjectIdentifier(), |
|
61
|
|
|
$this->client->getProjectApiKey() |
|
62
|
|
|
); |
|
63
|
|
|
|
|
64
|
|
|
$data[] = [ |
|
|
|
|
|
|
65
|
|
|
'name' => 'import_duplicates', |
|
66
|
|
|
'contents' => (int)$this->areDuplicatesImported |
|
67
|
|
|
]; |
|
68
|
|
|
$data[] = [ |
|
69
|
|
|
'name' => 'import_eq_suggestions', |
|
70
|
|
|
'contents' => (int)$this->areEqualSuggestionsImported |
|
71
|
|
|
]; |
|
72
|
|
|
$data[] = [ |
|
73
|
|
|
'name' => 'auto_approve_imported', |
|
74
|
|
|
'contents' => (int)$this->areImportsAutoApproved |
|
75
|
|
|
]; |
|
76
|
|
|
$data[] = [ |
|
77
|
|
|
'name' => 'language', |
|
78
|
|
|
'contents' => $this->locale |
|
79
|
|
|
]; |
|
80
|
|
|
|
|
81
|
|
|
foreach ($this->translations as $crowdinPath => $localFile) { |
|
82
|
|
|
$data[] = [ |
|
83
|
|
|
'name' => 'files['.$crowdinPath.']', |
|
84
|
|
|
'contents' => $this->fileReader->readStream($localFile) |
|
85
|
|
|
]; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
$data = ['multipart' => $data]; |
|
89
|
|
|
$response = $this->client->getHttpClient()->post($path, $data); |
|
90
|
|
|
|
|
91
|
|
|
return $response->getBody(); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @param string $crowdinPath the Crowdin file path |
|
96
|
|
|
* @param string $localPath the local file path |
|
97
|
|
|
* |
|
98
|
|
|
* @throws InvalidArgumentException |
|
99
|
|
|
* |
|
100
|
|
|
* @return UploadTranslation |
|
101
|
|
|
*/ |
|
102
|
|
|
public function addTranslation($crowdinPath, $localPath) |
|
103
|
|
|
{ |
|
104
|
|
|
if (!file_exists($localPath)) { |
|
105
|
|
|
throw new InvalidArgumentException(sprintf('File %s does not exist.', $localPath)); |
|
106
|
|
|
} |
|
107
|
|
|
$this->translations[$crowdinPath] = $localPath; |
|
108
|
|
|
|
|
109
|
|
|
return $this; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @return array |
|
114
|
|
|
*/ |
|
115
|
|
|
public function getTranslations() |
|
116
|
|
|
{ |
|
117
|
|
|
return $this->translations; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @param bool $importsAutoApproved |
|
122
|
|
|
* |
|
123
|
|
|
* @throws InvalidArgumentException |
|
124
|
|
|
* |
|
125
|
|
|
* @return UploadTranslation |
|
126
|
|
|
*/ |
|
127
|
|
|
public function setImportsAutoApproved($importsAutoApproved) |
|
128
|
|
|
{ |
|
129
|
|
|
if (!is_bool($importsAutoApproved)) { |
|
130
|
|
|
throw new InvalidArgumentException('A boolean is required.'); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
$this->areImportsAutoApproved = $importsAutoApproved; |
|
134
|
|
|
|
|
135
|
|
|
return $this; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @return bool |
|
140
|
|
|
*/ |
|
141
|
|
|
public function areImportsAutoApproved() |
|
142
|
|
|
{ |
|
143
|
|
|
return $this->areImportsAutoApproved; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* @param bool $duplicatesImported |
|
148
|
|
|
* |
|
149
|
|
|
* @throws InvalidArgumentException |
|
150
|
|
|
* |
|
151
|
|
|
* @return UploadTranslation |
|
152
|
|
|
*/ |
|
153
|
|
|
public function setDuplicatesImported($duplicatesImported) |
|
154
|
|
|
{ |
|
155
|
|
|
if (!is_bool($duplicatesImported)) { |
|
156
|
|
|
throw new InvalidArgumentException('A boolean is required.'); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
$this->areDuplicatesImported = $duplicatesImported; |
|
160
|
|
|
|
|
161
|
|
|
return $this; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* @return bool |
|
166
|
|
|
*/ |
|
167
|
|
|
public function areDuplicatesImported() |
|
168
|
|
|
{ |
|
169
|
|
|
return $this->areDuplicatesImported; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* @param bool $equalSuggestionsImported |
|
174
|
|
|
* |
|
175
|
|
|
* @throws InvalidArgumentException |
|
176
|
|
|
* |
|
177
|
|
|
* @return UploadTranslation |
|
178
|
|
|
*/ |
|
179
|
|
|
public function setEqualSuggestionsImported($equalSuggestionsImported) |
|
180
|
|
|
{ |
|
181
|
|
|
if (!is_bool($equalSuggestionsImported)) { |
|
182
|
|
|
throw new InvalidArgumentException('A boolean is required.'); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
$this->areEqualSuggestionsImported = $equalSuggestionsImported; |
|
186
|
|
|
|
|
187
|
|
|
return $this; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* @return bool |
|
192
|
|
|
*/ |
|
193
|
|
|
public function areEqualSuggestionsImported() |
|
194
|
|
|
{ |
|
195
|
|
|
return $this->areEqualSuggestionsImported; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* @param string $locale |
|
200
|
|
|
* |
|
201
|
|
|
* @return UploadTranslation |
|
202
|
|
|
*/ |
|
203
|
|
|
public function setLocale($locale) |
|
204
|
|
|
{ |
|
205
|
|
|
$this->locale = $locale; |
|
206
|
|
|
|
|
207
|
|
|
return $this; |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* @return string |
|
212
|
|
|
*/ |
|
213
|
|
|
public function getLocale() |
|
214
|
|
|
{ |
|
215
|
|
|
return $this->locale; |
|
216
|
|
|
} |
|
217
|
|
|
} |
|
218
|
|
|
|
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
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey 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.