|
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 Joomla\Http\Exception\UnexpectedResponseException; |
|
15
|
|
|
use Joomla\Http\Response; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Transifex API object class. |
|
19
|
|
|
*/ |
|
20
|
|
|
abstract class TransifexObject |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* Options for the Transifex object. |
|
24
|
|
|
* |
|
25
|
|
|
* @var array|\ArrayAccess |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $options; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* The HTTP client object to use in sending HTTP requests. |
|
31
|
|
|
* |
|
32
|
|
|
* @var Http |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $client; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param array|\ArrayAccess $options Transifex options array. |
|
38
|
|
|
* @param Http $client The HTTP client object. |
|
39
|
|
|
*/ |
|
40
|
1 |
|
public function __construct($options = [], Http $client = null) |
|
41
|
|
|
{ |
|
42
|
1 |
|
if (!is_array($options) && !($options instanceof \ArrayAccess)) { |
|
43
|
|
|
throw new \InvalidArgumentException( |
|
44
|
|
|
'The options param must be an array or implement the ArrayAccess interface.' |
|
45
|
|
|
); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
1 |
|
$this->options = $options; |
|
49
|
1 |
|
$this->client = isset($client) ? $client : new Http($this->options); |
|
50
|
1 |
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Method to build and return a full request URL for the request. |
|
54
|
|
|
* |
|
55
|
|
|
* This method will add appropriate pagination details if necessary and also prepend the API URL |
|
56
|
|
|
* to have a complete URL for the request. |
|
57
|
|
|
* |
|
58
|
|
|
* @param string $path URL to inflect |
|
59
|
|
|
* |
|
60
|
|
|
* @return string |
|
61
|
|
|
*/ |
|
62
|
1 |
|
protected function fetchUrl($path) |
|
63
|
|
|
{ |
|
64
|
|
|
// Ensure the API URL is set before moving on |
|
65
|
1 |
|
$base = isset($this->options['api.url']) ? $this->options['api.url'] : ''; |
|
66
|
|
|
|
|
67
|
1 |
|
return $base . $path; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Process the response and return it. |
|
72
|
|
|
* |
|
73
|
|
|
* @param Response $response The response. |
|
74
|
|
|
* @param int $expectedCode The expected response code. |
|
75
|
|
|
* |
|
76
|
|
|
* @return Response |
|
77
|
|
|
* |
|
78
|
|
|
* @throws UnexpectedResponseException |
|
79
|
|
|
*/ |
|
80
|
68 |
|
protected function processResponse(Response $response, $expectedCode = 200) |
|
81
|
|
|
{ |
|
82
|
|
|
// Validate the response code. |
|
83
|
68 |
|
if ($response->code != $expectedCode) { |
|
84
|
|
|
// Decode the error response and throw an exception. |
|
85
|
30 |
|
$error = json_decode($response->body); |
|
86
|
|
|
|
|
87
|
|
|
// Check if the error message is set; send a generic one if not |
|
88
|
30 |
|
$message = isset($error->message) ? $error->message : $response->body; |
|
89
|
|
|
|
|
90
|
30 |
|
throw new UnexpectedResponseException($response, $message, $response->code); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
38 |
|
return $response; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Method to update an API endpoint with resource content. |
|
98
|
|
|
* |
|
99
|
|
|
* @param string $path API path |
|
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 Response |
|
104
|
|
|
* |
|
105
|
|
|
* @throws \InvalidArgumentException |
|
106
|
|
|
*/ |
|
107
|
7 |
|
protected function updateResource($path, $content, $type) |
|
108
|
|
|
{ |
|
109
|
|
|
// Verify the content type is allowed |
|
110
|
7 |
|
if (!in_array($type, ['string', 'file'])) { |
|
111
|
2 |
|
throw new \InvalidArgumentException('The content type must be specified as file or string.'); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
$data = [ |
|
115
|
5 |
|
'content' => ($type == 'string') ? $content : file_get_contents($content), |
|
116
|
5 |
|
]; |
|
117
|
|
|
|
|
118
|
|
|
// Send the request. |
|
119
|
5 |
|
return $this->processResponse( |
|
120
|
5 |
|
$this->client->put( |
|
121
|
5 |
|
$this->fetchUrl($path), |
|
122
|
5 |
|
json_encode($data), |
|
123
|
5 |
|
['Content-Type' => 'application/json'] |
|
124
|
5 |
|
) |
|
125
|
5 |
|
); |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|