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\Model\Translation\Index; |
13
|
|
|
use FAPI\PhraseApp\Model\Translation\TranslationCreated; |
14
|
|
|
use FAPI\PhraseApp\Model\Translation\TranslationUpdated; |
15
|
|
|
use Psr\Http\Message\ResponseInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @author Sascha-Oliver Prolic <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
class Translation extends HttpApi |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Index a locale. |
24
|
|
|
* |
25
|
|
|
* @param string $projectKey |
26
|
|
|
* @param string $localeId |
27
|
|
|
* @param array $params |
28
|
|
|
* |
29
|
|
|
* @return mixed|ResponseInterface |
30
|
|
|
*/ |
31
|
|
View Code Duplication |
public function indexLocale(string $projectKey, string $localeId, array $params = []) |
|
|
|
|
32
|
|
|
{ |
33
|
|
|
if (isset($params['tags'])) { |
34
|
|
|
$params['q'] = 'tags:'.$params['tags']; |
35
|
|
|
unset($params['tags']); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
$response = $this->httpGet(sprintf('/api/v2/projects/%s/locales/%s/translations', $projectKey, $localeId), $params); |
39
|
|
|
|
40
|
|
|
if (!$this->hydrator) { |
41
|
|
|
return $response; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
if ($response->getStatusCode() !== 200 && $response->getStatusCode() !== 201) { |
45
|
|
|
$this->handleErrors($response); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
return $this->hydrator->hydrate($response, Index::class); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Create a translation. |
53
|
|
|
* |
54
|
|
|
* @param string $projectKey |
55
|
|
|
* @param string $keyId |
56
|
|
|
* @param string $content |
57
|
|
|
* @param array $params |
58
|
|
|
* |
59
|
|
|
* @return mixed|ResponseInterface |
60
|
|
|
*/ |
61
|
|
|
public function create(string $projectKey, string $localeId, string $keyId, string $content, array $params = []) |
62
|
|
|
{ |
63
|
|
|
$params['locale_id'] = $localeId; |
64
|
|
|
$params['key_id'] = $keyId; |
65
|
|
|
$params['content'] = $content; |
66
|
|
|
|
67
|
|
|
$response = $this->httpPost(sprintf('/api/v2/projects/%s/translations', $projectKey), $params); |
68
|
|
|
|
69
|
|
|
if (!$this->hydrator) { |
70
|
|
|
return $response; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if ($response->getStatusCode() !== 200 && $response->getStatusCode() !== 201) { |
74
|
|
|
$this->handleErrors($response); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $this->hydrator->hydrate($response, TranslationCreated::class); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Update a translation. |
82
|
|
|
* |
83
|
|
|
* @param string $projectKey |
84
|
|
|
* @param string $translationId |
85
|
|
|
* @param string $content |
86
|
|
|
* @param array $params |
87
|
|
|
* |
88
|
|
|
* @return mixed|ResponseInterface |
89
|
|
|
*/ |
90
|
|
View Code Duplication |
public function update(string $projectKey, string $translationId, string $content, array $params = []) |
|
|
|
|
91
|
|
|
{ |
92
|
|
|
$params['content'] = $content; |
93
|
|
|
|
94
|
|
|
$response = $this->httpPatch(sprintf('/api/v2/projects/%s/translations/%s', $projectKey, $translationId), $params); |
95
|
|
|
|
96
|
|
|
if (!$this->hydrator) { |
97
|
|
|
return $response; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
if ($response->getStatusCode() !== 200 && $response->getStatusCode() !== 201) { |
101
|
|
|
$this->handleErrors($response); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $this->hydrator->hydrate($response, TranslationUpdated::class); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* List translations for a specific key. |
109
|
|
|
* |
110
|
|
|
* @param string $projectKey |
111
|
|
|
* @param string $keyId |
112
|
|
|
* @param array $params |
113
|
|
|
* |
114
|
|
|
* @return mixed|ResponseInterface |
115
|
|
|
*/ |
116
|
|
View Code Duplication |
public function indexKey(string $projectKey, string $keyId, array $params = []) |
|
|
|
|
117
|
|
|
{ |
118
|
|
|
if (isset($params['tags'])) { |
119
|
|
|
$params['q'] = 'tags:'.$params['tags']; |
120
|
|
|
unset($params['tags']); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
$response = $this->httpGet(sprintf('/api/v2/projects/%s/keys/%s/translations', $projectKey, $keyId), $params); |
124
|
|
|
|
125
|
|
|
if (!$this->hydrator) { |
126
|
|
|
return $response; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
if ($response->getStatusCode() !== 200 && $response->getStatusCode() !== 201) { |
130
|
|
|
$this->handleErrors($response); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return $this->hydrator->hydrate($response, Index::class); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
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.