Completed
Push — master ( 5d09e0...2c6feb )
by Elf
06:05
created

Helper::aliasFacade()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
ccs 0
cts 4
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace ElfSundae\Laravel\Support;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Foundation\AliasLoader;
7
use Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesser;
8
9
class Helper
10
{
11
    /**
12
     * Create alias for the facade.
13
     *
14
     * @param  string  $facade
15
     * @param  string  $class
16
     * @return void
17
     */
18
    public static function aliasFacade($facade, $class)
19
    {
20
        AliasLoader::getInstance()->alias($facade, $class);
21
    }
22
23
    /**
24
     * Trim strings for the request's inputs.
25
     *
26
     * @see https://gist.github.com/drakakisgeo/3bba2a2600b4c554f836#gistcomment-1744670
27
     *
28
     * @param  \Illuminate\Http\Request  $request
29
     * @return \Illuminate\Http\Request
30
     */
31
    public static function trimRequestInputs(Request $request)
32
    {
33
        $inputs = $request->input();
34
35
        array_walk_recursive($inputs, function (&$value) {
36
            $value = trim($value);
37
        });
38
39
        $request->merge($inputs);
40
41
        return $request;
42
    }
43
44
    /**
45
     * Get file extension for MIME type.
46
     *
47
     * @param  string  $mimeType
48
     * @param  string  $prefix
49
     * @return string|null
50
     */
51
    public static function fileExtensionForMimeType($mimeType, $prefix = '')
52
    {
53
        $extension = ExtensionGuesser::getInstance()->guess($mimeType);
54
55
        if (! is_null($extension)) {
56
            if ('jpeg' == $extension) {
57
                $extension = 'jpg';
58
            }
59
60
            return $prefix.$extension;
61
        }
62
    }
63
64
    /**
65
     * Convert an iOS platform to the device model name.
66
     *
67
     * @see https://www.theiphonewiki.com/wiki/Models
68
     * @see https://support.hockeyapp.net/kb/client-integration-ios-mac-os-x-tvos/ios-device-types
69
     *
70
     * @param  string  $platform
71
     * @return string
72
     */
73
    public static function iDeviceModel($platform)
74
    {
75
        $list = [
76
            'iPhone1,1' => 'iPhone',
77
            'iPhone1,2' => 'iPhone 3G',
78
            'iPhone2,1' => 'iPhone 3GS',
79
            'iPhone3,1' => 'iPhone 4',
80
            'iPhone3,2' => 'iPhone 4',
81
            'iPhone3,3' => 'iPhone 4',
82
            'iPhone4,1' => 'iPhone 4S',
83
            'iPhone5,1' => 'iPhone 5',
84
            'iPhone5,2' => 'iPhone 5',
85
            'iPhone5,3' => 'iPhone 5c',
86
            'iPhone5,4' => 'iPhone 5c',
87
            'iPhone6,1' => 'iPhone 5s',
88
            'iPhone6,2' => 'iPhone 5s',
89
            'iPhone7,1' => 'iPhone 6 Plus',
90
            'iPhone7,2' => 'iPhone 6',
91
            'iPhone8,1' => 'iPhone 6s',
92
            'iPhone8,2' => 'iPhone 6s Plus',
93
            'iPhone8,4' => 'iPhone SE',
94
            'iPhone9,1' => 'iPhone 7',
95
            'iPhone9,2' => 'iPhone 7 Plus',
96
            'iPhone9,3' => 'iPhone 7',
97
            'iPhone9,4' => 'iPhone 7 Plus',
98
99
            'iPod1,1' => 'iPod touch',
100
            'iPod2,1' => 'iPod touch 2G',
101
            'iPod3,1' => 'iPod touch 3G',
102
            'iPod4,1' => 'iPod touch 4G',
103
            'iPod5,1' => 'iPod touch 5G',
104
            'iPod7,1' => 'iPod touch 6G',
105
106
            'iPad1,1' => 'iPad',
107
            'iPad2,1' => 'iPad 2',
108
            'iPad2,2' => 'iPad 2',
109
            'iPad2,3' => 'iPad 2',
110
            'iPad2,4' => 'iPad 2',
111
            'iPad2,5' => 'iPad mini',
112
            'iPad2,6' => 'iPad mini',
113
            'iPad2,7' => 'iPad mini',
114
            'iPad3,1' => 'iPad 3',
115
            'iPad3,2' => 'iPad 3',
116
            'iPad3,3' => 'iPad 3',
117
            'iPad3,4' => 'iPad 4',
118
            'iPad3,5' => 'iPad 4',
119
            'iPad3,6' => 'iPad 4',
120
            'iPad4,1' => 'iPad Air',
121
            'iPad4,2' => 'iPad Air',
122
            'iPad4,3' => 'iPad Air',
123
            'iPad4,4' => 'iPad mini 2',
124
            'iPad4,5' => 'iPad mini 2',
125
            'iPad4,6' => 'iPad mini 2',
126
            'iPad4,7' => 'iPad mini 3',
127
            'iPad4,8' => 'iPad mini 3',
128
            'iPad4,9' => 'iPad mini 3',
129
            'iPad5,1' => 'iPad mini 4',
130
            'iPad5,2' => 'iPad mini 4',
131
            'iPad5,3' => 'iPad Air 2',
132
            'iPad5,4' => 'iPad Air 2',
133
            'iPad6,3' => 'iPad Pro',
134
            'iPad6,4' => 'iPad Pro',
135
            'iPad6,7' => 'iPad Pro',
136
            'iPad6,8' => 'iPad Pro',
137
138
            'AppleTV2,1' => 'Apple TV 2G',
139
            'AppleTV3,1' => 'Apple TV 3G',
140
            'AppleTV3,2' => 'Apple TV 3G',
141
            'AppleTV5,3' => 'Apple TV 4G',
142
143
            'Watch1,1' => 'Apple Watch',
144
            'Watch1,2' => 'Apple Watch',
145
            'Watch2,6' => 'Apple Watch Series 1',
146
            'Watch2,7' => 'Apple Watch Series 1',
147
            'Watch2,3' => 'Apple Watch Series 2',
148
            'Watch2,4' => 'Apple Watch Series 2',
149
150
            'i386' => 'Simulator',
151
            'x86_64' => 'Simulator',
152
        ];
153
154
        return isset($list[$platform]) ? $list[$platform] : $platform;
155
    }
156
157
    /**
158
     * Get the mail homepage.
159
     *
160
     * @param  string  $address
161
     * @return string|null
162
     */
163
    public static function mailHomepage($address)
164
    {
165
        if (preg_match('#@([a-z0-9.-]+)#i', $address, $match)) {
166
            $list = [
167
                'qq.com' => 'https://mail.qq.com',
168
                'vip.qq.com' => 'https://mail.qq.com',
169
                '126.com' => 'http://www.126.com',
170
                'gmail.com' => 'https://mail.google.com',
171
                '139.com' => 'http://mail.10086.cn',
172
                'wo.cn' => 'http://mail.wo.com.cn',
173
                'sina.com' => 'http://mail.sina.com.cn',
174
                'sina.cn' => 'http://mail.sina.com.cn',
175
                'vip.sina.com' => 'http://mail.sina.com.cn',
176
            ];
177
178
            $domain = strtolower($match[1]);
179
180
            return isset($list[$domain]) ? $list[$domain] : 'http://mail.'.$domain;
181
        }
182
    }
183
184
    /**
185
     * Get the Gravatar URL.
186
     *
187
     * @param  string  $email
188
     * @param  int  $size
189
     * @param  string  $default
190
     * @param  string  $rating
191
     * @return string
192
     *
193
     * @see http://cn.gravatar.com/site/implement/images/
194
     */
195
    public static function gravatar($email, $size = 100, $default = null, $rating = null)
196
    {
197
        if (is_null($default)) {
198
            $default = config('services.gravatar.default');
199
        }
200
        if (is_null($rating)) {
201
            $rating = config('services.gravatar.rating');
202
        }
203
204
        $query = http_build_query(compact('size', 'default', 'rating'));
205
206
        return app('url')->assetFrom(
207
            config('services.gravatar.host', 'http://gravatar.com/avatar'),
208
            md5(strtolower(trim($email))).'?'.$query
209
        );
210
    }
211
212
    /**
213
     * Encrypt ASCII string via XOR.
214
     *
215
     * @param  string  $text
216
     * @param  string|null  $key
217
     * @return string
218
     */
219
    public static function sampleEncrypt($text, $key = null)
220
    {
221
        $text = (string) $text;
222
        if (is_null($key)) {
223
            $key = app('encrypter')->getKey();
224
        }
225
226
        // 生成随机字符串
227
        $random = str_random(strlen($text));
228
229
        // 按字符拼接:随机字符串 + 随机字符串异或原文
230
        $tmp = static::sampleEncryption($text, $random, function ($a, $b) {
231
            return $b.($a ^ $b);
232
        });
233
234
        // 异或 $tmp 和 $key
235
        $result = static::sampleEncryption($tmp, $key);
236
237
        return urlsafe_base64_encode($result);
238
    }
239
240
    /**
241
     * Decrypt string via XOR.
242
     *
243
     * @param  string  $text
244
     * @param  string|null  $key
245
     * @return string
246
     */
247
    public static function sampleDecrypt($text, $key = null)
248
    {
249
        if (is_null($key)) {
250
            $key = app('encrypter')->getKey();
251
        }
252
253
        $tmp = static::sampleEncryption(urlsafe_base64_decode($text), $key);
254
        $tmpLength = strlen($tmp);
255
        $result = '';
256
        for ($i = 0; $i < $tmpLength, ($i + 1) < $tmpLength; $i += 2) {
257
            $result .= $tmp[$i] ^ $tmp[$i + 1];
258
        }
259
260
        return $result;
261
    }
262
263
    /**
264
     * Do a sample XOR encryption.
265
     *
266
     * @param  string  $text
267
     * @param  string  $key
268
     * @param  \Closure|null  $callback `($a, $b, $index)`
269
     * @return string
270
     */
271
    protected static function sampleEncryption($text, $key, $callback = null)
272
    {
273
        // 对 $text 和 $key 的每个字符进行运算。
274
        // $callback 为 null 时默认进行异或运算。
275
        // $callback 参数: 第 i 位 $text, 第 i 位 $key, 下标 i
276
        // $callback 返回 false 时,结束字符循环.
277
278
        $text = (string) $text;
279
        $key = (string) $key;
280
        $keyLength = strlen($key);
281
        if (is_null($callback)) {
282
            $callback = function ($a, $b) {
283
                return $a ^ $b;
284
            };
285
        }
286
287
        $result = '';
288
        $textLength = strlen($text);
289
        for ($i = 0; $i < $textLength; $i++) {
290
            $tmp = $callback($text[$i], $key[$i % $keyLength], $i);
291
            if (false === $tmp) {
292
                break;
293
            }
294
            $result .= $tmp;
295
        }
296
297
        return $result;
298
    }
299
300
    /**
301
     * Convert an integer to a string.
302
     * It is useful to shorten a database ID to URL keyword, e.g. 54329 to 'xkE'.
303
     *
304
     * @param  int $number
305
     * @param  string|null  $characters
306
     * @return string
307
     */
308
    public static function int2string($number, string $characters = null)
309
    {
310
        $number = (int) $number;
311
        if ($number < 0) {
312
            return '';
313
        }
314
315
        if (is_null($characters)) {
316
            $characters = config('support.int2string');
317
        }
318
319
        $charactersLength = strlen($characters);
320
        $string = '';
321
322
        while ($number >= $charactersLength) {
323
            $mod = (int) bcmod($number, $charactersLength);
324
            $number = (int) bcdiv($number, $charactersLength);
325
            $string = $characters[$mod].$string;
326
        }
327
328
        return $characters[$number].$string;
329
    }
330
331
    /**
332
     * Convert an `int2string` generated string back to an integer.
333
     *
334
     * @param  string  $string
335
     * @param  string|null  $characters
336
     * @return int
337
     */
338
    public static function string2int($string, string $characters = null)
339
    {
340
        $string = strrev($string);
341
        $stringLength = strlen($string);
342
343
        if (is_null($characters)) {
344
            $characters = config('support.int2string');
345
        }
346
        $charactersLength = strlen($characters);
347
348
        $number = 0;
349
        for ($i = 0; $i < $stringLength; $i++) {
350
            $index = strpos($characters, $string[$i]);
351
            $number = (int) bcadd($number, bcmul($index, bcpow($charactersLength, $i)));
352
        }
353
354
        return $number;
355
    }
356
}
357