Completed
Push — master ( 1e6b66...ed86f9 )
by Loban
02:51
created

LanguageUrlTrait::setLanguage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
ccs 3
cts 3
cp 1
cc 1
eloc 2
nc 1
nop 1
crap 1
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 lav45\translate\LocaleHelperTrait;
13
14
/**
15
 * Class LanguageUrlTrait
16
 * @package lav45\translate\web
17
 *
18
 * @property string $language
19
 */
20
trait LanguageUrlTrait
21
{
22
    use LocaleHelperTrait;
23
    /**
24
     * @var string
25
     */
26
    public $languageParam = '_lang';
27
    /**
28
     * @var string set this language if exist in url params. If not set, it will use the value of
29
     * [[\yii\base\Application::language]].
30
     */
31
    private $_language;
32
33
    /**
34
     * @return string
35
     */
36 1
    public function getLanguage()
37
    {
38 1
        if ($this->_language === null) {
39 1
            $this->setLanguage(Yii::$app->language);
40 1
        }
41 1
        return $this->_language;
42
    }
43
44
    /**
45
     * @param string $locale
46
     */
47 1
    public function setLanguage($locale)
48
    {
49 1
        $this->_language = $this->getPrimaryLanguage($locale);
50 1
    }
51
52
    /**
53
     * @param array|string $params
54
     * @return mixed
55
     */
56 1
    public function checkLanguageParams($params)
57
    {
58 1
        if (is_array($params) && empty($params[$this->languageParam])) {
59 1
            $params[$this->languageParam] = $this->getLanguage();
60 1
        }
61 1
        return $params;
62
    }
63
}