1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Stichoza\GoogleTranslate; |
4
|
|
|
|
5
|
|
|
use BadMethodCallException; |
6
|
|
|
use ErrorException; |
7
|
|
|
use Exception; |
8
|
|
|
use InvalidArgumentException; |
9
|
|
|
use Stichoza\GoogleTranslate\Tokens\TokenProviderInterface; |
10
|
|
|
use UnexpectedValueException; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Free Google Translate API PHP Package. |
14
|
|
|
* |
15
|
|
|
* @author Levan Velijanashvili <[email protected]> |
16
|
|
|
* |
17
|
|
|
* @link http://stichoza.com/ |
18
|
|
|
* |
19
|
|
|
* @license MIT |
20
|
|
|
* |
21
|
|
|
* @method string getLastDetectedSource() Can be called statically too. |
22
|
|
|
* @method string translate(string $text) Can be called statically with signature |
23
|
|
|
* string translate(string $source, string $target, string $text) |
24
|
|
|
*/ |
25
|
|
|
class TranslateClient |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var Translator |
29
|
|
|
*/ |
30
|
|
|
private static $translatorInstance; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Class constructor. |
34
|
|
|
* |
35
|
|
|
* For more information about HTTP client configuration options, visit |
36
|
|
|
* "Creating a client" section of GuzzleHttp docs. |
37
|
|
|
* 5.x - http://guzzle.readthedocs.org/en/5.3/clients.html#creating-a-client |
38
|
|
|
* |
39
|
|
|
* @param string $source Source language (Optional) |
40
|
|
|
* @param string $target Target language (Optional) |
41
|
|
|
* @param array $options Associative array of http client configuration options (Optional) |
42
|
|
|
* |
43
|
|
|
* @throws Exception If token provider does not implement TokenProviderInterface |
44
|
|
|
*/ |
45
|
|
|
public function __construct($source = null, $target = 'en', $options = [], TokenProviderInterface $tokener = null) |
46
|
|
|
{ |
47
|
|
|
self::getInstance($tokener) |
48
|
|
|
->setSource($source) |
49
|
|
|
->setTarget($target) |
50
|
|
|
->setHttpOption($options); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Returns the singleton instance of Translator. |
55
|
|
|
* |
56
|
|
|
* @param TokenProviderInterface|null $tokener |
57
|
|
|
* |
58
|
|
|
* @return Translator |
59
|
|
|
*/ |
60
|
|
|
protected static function getInstance(TokenProviderInterface $tokener = null) |
61
|
|
|
{ |
62
|
|
|
if (!self::$translatorInstance) { |
63
|
|
|
self::$translatorInstance = new Translator(null, 'en', [], $tokener); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return self::$translatorInstance; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Override translate method for static call. |
71
|
|
|
* |
72
|
|
|
* @throws BadMethodCallException If calling nonexistent method |
73
|
|
|
* @throws InvalidArgumentException If parameters are passed incorrectly |
74
|
|
|
* @throws InvalidArgumentException If the provided argument is not of type 'string' |
75
|
|
|
* @throws ErrorException If the HTTP request fails |
76
|
|
|
* @throws UnexpectedValueException If received data cannot be decoded |
77
|
|
|
*/ |
78
|
|
|
public static function __callStatic($name, $args) |
79
|
|
|
{ |
80
|
|
|
switch ($name) { |
81
|
|
|
case 'translate': |
|
|
|
|
82
|
|
|
if (count($args) < 3) { |
83
|
|
|
throw new InvalidArgumentException('Expecting 3 parameters'); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
try { |
87
|
|
|
return self::getInstance() |
88
|
|
|
->setSource($args[0]) |
89
|
|
|
->setTarget($args[1]) |
90
|
|
|
->translate($args[2]); |
91
|
|
|
} catch (Exception $e) { |
92
|
|
|
throw $e; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
case 'getLastDetectedSource': |
96
|
|
|
return self::getInstance()->getLastDetectedSource(); |
97
|
|
|
default: |
98
|
|
|
throw new BadMethodCallException("Method [{$name}] does not exist"); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Override translate method for instance call. |
104
|
|
|
* |
105
|
|
|
* @throws BadMethodCallException If calling nonexistent method |
106
|
|
|
* @throws InvalidArgumentException If parameters are passed incorrectly |
107
|
|
|
* @throws InvalidArgumentException If the provided argument is not of type 'string' |
108
|
|
|
* @throws ErrorException If the HTTP request fails |
109
|
|
|
* @throws UnexpectedValueException If received data cannot be decoded |
110
|
|
|
*/ |
111
|
|
|
public function __call($methodName, $args) |
112
|
|
|
{ |
113
|
|
|
if (!method_exists(self::getInstance(), $methodName)) { |
114
|
|
|
throw new BadMethodCallException("Method [{$methodName}] does not exist"); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$reflectionMethod = new \ReflectionMethod(self::getInstance(), $methodName); |
118
|
|
|
$minimumArgs = $reflectionMethod->getNumberOfRequiredParameters(); |
119
|
|
|
if (count($args) < $minimumArgs) { |
120
|
|
|
throw new InvalidArgumentException("Expecting $minimumArgs parameter".($minimumArgs == 1 ? '' : 's')); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return call_user_func_array([self::getInstance(), $methodName], $args); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Check if given locale is valid. |
128
|
|
|
* |
129
|
|
|
* @param string $lang Language code to verify |
130
|
|
|
* |
131
|
|
|
* @return bool |
132
|
|
|
*/ |
133
|
|
|
public static function isValidLocale($lang) |
134
|
|
|
{ |
135
|
|
|
return self::getInstance()->isValidLocale($lang); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|