Completed
Push — master ( 38271d...9dbb97 )
by Nikolai
02:43
created

Translation::export()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 2
eloc 6
nc 2
nop 1
1
<?php
2
/**
3
 * Crowdin API implementation in PHP.
4
 *
5
 * @copyright  Copyright (C) 2016 Nikolai Plath (elkuku)
6
 * @license    GNU General Public License version 2 or later
7
 */
8
9
namespace ElKuKu\Crowdin\Package;
10
11
use ElKuKu\Crowdin\Languagefile;
12
use ElKuKu\Crowdin\Package;
13
14
/**
15
 * Class Translation
16
 *
17
 * @since  1.0
18
 */
19
Class Translation extends Package
20
{
21
	/**
22
	 * Upload existing translations to your Crowdin project.
23
	 *
24
	 * @param   Languagefile  $languagefile            The translation object.
25
	 * @param   string        $language                The language tag.
26
	 * @param   boolean       $importDuplicates        Defines whether to add translation if there is
27
	 *                                                 the same translation previously added.
28
	 *                                                 Acceptable values are: 0 or 1. Default is 0.
29
	 * @param   boolean       $importEqualSuggestions  Defines whether to add translation if it is equal to
30
	 *                                                 source string at Crowdin.
31
	 *                                                 Acceptable values are: 0 or 1. Default is 0.
32
	 * @param   boolean       $autoImproveImports      Mark uploaded translations as approved.
33
	 *                                                 Acceptable values are: 0 or 1. Default is 0.
34
	 *
35
	 * @see     https://crowdin.com/page/api/upload-translation
36
	 * @since   1.0.1
37
	 *
38
	 * @return \Psr\Http\Message\ResponseInterface
39
	 */
40
	public function upload(
41
		Languagefile $languagefile, $language, $importDuplicates = false,
42
		$importEqualSuggestions = false, $autoImproveImports = false)
43
	{
44
		$data = [];
45
46
		$data[] = [
47
			'name'     => 'import_duplicates',
48
			'contents' => (int) $importDuplicates
49
		];
50
51
		$data[] = [
52
			'name'     => 'import_eq_suggestions',
53
			'contents' => (int) $importEqualSuggestions
54
		];
55
56
		$data[] = [
57
			'name'     => 'auto_approve_imported',
58
			'contents' => (int) $autoImproveImports
59
		];
60
61
		$data[] = [
62
			'name'     => 'language',
63
			'contents' => $language
64
		];
65
66
		$data[] = [
67
			'name'     => 'files[' . $languagefile->getCrowdinPath() . ']',
68
			'contents' => fopen($languagefile->getLocalPath(), 'r')
69
		];
70
71
		return $this->getHttpClient()
72
			->post($this->getBasePath('upload-translation'), ['multipart' => $data]);
73
	}
74
75
	/**
76
	 * Build ZIP archive with the latest translations. Please note that this method can be invoked
77
	 * only once per 30 minutes (there is no such restriction for organization plans). Also API
78
	 * call will be ignored if there were no changes in the project since previous export. You can
79
	 * see whether ZIP archive with latest translations was actually build by status attribute
80
	 * ("built" or "skipped") returned in response.
81
	 *
82
	 * @param   string  $branch  The name of related version branch.
83
	 *
84
	 * @since 1.0.4
85
	 * @see   https://crowdin.com/page/api/export
86
	 *
87
	 * @return \Psr\Http\Message\ResponseInterface
88
	 */
89
	public function export($branch = '')
90
	{
91
		$path = $this->getBasePath('export');
92
93
		if ('' !== $branch)
94
		{
95
			$path .= '&branch=' . $branch;
96
		}
97
98
		return $this->getHttpClient()
99
			->get($path);
100
	}
101
102
	/**
103
	 * Download ZIP file with translations. You can choose the language of translation
104
	 * you need or download all of them at once.
105
	 * Note: If you would like to download the most recent translations you may want
106
	 * to use export API method before downloading.
107
	 *
108
	 * @param   string  $package  Language code or "all" to download a bundle with translations to all languages.
109
	 * @param   string  $toPath   Local path where to download the translation package.
110
	 * @param   string  $branch   The name of related version branch.
111
	 *
112
	 * @since 1.0.4
113
	 * @see   https://crowdin.com/page/api/download
114
	 *
115
	 * @return \Psr\Http\Message\ResponseInterface
116
	 */
117
	public function download($package, $toPath, $branch = '')
118
	{
119
		$path = sprintf(
120
			'project/%s/download/%s?key=%s',
121
			$this->getProjectId(),
122
			$package,
123
			$this->getApiKey()
124
		);
125
126
		if ('' !== $branch)
127
		{
128
			$path .= '&branch=' . $branch;
129
		}
130
131
		return $this->getHttpClient()
132
			->get($path, ['sink' => $toPath]);
133
	}
134
135
	/**
136
	 * Track overall translation and proofreading progresses of each target language.
137
	 * Default response format is XML.
138
	 *
139
	 * @see    https://crowdin.com/page/api/status
140
	 * @since  1.0.4
141
	 *
142
	 * @return \Psr\Http\Message\ResponseInterface
143
	 */
144
	public function getStatus()
145
	{
146
		return $this->getHttpClient()
147
			->get($this->getBasePath('status'));
148
	}
149
}
150