Completed
Branch master (e25020)
by Richan
03:06
created

BrowserNegotiator::preferredLocale()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
c 0
b 0
f 0
rs 9.4285
cc 3
eloc 6
nc 3
nop 1
1
<?php
2
3
namespace RichanFongdasen\I18n\Negotiators;
4
5
use Illuminate\Http\Request;
6
use RichanFongdasen\I18n\Contracts\LanguageNegotiator;
7
8
class BrowserNegotiator implements LanguageNegotiator
9
{
10
    /**
11
     * Determine the user preferred locale by reading
12
     * the browser languages.
13
     *
14
     * @param \Illuminate\Http\Request $request
15
     *
16
     * @return \RichanFongdasen\I18n\Locale
17
     */
18
    public function preferredLocale(Request $request)
19
    {
20
        $languages = $request->getLanguages();
21
22
        foreach ($languages as $language) {
23
            if ($locale = \I18n::getLocale($language)) {
24
                return $locale;
25
            }
26
        }
27
28
        return \I18n::defaultLocale();
29
    }
30
}
31