From 9229d19fe62d83c2e0be00ccda2f8d09a15ae43f Mon Sep 17 00:00:00 2001 From: Anton Date: Sat, 1 Aug 2020 02:04:02 +0300 Subject: [PATCH] Update GoogleTokenGenerator Optimize methods, update PhpDoc --- src/Tokens/GoogleTokenGenerator.php | 157 ++++++++++++++-------------- 1 file changed, 80 insertions(+), 77 deletions(-) diff --git a/src/Tokens/GoogleTokenGenerator.php b/src/Tokens/GoogleTokenGenerator.php index c99dc1f..b552e2a 100644 --- a/src/Tokens/GoogleTokenGenerator.php +++ b/src/Tokens/GoogleTokenGenerator.php @@ -3,13 +3,39 @@ namespace Stichoza\GoogleTranslate\Tokens; /** - * Google Token Generator. + * Google token generator. * - * Thanks to @helen5106 and @tehmaestro and few other cool guys - * at https://github.com/Stichoza/google-translate-php/issues/32 + * @link https://github.com/Stichoza/google-translate-php/issues/32 Thanks to @helen5106 and @tehmaestro and few other cool guys + * {@inheritDoc} */ class GoogleTokenGenerator implements TokenProviderInterface { + /** + * @var array Token keys + */ + protected const TKK = ['406398', 2087938574]; + + /** + * @var string Character encoding + */ + protected $encoding; + + /** + * @var string[] Generated tokens + */ + protected $tokens = []; + + /** + * Creates new instance. + * + * @param string $encoding Character encoding + * @return void + */ + public function __construct(string $encoding = 'UTF-8') + { + $this->encoding = $encoding; + } + /** * Generate and return a token. * @@ -18,33 +44,24 @@ class GoogleTokenGenerator implements TokenProviderInterface * @param string $text Text to translate * @return string Token */ - public function generateToken(string $source, string $target, string $text) : string + public function generateToken(string $source, string $target, string $text): string { - return $this->TL($text); - } - - /** - * Generate a valid Google Translate request token. - * - * @param string $a text to translate - * - * @return string - */ - private function TL($a) - { - $tkk = $this->TKK(); - $b = $tkk[0]; - - for ($d = [], $e = 0, $f = 0; $f < $this->JS_length($a); $f++) { - $g = $this->JS_charCodeAt($a, $f); + $hash = md5($text); + if (isset($this->tokens[$hash])) { + return $this->tokens[$hash]; + } + + $b = static::TKK[0]; + for ($d = [], $e = 0, $f = 0; $f < $this->length($text); $f++) { + $g = $this->charCodeAt($text, $f); if (128 > $g) { $d[$e++] = $g; } else { if (2048 > $g) { $d[$e++] = $g >> 6 | 192; } else { - if (55296 == ($g & 64512) && $f + 1 < $this->JS_length($a) && 56320 == ($this->JS_charCodeAt($a, $f + 1) & 64512)) { - $g = 65536 + (($g & 1023) << 10) + ($this->JS_charCodeAt($a, ++$f) & 1023); + if (55296 === ($g & 64512) && $f + 1 < $this->length($text) && 56320 === ($this->charCodeAt($text, $f + 1) & 64512)) { + $g = 65536 + (($g & 1023) << 10) + ($this->charCodeAt($text, ++$f) & 1023); $d[$e++] = $g >> 18 | 240; $d[$e++] = $g >> 12 & 63 | 128; } else { @@ -55,109 +72,95 @@ private function TL($a) $d[$e++] = $g & 63 | 128; } } - $a = $b; + $text = $b; for ($e = 0; $e < count($d); $e++) { - $a += $d[$e]; - $a = $this->RL($a, '+-a^+6'); + $text = $this->rl($text + $d[$e], '+-a^+6'); } - $a = $this->RL($a, '+-3^+b+-f'); - $a ^= $tkk[1] ? $tkk[1] + 0 : 0; - if (0 > $a) { - $a = ($a & 2147483647) + 2147483648; + $text = $this->rl($text, '+-3^+b+-f'); + $text ^= static::TKK[1]; + if (0 > $text) { + $text = ($text & 2147483647) + 2147483648; } - $a = fmod($a, pow(10, 6)); - - return $a.'.'.($a ^ $b); - } - - /** - * @return array - */ - private function TKK() - { - return ['406398', (561666268 + 1526272306)]; + $text = fmod($text, pow(10, 6)); + + $this->tokens[$hash] = $text . '.' . ($text ^ $b); + + return $this->tokens[$hash]; } /** * Process token data by applying multiple operations. - * (Params are safe, no need for multibyte functions) + * (Parameters are safe, no need for multibyte functions) * * @param int $a * @param string $b - * * @return int */ - private function RL($a, $b) + private function rl(int $a, string $b): int { for ($c = 0; $c < strlen($b) - 2; $c += 3) { $d = $b[$c + 2]; - $d = 'a' <= $d ? ord($d[0]) - 87 : intval($d); - $d = '+' == $b[$c + 1] ? $this->unsignedRightShift($a, $d) : $a << $d; - $a = '+' == $b[$c] ? ($a + $d & 4294967295) : $a ^ $d; + $d = 'a' <= $d ? ord($d[0]) - 87 : (int) $d; + $d = '+' === $b[$c + 1] ? $this->unsignedRightShift($a, $d) : $a << $d; + $a = '+' === $b[$c] ? ($a + $d & 4294967295) : $a ^ $d; } - return $a; } /** - * Unsigned right shift implementation - * https://msdn.microsoft.com/en-us/library/342xfs5s(v=vs.94).aspx - * http://stackoverflow.com/a/43359819/2953830 - * - * @param $a - * @param $b - * - * @return number + * JS unsigned right shift(`>>>`) implementation. + * + * @link https://msdn.microsoft.com/en-us/library/342xfs5s(v=vs.94).aspx + * @link http://stackoverflow.com/a/43359819/2953830 + * @param int $a + * @param int $b + * @return int */ - private function unsignedRightShift($a, $b) + private function unsignedRightShift($a, $b): int { if ($b >= 32 || $b < -32) { - $m = (int)($b / 32); - $b = $b - ($m * 32); + $b -= intval($b / 32) * 32; } - if ($b < 0) { - $b = 32 + $b; + $b += 32; } - - if ($b == 0) { + + if ($b === 0) { return (($a >> 1) & 0x7fffffff) * 2 + (($a >> $b) & 1); } if ($a < 0) { - $a = ($a >> 1); + $a = $a >> 1; $a &= 2147483647; $a |= 0x40000000; $a = ($a >> ($b - 1)); } else { - $a = ($a >> $b); + $a = $a >> $b; } return $a; } /** - * Get JS charCodeAt equivalent result with UTF-16 encoding + * Get JS `charCodeAt()` equivalent result. * * @param string $str * @param int $index - * - * @return number + * @return int */ - private function JS_charCodeAt($str, $index) { - $utf16 = mb_convert_encoding($str, 'UTF-16LE', 'UTF-8'); - return ord($utf16[$index*2]) + (ord($utf16[$index*2+1]) << 8); + private function charCodeAt(string $str, int $index): int + { + return mb_ord(mb_substr($str, $index, 1, $this->encoding), $this->encoding); } /** - * Get JS equivalent string length with UTF-16 encoding + * Get JS equivalent string `length`. * * @param string $str - * - * @return number + * @return int */ - private function JS_length($str) { - $utf16 = mb_convert_encoding($str, 'UTF-16LE', 'UTF-8'); - return strlen($utf16)/2; + private function length(string $str): int + { + return mb_strlen($str, $this->encoding); } }