1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace BabDev\Transifex; |
4
|
|
|
|
5
|
|
|
use Psr\Http\Message\ResponseInterface; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Transifex API Projects class. |
9
|
|
|
* |
10
|
|
|
* @link http://docs.transifex.com/api/projects/ |
11
|
|
|
*/ |
12
|
|
|
class Projects extends TransifexObject |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Build the data array to send with create and update requests. |
16
|
|
|
* |
17
|
|
|
* @param array $options Optional additional params to send with the request |
18
|
|
|
* |
19
|
|
|
* @return array |
20
|
|
|
*/ |
21
|
7 |
|
private function buildProjectRequest(array $options): array |
22
|
|
|
{ |
23
|
7 |
|
$data = []; |
24
|
|
|
|
25
|
|
|
// Valid options to check |
26
|
|
|
$validOptions = [ |
27
|
7 |
|
'long_description', |
28
|
|
|
'private', |
29
|
|
|
'homepage', |
30
|
|
|
'trans_instructions', |
31
|
|
|
'tags', |
32
|
|
|
'maintainers', |
33
|
|
|
'team', |
34
|
|
|
'auto_join', |
35
|
|
|
'license', |
36
|
|
|
'fill_up_resources', |
37
|
|
|
'repository_url', |
38
|
|
|
'organization', |
39
|
|
|
'archived', |
40
|
|
|
'type', |
41
|
|
|
]; |
42
|
|
|
|
43
|
|
|
// Loop through the valid options and if we have them, add them to the request data |
44
|
7 |
|
foreach ($validOptions as $option) { |
45
|
7 |
|
if (isset($options[$option])) { |
46
|
7 |
|
$data[$option] = $options[$option]; |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
// Set the license if present |
51
|
7 |
|
if (isset($options['license'])) { |
52
|
3 |
|
$this->checkLicense($options['license']); |
53
|
1 |
|
$data['license'] = $options['license']; |
54
|
|
|
} |
55
|
|
|
|
56
|
5 |
|
return $data; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Checks that a license is an accepted value. |
61
|
|
|
* |
62
|
|
|
* @param string $license The license to check |
63
|
|
|
* |
64
|
|
|
* @throws \InvalidArgumentException |
65
|
|
|
*/ |
66
|
4 |
|
private function checkLicense(string $license): void |
67
|
|
|
{ |
68
|
4 |
|
$accepted = ['proprietary', 'permissive_open_source', 'other_open_source']; |
69
|
|
|
|
70
|
|
|
// Ensure the license option is an allowed value |
71
|
4 |
|
if (!\in_array($license, $accepted)) { |
72
|
2 |
|
throw new \InvalidArgumentException( |
73
|
2 |
|
\sprintf( |
74
|
2 |
|
'The license %s is not valid, accepted license values are %s', |
75
|
2 |
|
$license, |
76
|
2 |
|
\implode(', ', $accepted) |
77
|
|
|
) |
78
|
|
|
); |
79
|
|
|
} |
80
|
2 |
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Create a project. |
84
|
|
|
* |
85
|
|
|
* @param string $name The name of the project |
86
|
|
|
* @param string $slug The slug for the project |
87
|
|
|
* @param string $description A description of the project |
88
|
|
|
* @param string $sourceLanguage The source language code for the project |
89
|
|
|
* @param array $options Optional additional params to send with the request |
90
|
|
|
* |
91
|
|
|
* @return ResponseInterface |
92
|
|
|
* |
93
|
|
|
* @throws \InvalidArgumentException |
94
|
|
|
*/ |
95
|
4 |
|
public function createProject( |
96
|
|
|
string $name, |
97
|
|
|
string $slug, |
98
|
|
|
string $description, |
99
|
|
|
string $sourceLanguage, |
100
|
|
|
array $options = [] |
101
|
|
|
): ResponseInterface { |
102
|
|
|
// Build the request data. |
103
|
4 |
|
$data = \array_merge( |
104
|
|
|
[ |
105
|
4 |
|
'name' => $name, |
106
|
4 |
|
'slug' => $slug, |
107
|
4 |
|
'description' => $description, |
108
|
4 |
|
'source_language_code' => $sourceLanguage, |
109
|
|
|
], |
110
|
4 |
|
$this->buildProjectRequest($options) |
111
|
|
|
); |
112
|
|
|
|
113
|
|
|
// Check mandatory fields. |
114
|
3 |
|
if (!isset($data['license']) || \in_array($data['license'], ['permissive_open_source', 'other_open_source'])) { |
115
|
3 |
|
if (!isset($data['repository_url'])) { |
116
|
1 |
|
throw new \InvalidArgumentException( |
117
|
1 |
|
'If a project is denoted either as permissive_open_source or other_open_source, the field repository_url is mandatory and should contain a link to the public repository of the project to be created.' |
118
|
|
|
); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
2 |
|
$request = $this->createRequest('POST', $this->createUri('/api/2/projects/')); |
123
|
2 |
|
$request = $request->withBody($this->streamFactory->createStream(\json_encode($data))); |
124
|
2 |
|
$request = $request->withHeader('Content-Type', 'application/json'); |
125
|
|
|
|
126
|
2 |
|
return $this->client->sendRequest($request); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Delete a project. |
131
|
|
|
* |
132
|
|
|
* @param string $slug The slug for the resource |
133
|
|
|
* |
134
|
|
|
* @return ResponseInterface |
135
|
|
|
*/ |
136
|
2 |
|
public function deleteProject(string $slug): ResponseInterface |
137
|
|
|
{ |
138
|
2 |
|
return $this->client->sendRequest($this->createRequest('DELETE', $this->createUri("/api/2/project/$slug"))); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Get a list of projects belonging to an organization. |
143
|
|
|
* |
144
|
|
|
* @return ResponseInterface |
145
|
|
|
*/ |
146
|
2 |
|
public function getOrganizationProjects(string $organization): ResponseInterface |
147
|
|
|
{ |
148
|
|
|
// This API endpoint uses the newer `api.transifex.com` subdomain, only change if the default www was given |
149
|
2 |
|
if (!$this->getOption('base_uri') || $this->getOption('base_uri') === 'https://www.transifex.com') { |
150
|
2 |
|
$this->setOption('base_uri', 'https://api.transifex.com'); |
151
|
|
|
} |
152
|
|
|
|
153
|
2 |
|
return $this->client->sendRequest($this->createRequest('GET', $this->createUri("/organizations/$organization/projects/"))); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Get information about a project. |
158
|
|
|
* |
159
|
|
|
* @param string $project The project to retrieve details for |
160
|
|
|
* @param bool $details True to retrieve additional project details |
161
|
|
|
* |
162
|
|
|
* @return ResponseInterface |
163
|
|
|
*/ |
164
|
2 |
|
public function getProject(string $project, bool $details = false): ResponseInterface |
165
|
|
|
{ |
166
|
2 |
|
$uri = $this->createUri("/api/2/project/$project/"); |
167
|
|
|
|
168
|
2 |
|
if ($details) { |
169
|
2 |
|
$uri = $uri->withQuery('details'); |
170
|
|
|
} |
171
|
|
|
|
172
|
2 |
|
return $this->client->sendRequest($this->createRequest('GET', $uri)); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Get a list of projects the user is part of. |
177
|
|
|
* |
178
|
|
|
* @return ResponseInterface |
179
|
|
|
*/ |
180
|
2 |
|
public function getProjects(): ResponseInterface |
181
|
|
|
{ |
182
|
2 |
|
return $this->client->sendRequest($this->createRequest('GET', $this->createUri('/api/2/projects/'))); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Update a project. |
187
|
|
|
* |
188
|
|
|
* @param string $slug The slug for the project |
189
|
|
|
* @param array $options Additional params to send with the request |
190
|
|
|
* |
191
|
|
|
* @return ResponseInterface |
192
|
|
|
* |
193
|
|
|
* @throws \RuntimeException |
194
|
|
|
*/ |
195
|
4 |
|
public function updateProject(string $slug, array $options): ResponseInterface |
196
|
|
|
{ |
197
|
4 |
|
$data = $this->buildProjectRequest($options); |
198
|
|
|
|
199
|
|
|
// Make sure we actually have data to send |
200
|
3 |
|
if (empty($data)) { |
201
|
1 |
|
throw new \RuntimeException('There is no data to send to Transifex.'); |
202
|
|
|
} |
203
|
|
|
|
204
|
2 |
|
$request = $this->createRequest('PUT', $this->createUri("/api/2/project/$slug/")); |
205
|
2 |
|
$request = $request->withBody($this->streamFactory->createStream(\json_encode($data))); |
206
|
2 |
|
$request = $request->withHeader('Content-Type', 'application/json'); |
207
|
|
|
|
208
|
2 |
|
return $this->client->sendRequest($request); |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|