Completed
Push — master ( b97c15...2a91fa )
by Loban
03:36
created

LanguageUrlTrait::getLanguage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 1 Features 1
Metric Value
c 1
b 1
f 1
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
crap 2
1
<?php
2
/**
3
 * @link https://github.com/LAV45/yii2-translated-behavior
4
 * @copyright Copyright (c) 2015 LAV45!
5
 * @author Alexey Loban <[email protected]>
6
 * @license http://opensource.org/licenses/BSD-3-Clause
7
 */
8
9
namespace lav45\translate\web;
10
11
use Yii;
12
use Locale;
13
14
trait LanguageUrlTrait
15
{
16
    /**
17
     * @var string
18
     */
19
    public $languageParam = '_lang';
20
    /**
21
     * @var string set this language if exist in url params. If not set, it will use the value of
22
     * [[\yii\base\Application::language]].
23
     */
24
    private $_language;
25
26
    /**
27
     * @return string
28
     */
29 1
    protected function getLanguage()
30
    {
31 1
        if ($this->_language === null) {
32 1
            $this->_language = Locale::getPrimaryLanguage(Yii::$app->language);
33 1
        }
34 1
        return $this->_language;
35
    }
36
37
    /**
38
     * @param array|string $params
39
     * @return mixed
40
     */
41 1
    public function checkLanguageParams($params)
42
    {
43 1
        if (is_array($params) && empty($params[$this->languageParam])) {
44 1
            $params[$this->languageParam] = $this->getLanguage();
45 1
        }
46 1
        return $params;
47
    }
48
}