|
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
|
|
|
use GuzzleHttp\Client; |
|
15
|
|
|
use GuzzleHttp\ClientInterface; |
|
16
|
|
|
use GuzzleHttp\Psr7\Uri; |
|
17
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
18
|
|
|
use Psr\Http\Message\UriInterface; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Transifex API object class. |
|
22
|
|
|
*/ |
|
23
|
|
|
abstract class TransifexObject |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* Options for the Transifex object. |
|
27
|
|
|
* |
|
28
|
|
|
* @var array |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $options; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* The HTTP client object to use in sending HTTP requests. |
|
34
|
|
|
* |
|
35
|
|
|
* @var ClientInterface |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $client; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param array $options Transifex options array |
|
41
|
|
|
* @param ClientInterface $client The HTTP client object |
|
42
|
|
|
*/ |
|
43
|
1 |
|
public function __construct(array $options = [], ClientInterface $client = null) |
|
44
|
|
|
{ |
|
45
|
1 |
|
$this->options = $options; |
|
46
|
1 |
|
$this->client = $client ?: new Client($this->options); |
|
47
|
1 |
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Create a Uri object for the path |
|
51
|
|
|
* |
|
52
|
|
|
* @param string $path |
|
53
|
|
|
* |
|
54
|
|
|
* @return Uri |
|
55
|
|
|
*/ |
|
56
|
|
|
protected function createUri(string $path) : Uri |
|
57
|
|
|
{ |
|
58
|
|
|
$baseUrl = $this->getOption('base_url', 'https://www.transifex.com'); |
|
59
|
|
|
|
|
60
|
|
|
return new Uri($baseUrl.$path); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Get the authentication credentials for the API request. |
|
65
|
|
|
* |
|
66
|
|
|
* @return array |
|
67
|
|
|
* |
|
68
|
|
|
* @throws \InvalidArgumentException if credentials are not set |
|
69
|
|
|
*/ |
|
70
|
71 |
|
protected function getAuthData() : array |
|
71
|
|
|
{ |
|
72
|
71 |
|
$username = $this->getOption('api.username'); |
|
73
|
71 |
|
$password = $this->getOption('api.password'); |
|
74
|
|
|
|
|
75
|
|
|
// The API requires HTTP Basic Authentication, we can't proceed without credentials |
|
76
|
71 |
|
if ($username === null || $password === null) { |
|
77
|
1 |
|
throw new \InvalidArgumentException('Missing credentials for API authentication.'); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
70 |
|
return [$username, $password]; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Get an option from the options store. |
|
85
|
|
|
* |
|
86
|
|
|
* @param string $key The name of the option to get |
|
87
|
|
|
* @param mixed $default The default value if the option is not set |
|
88
|
|
|
* |
|
89
|
|
|
* @return mixed The option value |
|
90
|
|
|
*/ |
|
91
|
1 |
|
protected function getOption(string $key, $default = null) |
|
92
|
|
|
{ |
|
93
|
1 |
|
return $this->options[$key] ?? $default; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Update an API endpoint with resource content. |
|
98
|
|
|
* |
|
99
|
|
|
* @param UriInterface $uri URI object representing the API path to request |
|
100
|
|
|
* @param string $content The content of the resource, this can either be a string of data or a file path |
|
101
|
|
|
* @param string $type The type of content in the $content variable, this should be either string or file |
|
102
|
|
|
* |
|
103
|
|
|
* @return ResponseInterface |
|
104
|
|
|
* |
|
105
|
|
|
* @throws \InvalidArgumentException |
|
106
|
|
|
*/ |
|
107
|
9 |
|
protected function updateResource(UriInterface $uri, string $content, string $type) : ResponseInterface |
|
108
|
|
|
{ |
|
109
|
|
|
// Verify the content type is allowed |
|
110
|
9 |
|
if (!in_array($type, ['string', 'file'])) { |
|
111
|
2 |
|
throw new \InvalidArgumentException('The content type must be specified as file or string.'); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
7 |
|
if ($type == 'file') { |
|
115
|
3 |
|
if (!file_exists($content)) { |
|
116
|
2 |
|
throw new \InvalidArgumentException( |
|
117
|
2 |
|
sprintf('The specified file, "%s", does not exist.', $content) |
|
118
|
|
|
); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
1 |
|
$content = file_get_contents($content); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
$data = [ |
|
125
|
5 |
|
'content' => $content, |
|
126
|
|
|
]; |
|
127
|
|
|
|
|
128
|
5 |
|
return $this->client->request( |
|
129
|
5 |
|
'PUT', |
|
130
|
5 |
|
$uri, |
|
131
|
|
|
[ |
|
132
|
5 |
|
'body' => json_encode($data), |
|
133
|
5 |
|
'auth' => $this->getAuthData(), |
|
134
|
|
|
'headers' => ['Content-Type' => 'application/json'], |
|
135
|
|
|
] |
|
136
|
|
|
); |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|