1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Crowdin API implementation in PHP. |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (C) 2016 Nikolai Plath (elkuku) |
6
|
|
|
* @license WTFPL - See license.txt |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace ElKuKu\Crowdin\Package; |
10
|
|
|
|
11
|
|
|
use ElKuKu\Crowdin\Package; |
12
|
|
|
|
13
|
|
|
use Psr\Http\Message\ResponseInterface; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class Memory |
17
|
|
|
* |
18
|
|
|
* @since 1.0.5 |
19
|
|
|
*/ |
20
|
|
|
Class Memory extends Package |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Download Crowdin project Translation Memory as TMX file. |
24
|
|
|
* |
25
|
|
|
* @param string $toPath Export to path. |
26
|
|
|
* @param boolean $includeAssigned Defines whether the assigned TMs should be included in downloaded TMX file. |
27
|
|
|
* Acceptable values are: 0, 1. |
28
|
|
|
* Default is 1. |
29
|
|
|
* @param boolean $sourceLanguage Defines a source language for language pair. Сrowdin language code should be used. |
30
|
|
|
* @param boolean $targetLanguage Defines a target language for language pair. Сrowdin language code should be used. |
31
|
|
|
* |
32
|
|
|
* @since 1.0.5 |
33
|
|
|
* @see https://crowdin.com/page/api/download-tm |
34
|
|
|
* |
35
|
|
|
* @return ResponseInterface |
36
|
|
|
*/ |
37
|
|
|
public function download(string $toPath, bool $includeAssigned = true, string $sourceLanguage = '', string $targetLanguage = '') : ResponseInterface |
38
|
|
|
{ |
39
|
|
|
$url = $this->getBasePath('download-tm'); |
40
|
|
|
if ($includeAssigned) { |
41
|
|
|
$url .= '&include_assigned=1'; |
42
|
|
|
} |
43
|
|
|
if (!empty($sourceLanguage)) { |
44
|
|
|
$url .= '&source_language=' . $sourceLanguage; |
45
|
|
|
} |
46
|
|
|
if (!empty($targetLanguage)) { |
47
|
|
|
$url .= '&target_language=' . $targetLanguage; |
48
|
|
|
} |
49
|
|
|
return $this->getHttpClient() |
50
|
|
|
->get($url, ['sink' => $toPath]); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Upload your Translation Memory for Crowdin Project in TMX file format. |
55
|
|
|
* |
56
|
|
|
* @param string $file Full path to file to upload. |
57
|
|
|
* |
58
|
|
|
* @since 1.0.5 |
59
|
|
|
* @see https://crowdin.com/page/api/upload-tm |
60
|
|
|
* |
61
|
|
|
* @return ResponseInterface |
62
|
|
|
*/ |
63
|
2 |
View Code Duplication |
public function upload(string $file) : ResponseInterface |
|
|
|
|
64
|
|
|
{ |
65
|
2 |
|
if (false === file_exists($file)) |
66
|
|
|
{ |
67
|
1 |
|
throw new \UnexpectedValueException('File not found for upload'); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$data = [[ |
71
|
1 |
|
'name' => 'file', |
72
|
1 |
|
'contents' => fopen($file, 'r') |
73
|
|
|
]]; |
74
|
|
|
|
75
|
1 |
|
return $this->getHttpClient() |
76
|
1 |
|
->post($this->getBasePath('upload-tm'), ['multipart' => $data]); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.