Passed
Pull Request — 2.x (#727)
by Bekzat
16:53
created

cyrillicToLatin()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 22
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 29
ccs 3
cts 3
cp 1
crap 1
rs 9.568
1
<?php
2
3
use Illuminate\Support\Facades\Storage;
4
5 1
if (!function_exists('s3Endpoint')) {
6
    /**
7
     * @param string $disk
8
     * @return string
9
     */
10 1
    function s3Endpoint($disk = 'libraries')
11
    {
12
        // if a custom s3 endpoint is configured explicitly, return it
13
        $customEndpoint = config("filesystems.disks.{$disk}.endpoint");
14
15
        if ($customEndpoint) {
16
            return $customEndpoint;
17
        }
18
19
        $scheme = config("filesystems.disks.{$disk}.use_https") ? 'https://' : '';
20
        return $scheme . config("filesystems.disks.{$disk}.bucket") . '.' . Storage::disk($disk)->getAdapter()->getClient()->getEndpoint()->getHost();
0 ignored issues
show
Bug introduced by
The method getAdapter() does not exist on Illuminate\Contracts\Filesystem\Filesystem. It seems like you code against a sub-type of Illuminate\Contracts\Filesystem\Filesystem such as Illuminate\Filesystem\FilesystemAdapter. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

20
        return $scheme . config("filesystems.disks.{$disk}.bucket") . '.' . Storage::disk($disk)->/** @scrutinizer ignore-call */ getAdapter()->getClient()->getEndpoint()->getHost();
Loading history...
21
    }
22
}
23
24 1
if (!function_exists('azureEndpoint')) {
25
    /**
26
     * @param string $disk
27
     * @return string
28
     */
29 1
    function azureEndpoint($disk = 'libraries')
30
    {
31
        $scheme = config("filesystems.disks.{$disk}.use_https") ? 'https://' : '';
32
        return $scheme . config("filesystems.disks.{$disk}.name") . '.blob.' . config("filesystems.disks.{$disk}.endpoint-suffix") . '/' . config("filesystems.disks.{$disk}.container");
33
    }
34
}
35
36 1
if (!function_exists('bytesToHuman')) {
37
    /**
38
     * @param float $bytes
39
     * @return string
40
     */
41 1
    function bytesToHuman($bytes)
42
    {
43 3
        $units = ['B', 'Kb', 'Mb', 'Gb', 'Tb', 'Pb'];
44
45 3
        for ($i = 0; $bytes > 1024; $i++) {
46 3
            $bytes /= 1024;
47
        }
48
49 3
        return round($bytes, 2) . ' ' . $units[$i];
50
    }
51
}
52
53 1
if (!function_exists('cyrillicToLatin')) {
54
    /**
55
     * @param string $str
56
     * @return bool|string
57
     */
58 1
    function cyrillicToLatin($str)
59
    {
60
      $tr = [
61
        // Russian
62 3
        "А" => "a", "Б" => "b", "В" => "v", "Г" => "g", "Д" => "d",
63
        "Е" => "e", "Ё" => "yo", "Ж" => "zh", "З" => "z", "И" => "i",
64
        "Й" => "j", "К" => "k", "Л" => "l", "М" => "m", "Н" => "n",
65
        "О" => "o", "П" => "p", "Р" => "r", "С" => "s", "Т" => "t",
66
        "У" => "u", "Ф" => "f", "Х" => "kh", "Ц" => "ts", "Ч" => "ch",
67
        "Ш" => "sh", "Щ" => "sch", "Ъ" => "", "Ы" => "y", "Ь" => "",
68
        "Э" => "e", "Ю" => "yu", "Я" => "ya", "а" => "a", "б" => "b",
69
        "в" => "v", "г" => "g", "д" => "d", "е" => "e", "ё" => "yo",
70
        "ж" => "zh", "з" => "z", "и" => "i", "й" => "j", "к" => "k",
71
        "л" => "l", "м" => "m", "н" => "n", "о" => "o", "п" => "p",
72
        "р" => "r", "с" => "s", "т" => "t", "у" => "u", "ф" => "f",
73
        "х" => "kh", "ц" => "ts", "ч" => "ch", "ш" => "sh", "щ" => "sch",
74
        "ъ" => "", "ы" => "y", "ь" => "", "э" => "e", "ю" => "yu",
75
        "я" => "ya",
76
        // Ukrainian
77
        'Ґ' => 'G', 'Є' => 'Ye', 'І' => 'I', 'Ї' => 'Yi', 'ґ' => 'g',
78
        'є' => 'ie', 'і' => 'i', 'ї' => 'i', 'і́' => 'i',
79
        // Kazakh
80
        'Ә' => 'A', 'Ғ' => 'G', 'Қ' => 'Q', 'Ң' => 'N', 'Ө' => 'O',
81
        'Ұ' => 'U', 'Ү' => 'U', 'Һ' => 'H', 'І' => 'I', 'ә' => 'a',
82
        'ғ' => 'g', 'қ' => 'q', 'ң' => 'n', 'ө' => 'o', 'ұ' => 'u',
83
        'ү' => 'u', 'һ' => 'h', 'і' => 'i',
84
      ];
85
86 3
      return strtr($str, $tr);
87
    }
88
}
89
90 1
if (!function_exists('replaceAccents')) {
91
    /**
92
     * @param string $str
93
     * @return bool|string
94
     */
95 1
    function replaceAccents($str)
96
    {
97 3
        return iconv('UTF-8', 'ASCII//TRANSLIT', cyrillicToLatin($str));
98
    }
99
}
100
101 1
if (!function_exists('sanitizeFilename')) {
102
    /**
103
     * @param string $filename
104
     * @return string
105
     */
106 1
    function sanitizeFilename($filename)
107
    {
108 3
        $sanitizedFilename = replaceAccents($filename);
109
110
        $invalid = array(
111 3
            ' ' => '-',
112
            '%20' => '-',
113
            '_' => '-',
114
        );
115
116 3
        $sanitizedFilename = str_replace(array_keys($invalid), array_values($invalid), $sanitizedFilename);
117
118 3
        $sanitizedFilename = preg_replace('/[^A-Za-z0-9-\. ]/', '', $sanitizedFilename); // Remove all non-alphanumeric except .
119 3
        $sanitizedFilename = preg_replace('/\.(?=.*\.)/', '', $sanitizedFilename); // Remove all but last .
120 3
        $sanitizedFilename = preg_replace('/-+/', '-', $sanitizedFilename); // Replace any more than one - in a row
121 3
        $sanitizedFilename = str_replace('-.', '.', $sanitizedFilename); // Remove last - if at the end
122 3
        $sanitizedFilename = strtolower($sanitizedFilename); // Lowercase
123
124 3
        return $sanitizedFilename;
125
    }
126
}
127