|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This software may be modified and distributed under the terms |
|
7
|
|
|
* of the MIT license. See the LICENSE file for details. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace FAPI\PhraseApp\Api; |
|
11
|
|
|
|
|
12
|
|
|
use FAPI\PhraseApp\Exception; |
|
13
|
|
|
use FAPI\PhraseApp\Model\Locale\Uploaded; |
|
14
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @author Sascha-Oliver Prolic <[email protected]> |
|
18
|
|
|
*/ |
|
19
|
|
|
class Upload extends HttpApi |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* Upload a locale. |
|
23
|
|
|
* |
|
24
|
|
|
* @param string $projectKey |
|
25
|
|
|
* @param string $ext |
|
26
|
|
|
* @param string $filename |
|
27
|
|
|
* @param array $params |
|
28
|
|
|
* |
|
29
|
|
|
* @throws Exception |
|
30
|
|
|
* |
|
31
|
|
|
* @return string|ResponseInterface |
|
32
|
|
|
*/ |
|
33
|
|
|
public function upload(string $projectKey, string $ext, string $filename, array $params) |
|
|
|
|
|
|
34
|
|
|
{ |
|
35
|
|
|
if (!file_exists($filename)) { |
|
36
|
|
|
throw new Exception\InvalidArgumentException('file '.$filename.' not found'); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
if (!isset($params['locale_id'])) { |
|
40
|
|
|
throw new Exception\InvalidArgumentException('locale_id is missing in params'); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
$postData = [ |
|
44
|
|
|
['name' => 'file', 'content' => file_get_contents($filename), 'filename' => basename($filename)], |
|
45
|
|
|
['name' => 'file_format', 'content' => $ext], |
|
46
|
|
|
['name' => 'locale_id', 'content' => $params['locale_id']], |
|
47
|
|
|
]; |
|
48
|
|
|
|
|
49
|
|
|
if (isset($params['tags'])) { |
|
50
|
|
|
$postData[] = ['name' => 'tags', 'content' => $params['tags']]; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
$response = $this->httpPostRaw(sprintf('/api/v2/projects/%s/uploads', $projectKey), $postData, [ |
|
54
|
|
|
'Content-Type' => 'application/x-www-form-urlencoded', |
|
55
|
|
|
]); |
|
56
|
|
|
|
|
57
|
|
|
if (!$this->hydrator) { |
|
58
|
|
|
return $response; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
if ($response->getStatusCode() !== 200 && $response->getStatusCode() !== 201) { |
|
62
|
|
|
$this->handleErrors($response); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return $this->hydrator->hydrate($response, Uploaded::class); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|