Completed
Pull Request — master (#103)
by
unknown
02:58
created

GoogleTokenGenerator::TL()   A

Complexity

Conditions 5
Paths 16

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 9.2568
c 0
b 0
f 0
cc 5
nc 16
nop 1
1
<?php
2
3
namespace Stichoza\GoogleTranslate\Tokens;
4
5
/**
6
 * Google Token Generator.
7
 *
8
 * Thanks to @helen5106 and @tehmaestro and few other cool guys
9
 * at https://github.com/Stichoza/google-translate-php/issues/32
10
 */
11
class GoogleTokenGenerator implements TokenProviderInterface
12
{
13
    /**
14
     * Generate and return a token.
15
     *
16
     * @param string $source Source language
17
     * @param string $target Target language
18
     * @param string $text   Text to translate
19
     *
20
     * @return mixed A token
21
     */
22
    public function generateToken($source, $target, $text)
23
    {
24
        return $this->TL($text);
25
    }
26
27
    /**
28
     * Generate a valid Google Translate request token.
29
     *
30
     * @param string $a text to translate
31
     *
32
     * @return string
33
     */
34
    private function TL($a)
35
    {
36
        $tkk = $this->TKK();
37
        $b = $tkk[0];
38
39
        for ($d = [], $e = 0, $f = 0; $f < strlen($a); $f++) {
40
            $d[$e++] = ord(substr($a, $f, 1));
41
        }
42
        $a = $b;
43
        for ($e = 0; $e < count($d); $e++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
44
            $a += $d[$e];
45
            $a = $this->RL($a, '+-a^+6');
46
        }
47
        $a = $this->RL($a, '+-3^+b+-f');
48
        $a ^= $tkk[1] ? $tkk[1] + 0 : 0;
49
        if (0 > $a) {
50
            $a = ($a & 2147483647) + 2147483648;
51
        }
52
        $a = fmod($a, pow(10, 6));
53
54
        return $a.'.'.($a ^ $b);
55
    }
56
57
    /**
58
     * @return array
59
     */
60
    private function TKK()
61
    {
62
        return ['406398', (561666268 + 1526272306)];
63
    }
64
65
    /**
66
     * Process token data by applying multiple operations.
67
     * (Params are safe, no need for multibyte functions)
68
     *
69
     * @param int $a
70
     * @param string $b
71
     *
72
     * @return int
73
     */
74
    private function RL($a, $b)
75
    {
76
        for ($c = 0; $c < strlen($b) - 2; $c += 3) {
77
            $d = $b[$c + 2];
78
            $d = 'a' <= $d ? ord($d[0]) - 87 : intval($d);
79
            $d = '+' == $b[$c + 1] ? $this->unsignedRightShift($a, $d) : $a << $d;
80
            $a = '+' == $b[$c] ? ($a + $d & 4294967295) : $a ^ $d;
81
        }
82
83
        return $a;
84
    }
85
86
    /**
87
     * Unsigned right shift implementation
88
     * https://msdn.microsoft.com/en-us/library/342xfs5s(v=vs.94).aspx
89
     * http://stackoverflow.com/a/43359819/2953830
90
     *
91
     * @param $a
92
     * @param $b
93
     *
94
     * @return number
95
     */
96
    private function unsignedRightShift($a, $b)
97
    {
98
        if ($b >= 32 || $b < -32) {
99
            $m = (int)($b / 32);
100
            $b = $b - ($m * 32);
101
        }
102
103
        if ($b < 0) {
104
            $b = 32 + $b;
105
        }
106
107
        if ($b == 0) {
108
            return (($a >> 1) & 0x7fffffff) * 2 + (($a >> $b) & 1);
109
        }
110
111
        if ($a < 0) {
112
            $a = ($a >> 1);
113
            $a &= 2147483647;
114
            $a |= 0x40000000;
115
            $a = ($a >> ($b - 1));
116
        } else { 
117
            $a = ($a >> $b);
118
        }
119
120
        return $a;
121
    }
122
}
123