Passed
Push — master ( e1e679...09e224 )
by Quentin
10:29
created

twillTrans()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
use Illuminate\Support\Collection;
4
5
if (!function_exists('twillTrans')) {
6
    function twillTrans($key, $replace = [])
7
    {
8 47
        $locale = config('twill.locale', config('twill.fallback_locale', 'en'));
9 47
        return trans($key, $replace, $locale);
10
    }
11
}
12
13
if (!function_exists('getLocales')) {
14
    /**
15
     * @return string[]
16
     */
17
    function getLocales()
18
    {
19 55
        return config('translatable.locales') ?? [config('app.locale')];
20
    }
21
}
22
23
if (!function_exists('getLanguagesForVueStore')) {
24
    /**
25
     * @param array $form_fields
26
     * @param bool $translate
27
     * @return array
28
     */
29
    function getLanguagesForVueStore($form_fields = [], $translate = true)
30
    {
31 46
        $manageMultipleLanguages = count(getLocales()) > 1;
32 46
        if ($manageMultipleLanguages && $translate) {
33
            $allLanguages = Collection::make(config('translatable.locales'))->map(function ($locale, $index) use ($form_fields) {
34
                return [
35 46
                    'shortlabel' => strtoupper($locale),
36 46
                    'label' => getLanguageLabelFromLocaleCode($locale),
37 46
                    'value' => $locale,
38
                    'disabled' => false,
39 46
                    'published' => $form_fields['translations']['active'][$locale] ?? ($index === 0),
40
                ];
41 46
            });
42
43
            return [
44 46
                'all' => $allLanguages,
45 46
                'active' => request()->has('lang') ? $allLanguages->where('value', request('lang'))->first() : null,
46
            ];
47
        }
48
49 1
        $locale = config('app.locale');
50
51
        return [
52
            'all' => [
53
                [
54 1
                    'shortlabel' => strtoupper($locale),
55 1
                    'label' => getLanguageLabelFromLocaleCode($locale),
56 1
                    'value' => $locale,
57
                    'disabled' => false,
58
                    'published' => true,
59
                ],
60
            ],
61
        ];
62
    }
63
}
64
65
if (!function_exists('getLanguageLabelFromLocaleCode')) {
66
    /**
67
     * @param string $code
68
     * @return string
69
     */
70
    function getLanguageLabelFromLocaleCode($code, $native = false)
71
    {
72 46
        if (class_exists(Locale::class)) {
73 46
            if ($native) {
74 1
                return Locale::getDisplayLanguage($code, $code);
75
            } else {
76 46
                return Locale::getDisplayLanguage($code, 'en');
77
            }
78
        }
79
80
        $codeToLanguageMappings = [
81
            'ab' => 'Abkhazian',
82
            'aa' => 'Afar',
83
            'af' => 'Afrikaans',
84
            'sq' => 'Albanian',
85
            'am' => 'Amharic',
86
            'ar' => ['Arabic', 'العربية'],
87
            'an' => 'Aragonese',
88
            'hy' => 'Armenian',
89
            'as' => 'Assamese',
90
            'ay' => 'Aymara',
91
            'az' => 'Azerbaijani',
92
            'ba' => 'Bashkir',
93
            'eu' => 'Basque',
94
            'bn' => 'Bengali (Bangla)',
95
            'dz' => 'Bhutani',
96
            'bh' => 'Bihari',
97
            'bi' => 'Bislama',
98
            'br' => 'Breton',
99
            'bg' => ['Bulgarian', 'български език'],
100
            'my' => 'Burmese',
101
            'be' => 'Byelorussian (Belarusian)',
102
            'km' => 'Cambodian',
103
            'ca' => 'Catalan',
104
            'zh' => 'Chinese',
105
            'zh-Hans' => ['Chinese (simplified)', '简体中文'],
106
            'zh-Hant' => ['Chinese (traditional)', '繁體中文'],
107
            'co' => 'Corsican',
108
            'hr' => 'Croatian',
109
            'cs' => ['Czech', 'čeština'],
110
            'da' => ['Danish', 'Dansk'],
111
            'nl' => ['Dutch', 'Nederlands'],
112
            'en' => 'English',
113
            'eo' => 'Esperanto',
114
            'et' => 'Estonian',
115
            'fo' => 'Faeroese',
116
            'fa' => 'Farsi',
117
            'fj' => ['Fiji', 'Suomi'],
118
            'fi' => 'Finnish',
119
            'fr' => ['French', 'Français'],
120
            'fy' => 'Frisian',
121
            'gd' => 'Gaelic (Scottish)',
122
            'gv' => 'Gaelic (Manx)',
123
            'gl' => 'Galician',
124
            'ka' => 'Georgian',
125
            'de' => ['German', 'Deutsch'],
126
            'el' => ['Greek', 'Ελληνικά'],
127
            'kl' => 'Kalaallisut (Greenlandic)',
128
            'gn' => 'Guarani',
129
            'gu' => 'Gujarati',
130
            'ht' => 'Haitian Creole',
131
            'ha' => 'Hausa',
132
            'he' => 'Hebrew',
133
            'iw' => 'Hebrew',
134
            'hi' => 'Hindi',
135
            'hu' => ['Hungarian', 'Magyar'],
136
            'is' => 'Icelandic',
137
            'io' => 'Ido',
138
            'id' => 'Indonesian',
139
            'in' => 'Indonesian',
140
            'ia' => 'Interlingua',
141
            'ie' => 'Interlingue',
142
            'iu' => 'Inuktitut',
143
            'ik' => 'Inupiak',
144
            'ga' => 'Irish',
145
            'it' => ['Italian', 'Italiano'],
146
            'ja' => ['Japanese', '日本語'],
147
            'jv' => 'Javanese',
148
            'kn' => 'Kannada',
149
            'ks' => 'Kashmiri',
150
            'kk' => 'Kazakh',
151
            'rw' => 'Kinyarwanda (Ruanda)',
152
            'ky' => 'Kirghiz',
153
            'rn' => 'Kirundi (Rundi)',
154
            'ko' => ['Korean', '한국어'],
155
            'ku' => 'Kurdish',
156
            'lo' => 'Laothian',
157
            'la' => 'Latin',
158
            'lv' => 'Latvian (Lettish)',
159
            'li' => 'Limburgish ( Limburger)',
160
            'ln' => 'Lingala',
161
            'lt' => 'Lithuanian',
162
            'mk' => 'Macedonian',
163
            'mg' => 'Malagasy',
164
            'ms' => 'Malay',
165
            'ml' => 'Malayalam',
166
            'mt' => 'Maltese',
167
            'mi' => 'Maori',
168
            'mr' => 'Marathi',
169
            'mo' => 'Moldavian',
170
            'mn' => 'Mongolian',
171
            'na' => 'Nauru',
172
            'ne' => 'Nepali',
173
            'no' => 'Norwegian',
174
            'oc' => 'Occitan',
175
            'or' => 'Oriya',
176
            'om' => 'Oromo (Afan, Galla)',
177
            'ps' => 'Pashto (Pushto)',
178
            'pl' => ['Polish', 'Polski'],
179
            'pt' => ['Portuguese', 'Português'],
180
            'pa' => 'Punjabi',
181
            'qu' => 'Quechua',
182
            'rm' => 'Rhaeto-Romance',
183
            'ro' => ['Romanian', 'Română'],
184
            'ru' => ['Russian', 'Русский'],
185
            'sm' => 'Samoan',
186
            'sg' => 'Sango',
187
            'sa' => 'Sanskrit',
188
            'sr' => 'Serbian',
189
            'sh' => 'Serbo-Croatian',
190
            'st' => 'Sesotho',
191
            'tn' => 'Setswana',
192
            'sn' => 'Shona',
193
            'ii' => 'Sichuan Yi',
194
            'sd' => 'Sindhi',
195
            'si' => 'Sinhalese',
196
            'ss' => 'Siswati',
197
            'sk' => 'Slovak',
198
            'sl' => 'Slovenian',
199
            'so' => 'Somali',
200
            'es' => ['Spanish', 'Español'],
201
            'su' => 'Sundanese',
202
            'sw' => 'Swahili (Kiswahili)',
203
            'sv' => ['Swedish', 'Svenska'],
204
            'tl' => 'Tagalog',
205
            'tg' => 'Tajik',
206
            'ta' => 'Tamil',
207
            'tt' => 'Tatar',
208
            'te' => 'Telugu',
209
            'th' => ['Thai', 'ไทย'],
210
            'bo' => 'Tibetan',
211
            'ti' => 'Tigrinya',
212
            'to' => 'Tonga',
213
            'ts' => 'Tsonga',
214
            'tr' => ['Turkish', 'Türkçe'],
215
            'tk' => 'Turkmen',
216
            'tw' => 'Twi',
217
            'ug' => 'Uighur',
218
            'uk' => ['Ukrainian', 'Українська'],
219
            'ur' => 'Urdu',
220
            'uz' => 'Uzbek',
221
            'vi' => 'Vietnamese',
222
            'vo' => 'Volapük',
223
            'wa' => 'Wallon',
224
            'cy' => 'Welsh',
225
            'wo' => 'Wolof',
226
            'xh' => 'Xhosa',
227
            'yi' => 'Yiddish',
228
            'ji' => 'Yiddish',
229
            'yo' => 'Yoruba',
230
            'zu' => 'Zulu',
231
        ];
232
233
        if (isset($codeToLanguageMappings[$code])) {
234
            $lang = $codeToLanguageMappings[$code];
235
            if (is_array($lang) && isset($lang[1]) && $native) {
236
                return $lang[1];
237
            }
238
            return $lang;
239
        }
240
        return $code;
241
    }
242
}
243
244
/**
245
 * Converts camelCase string to have spaces between each.
246
 * @param string $camelCaseString
247
 * @return string (ex.: camel case string)
248
 */
249
if (!function_exists('camelCaseToWords')) {
250
    function camelCaseToWords($camelCaseString)
251
    {
252 40
        $re = '/(?<=[a-z])(?=[A-Z])/x';
253 40
        $a = preg_split($re, $camelCaseString);
254 40
        $words = join(" ", $a);
0 ignored issues
show
Bug introduced by
It seems like $a can also be of type false; however, parameter $pieces of join() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

254
        $words = join(" ", /** @scrutinizer ignore-type */ $a);
Loading history...
255 40
        return ucfirst(strtolower($words));
256
    }
257
}
258