|
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\{ |
|
15
|
|
|
Client, ClientInterface |
|
16
|
|
|
}; |
|
17
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Transifex API object class. |
|
21
|
|
|
*/ |
|
22
|
|
|
abstract class TransifexObject |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* Options for the Transifex object. |
|
26
|
|
|
* |
|
27
|
|
|
* @var array |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $options; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* The HTTP client object to use in sending HTTP requests. |
|
33
|
|
|
* |
|
34
|
|
|
* @var ClientInterface |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $client; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param array $options Transifex options array. |
|
40
|
|
|
* @param ClientInterface $client The HTTP client object. |
|
41
|
|
|
*/ |
|
42
|
1 |
|
public function __construct(array $options = [], ClientInterface $client = null) |
|
43
|
|
|
{ |
|
44
|
1 |
|
$this->options = $options; |
|
45
|
1 |
|
$this->client = $client ?: new Client($this->options); |
|
|
|
|
|
|
46
|
1 |
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Get the authentication credentials for the API request. |
|
50
|
|
|
* |
|
51
|
|
|
* @return array |
|
52
|
|
|
* |
|
53
|
|
|
* @throws \InvalidArgumentException if credentials are not set |
|
54
|
|
|
*/ |
|
55
|
69 |
|
protected function getAuthData() : array |
|
56
|
|
|
{ |
|
57
|
69 |
|
$username = $this->getOption('api.username'); |
|
58
|
69 |
|
$password = $this->getOption('api.password'); |
|
59
|
|
|
|
|
60
|
|
|
// The API requires HTTP Basic Authentication, we can't proceed without credentials |
|
61
|
69 |
|
if ($username === null || $password === null) { |
|
62
|
1 |
|
throw new \InvalidArgumentException('Missing credentials for API authentication.'); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
68 |
|
return [$username, $password]; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Get an option from the options store. |
|
70
|
|
|
* |
|
71
|
|
|
* @param string $key The name of the option to get. |
|
72
|
|
|
* @param mixed $default The default value if the option is not set. |
|
73
|
|
|
* |
|
74
|
|
|
* @return mixed The option value. |
|
75
|
|
|
*/ |
|
76
|
1 |
|
protected function getOption(string $key, $default = null) |
|
77
|
|
|
{ |
|
78
|
1 |
|
return isset($this->options[$key]) ? $this->options[$key] : $default; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Update an API endpoint with resource content. |
|
83
|
|
|
* |
|
84
|
|
|
* @param string $path API path |
|
85
|
|
|
* @param string $content The content of the resource. This can either be a string of data or a file path. |
|
86
|
|
|
* @param string $type The type of content in the $content variable. This should be either string or file. |
|
87
|
|
|
* |
|
88
|
|
|
* @return ResponseInterface |
|
89
|
|
|
* |
|
90
|
|
|
* @throws \InvalidArgumentException |
|
91
|
|
|
*/ |
|
92
|
7 |
|
protected function updateResource(string $path, string $content, string $type) : ResponseInterface |
|
93
|
|
|
{ |
|
94
|
|
|
// Verify the content type is allowed |
|
95
|
7 |
|
if (!in_array($type, ['string', 'file'])) { |
|
96
|
2 |
|
throw new \InvalidArgumentException('The content type must be specified as file or string.'); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
$data = [ |
|
100
|
5 |
|
'content' => ($type == 'string') ? $content : file_get_contents($content), |
|
101
|
|
|
]; |
|
102
|
|
|
|
|
103
|
5 |
|
return $this->client->request( |
|
104
|
5 |
|
'PUT', |
|
105
|
5 |
|
"/api/2/$path", |
|
106
|
|
|
[ |
|
107
|
5 |
|
'body' => json_encode($data), |
|
108
|
5 |
|
'auth' => $this->getAuthData(), |
|
109
|
|
|
'headers' => ['Content-Type' => 'application/json'], |
|
110
|
|
|
] |
|
111
|
|
|
); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..