1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace BabDev\Transifex; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Base class for interacting with the Transifex API. |
7
|
|
|
*/ |
8
|
|
|
final class Transifex implements TransifexInterface |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* The API factory. |
12
|
|
|
* |
13
|
|
|
* @var FactoryInterface |
14
|
|
|
*/ |
15
|
|
|
private $apiFactory; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Options for the API client. |
19
|
|
|
* |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
private $options; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param FactoryInterface $apiFactory The API factory |
26
|
|
|
* @param array $options Transifex options array |
27
|
|
|
*/ |
28
|
2 |
|
public function __construct(FactoryInterface $apiFactory, array $options = []) |
29
|
|
|
{ |
30
|
2 |
|
$this->apiFactory = $apiFactory; |
31
|
2 |
|
$this->options = $options; |
32
|
|
|
|
33
|
|
|
// Setup the default API url if not already set. |
34
|
2 |
|
if (!$this->getOption('base_uri')) { |
35
|
2 |
|
$this->setOption('base_uri', 'https://www.transifex.com'); |
36
|
|
|
} |
37
|
2 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Factory method to fetch API objects. |
41
|
|
|
* |
42
|
|
|
* @param string $name Name of the API object to retrieve |
43
|
|
|
* |
44
|
|
|
* @return ApiConnector |
45
|
|
|
* |
46
|
|
|
* @throws Exception\UnknownApiConnectorException |
47
|
|
|
*/ |
48
|
1 |
|
public function get(string $name): ApiConnector |
49
|
|
|
{ |
50
|
1 |
|
return $this->apiFactory->createApiConnector($name, $this->options); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Get an option from the API client. |
55
|
|
|
* |
56
|
|
|
* @param string $key The name of the option to get |
57
|
|
|
* @param mixed $default The default value if the option is not set |
58
|
|
|
* |
59
|
|
|
* @return mixed The option value |
60
|
|
|
*/ |
61
|
2 |
|
public function getOption(string $key, $default = null) |
62
|
|
|
{ |
63
|
2 |
|
return $this->options[$key] ?? $default; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Set an option for the API client. |
68
|
|
|
* |
69
|
|
|
* @param string $key The name of the option to set |
70
|
|
|
* @param mixed $value The option value to set |
71
|
|
|
* |
72
|
|
|
* @return void |
73
|
|
|
*/ |
74
|
2 |
|
public function setOption(string $key, $value): void |
75
|
|
|
{ |
76
|
2 |
|
$this->options[$key] = $value; |
77
|
2 |
|
} |
78
|
|
|
} |
79
|
|
|
|