1 | <?php |
||
11 | class GoogleTokenGenerator implements TokenProviderInterface |
||
12 | { |
||
13 | /** |
||
14 | * @var array Token keys |
||
15 | */ |
||
16 | protected const TKK = ['406398', 2087938574]; |
||
17 | |||
18 | /** |
||
19 | * @var string Character encoding |
||
20 | */ |
||
21 | protected $encoding; |
||
22 | |||
23 | /** |
||
24 | * @var string[] Generated tokens |
||
25 | */ |
||
26 | protected $tokens = []; |
||
27 | |||
28 | /** |
||
29 | * Creates new instance. |
||
30 | * |
||
31 | * @param string $encoding Character encoding |
||
32 | * @return void |
||
|
|||
33 | */ |
||
34 | public function __construct(string $encoding = 'UTF-8') |
||
38 | |||
39 | /** |
||
40 | * Generate and return a token. |
||
41 | * |
||
42 | * @param string $source Source language |
||
43 | * @param string $target Target language |
||
44 | * @param string $text Text to translate |
||
45 | * @return string Token |
||
46 | */ |
||
47 | public function generateToken(string $source, string $target, string $text): string |
||
90 | |||
91 | /** |
||
92 | * Process token data by applying multiple operations. |
||
93 | * (Parameters are safe, no need for multibyte functions) |
||
94 | * |
||
95 | * @param int $a |
||
96 | * @param string $b |
||
97 | * @return int |
||
98 | */ |
||
99 | private function rl(int $a, string $b): int |
||
109 | |||
110 | /** |
||
111 | * JS unsigned right shift(`>>>`) implementation. |
||
112 | * |
||
113 | * @link https://msdn.microsoft.com/en-us/library/342xfs5s(v=vs.94).aspx |
||
114 | * @link http://stackoverflow.com/a/43359819/2953830 |
||
115 | * @param int $a |
||
116 | * @param int $b |
||
117 | * @return int |
||
118 | */ |
||
119 | private function unsignedRightShift($a, $b): int |
||
143 | |||
144 | /** |
||
145 | * Get JS `charCodeAt()` equivalent result. |
||
146 | * |
||
147 | * @param string $str |
||
148 | * @param int $index |
||
149 | * @return int |
||
150 | */ |
||
151 | private function charCodeAt(string $str, int $index): int |
||
155 | |||
156 | /** |
||
157 | * Get JS equivalent string `length`. |
||
158 | * |
||
159 | * @param string $str |
||
160 | * @return int |
||
161 | */ |
||
162 | private function length(string $str): int |
||
166 | } |
||
167 |
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.