1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* BabDev Transifex Package |
5
|
|
|
* |
6
|
|
|
* (c) Michael Babker <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace BabDev\Transifex; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Transifex API Resources class. |
16
|
|
|
* |
17
|
|
|
* @link http://docs.transifex.com/developer/api/resources |
18
|
|
|
*/ |
19
|
|
|
class Resources extends TransifexObject |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Method to create a resource. |
23
|
|
|
* |
24
|
|
|
* @param string $project The slug for the project |
25
|
|
|
* @param string $name The name of the resource |
26
|
|
|
* @param string $slug The slug for the resource |
27
|
|
|
* @param string $fileType The file type of the resource |
28
|
|
|
* @param array $options Optional additional params to send with the request |
29
|
|
|
* |
30
|
|
|
* @return \Joomla\Http\Response |
31
|
|
|
*/ |
32
|
3 |
|
public function createResource($project, $name, $slug, $fileType, array $options = []) |
33
|
|
|
{ |
34
|
|
|
// Build the request path. |
35
|
3 |
|
$path = '/project/' . $project . '/resources/'; |
36
|
|
|
|
37
|
|
|
// Build the required request data. |
38
|
|
|
$data = [ |
39
|
3 |
|
'name' => $name, |
40
|
3 |
|
'slug' => $slug, |
41
|
3 |
|
'i18n_type' => $fileType, |
42
|
3 |
|
]; |
43
|
|
|
|
44
|
|
|
// Set the accept translations flag if provided |
45
|
3 |
|
if (isset($options['accept_translations'])) { |
46
|
2 |
|
$data['accept_translations'] = $options['accept_translations']; |
47
|
2 |
|
} |
48
|
|
|
|
49
|
|
|
// Set the resource category if provided |
50
|
3 |
|
if (isset($options['category'])) { |
51
|
2 |
|
$data['category'] = $options['category']; |
52
|
2 |
|
} |
53
|
|
|
|
54
|
|
|
// Set a resource priority if provided |
55
|
3 |
|
if (isset($options['priority'])) { |
56
|
2 |
|
$data['priority'] = $options['priority']; |
57
|
2 |
|
} |
58
|
|
|
|
59
|
|
|
// Attach the resource data if provided as a string |
60
|
3 |
|
if (isset($options['content'])) { |
61
|
2 |
|
$data['content'] = $options['content']; |
62
|
3 |
|
} elseif (isset($options['file'])) { |
63
|
1 |
|
$data['content'] = file_get_contents($options['file']); |
64
|
1 |
|
} |
65
|
|
|
|
66
|
|
|
// Send the request. |
67
|
3 |
|
return $this->processResponse( |
68
|
3 |
|
$this->client->post( |
69
|
3 |
|
$this->fetchUrl($path), |
70
|
3 |
|
json_encode($data), |
71
|
3 |
|
['Content-Type' => 'application/json'] |
72
|
3 |
|
), |
73
|
|
|
201 |
74
|
3 |
|
); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Method to delete a resource within a project. |
79
|
|
|
* |
80
|
|
|
* @param string $project The project the resource is part of |
81
|
|
|
* @param string $resource The resource slug within the project |
82
|
|
|
* |
83
|
|
|
* @return \Joomla\Http\Response |
84
|
|
|
*/ |
85
|
2 |
View Code Duplication |
public function deleteResource($project, $resource) |
|
|
|
|
86
|
|
|
{ |
87
|
|
|
// Build the request path. |
88
|
2 |
|
$path = '/project/' . $project . '/resource/' . $resource; |
89
|
|
|
|
90
|
|
|
// Send the request. |
91
|
2 |
|
return $this->processResponse($this->client->delete($this->fetchUrl($path)), 204); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Method to get information about a resource within a project. |
96
|
|
|
* |
97
|
|
|
* @param string $project The project the resource is part of |
98
|
|
|
* @param string $resource The resource slug within the project |
99
|
|
|
* @param bool $details True to retrieve additional project details |
100
|
|
|
* |
101
|
|
|
* @return \Joomla\Http\Response |
102
|
|
|
*/ |
103
|
2 |
View Code Duplication |
public function getResource($project, $resource, $details = false) |
|
|
|
|
104
|
|
|
{ |
105
|
|
|
// Build the request path. |
106
|
2 |
|
$path = '/project/' . $project . '/resource/' . $resource . '/'; |
107
|
|
|
|
108
|
2 |
|
if ($details) { |
109
|
2 |
|
$path .= '?details'; |
110
|
2 |
|
} |
111
|
|
|
|
112
|
|
|
// Send the request. |
113
|
2 |
|
return $this->processResponse($this->client->get($this->fetchUrl($path))); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Method to get the content of a resource within a project. |
118
|
|
|
* |
119
|
|
|
* @param string $project The project the resource is part of |
120
|
|
|
* @param string $resource The resource slug within the project |
121
|
|
|
* |
122
|
|
|
* @return \Joomla\Http\Response |
123
|
|
|
*/ |
124
|
2 |
View Code Duplication |
public function getResourceContent($project, $resource) |
|
|
|
|
125
|
|
|
{ |
126
|
|
|
// Build the request path. |
127
|
2 |
|
$path = '/project/' . $project . '/resource/' . $resource . '/content/'; |
128
|
|
|
|
129
|
|
|
// Send the request. |
130
|
2 |
|
return $this->processResponse($this->client->get($this->fetchUrl($path))); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Method to get information about a project's resources. |
135
|
|
|
* |
136
|
|
|
* @param string $project The project to retrieve details for |
137
|
|
|
* |
138
|
|
|
* @return \Joomla\Http\Response |
139
|
|
|
*/ |
140
|
2 |
View Code Duplication |
public function getResources($project) |
|
|
|
|
141
|
|
|
{ |
142
|
|
|
// Build the request path. |
143
|
2 |
|
$path = '/project/' . $project . '/resources'; |
144
|
|
|
|
145
|
|
|
// Send the request. |
146
|
2 |
|
return $this->processResponse($this->client->get($this->fetchUrl($path))); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Method to update the content of a resource within a project. |
151
|
|
|
* |
152
|
|
|
* @param string $project The project the resource is part of |
153
|
|
|
* @param string $resource The resource slug within the project |
154
|
|
|
* @param string $content The content of the resource. This can either be a string of data or a file path. |
155
|
|
|
* @param string $type The type of content in the $content variable. This should be either string or file. |
156
|
|
|
* |
157
|
|
|
* @return \Joomla\Http\Response |
158
|
|
|
*/ |
159
|
4 |
View Code Duplication |
public function updateResourceContent($project, $resource, $content, $type = 'string') |
|
|
|
|
160
|
|
|
{ |
161
|
|
|
// Build the request path. |
162
|
4 |
|
$path = '/project/' . $project . '/resource/' . $resource . '/content/'; |
163
|
|
|
|
164
|
4 |
|
return $this->updateResource($path, $content, $type); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
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.