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
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Base class for interacting with the Transifex API. |
20
|
|
|
*/ |
21
|
|
|
class Transifex |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Options for the Transifex object. |
25
|
|
|
* |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
protected $options; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* The HTTP client object to use in sending HTTP requests. |
32
|
|
|
* |
33
|
|
|
* @var ClientInterface |
34
|
|
|
*/ |
35
|
|
|
protected $client; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param array $options Transifex options array. |
39
|
|
|
* @param ClientInterface $client The HTTP client object. |
40
|
|
|
*/ |
41
|
1 |
|
public function __construct(array $options = [], ClientInterface $client = null) |
42
|
|
|
{ |
43
|
1 |
|
$this->options = $options; |
44
|
|
|
|
45
|
|
|
// Setup the default API url if not already set. |
46
|
1 |
|
if (!$this->getOption('base_uri')) { |
47
|
1 |
|
$this->setOption('base_uri', 'https://www.transifex.com'); |
48
|
|
|
} |
49
|
|
|
|
50
|
1 |
|
$this->client = $client ?: new Client($this->options); |
|
|
|
|
51
|
1 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Factory method to fetch API objects. |
55
|
|
|
* |
56
|
|
|
* @param string $name Name of the API object to retrieve. |
57
|
|
|
* |
58
|
|
|
* @return TransifexObject |
59
|
|
|
* |
60
|
|
|
* @throws \InvalidArgumentException |
61
|
|
|
*/ |
62
|
5 |
|
public function get(string $name) : TransifexObject |
63
|
|
|
{ |
64
|
5 |
|
$namespace = $this->getOption('object.namespace', __NAMESPACE__); |
65
|
5 |
|
$class = $namespace . '\\' . ucfirst(strtolower($name)); |
66
|
|
|
|
67
|
5 |
|
if (class_exists($class)) { |
68
|
2 |
|
return new $class($this->options, $this->client); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
// If a custom namespace was specified, let's try to find an object in the local namespace |
72
|
3 |
|
if ($namespace !== __NAMESPACE__) { |
73
|
2 |
|
$class = __NAMESPACE__ . '\\' . ucfirst(strtolower($name)); |
74
|
|
|
|
75
|
2 |
|
if (class_exists($class)) { |
76
|
1 |
|
return new $class($this->options, $this->client); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
// No class found, sorry! |
81
|
2 |
|
throw new \InvalidArgumentException("Could not find an API object for '$name'."); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Get an option from the Transifex instance. |
86
|
|
|
* |
87
|
|
|
* @param string $key The name of the option to get. |
88
|
|
|
* @param mixed $default The default value if the option is not set. |
89
|
|
|
* |
90
|
|
|
* @return mixed The option value. |
91
|
|
|
*/ |
92
|
1 |
|
public function getOption(string $key, $default = null) |
93
|
|
|
{ |
94
|
1 |
|
return isset($this->options[$key]) ? $this->options[$key] : $default; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Set an option for the Transifex instance. |
99
|
|
|
* |
100
|
|
|
* @param string $key The name of the option to set. |
101
|
|
|
* @param mixed $value The option value to set. |
102
|
|
|
* |
103
|
|
|
* @return $this |
104
|
|
|
*/ |
105
|
1 |
|
public function setOption(string $key, $value) : Transifex |
106
|
|
|
{ |
107
|
1 |
|
$this->options[$key] = $value; |
108
|
|
|
|
109
|
1 |
|
return $this; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
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..