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

LanguageUrlTrait   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 1 Features 2
Metric Value
wmc 6
c 3
b 1
f 2
lcom 1
cbo 2
dl 0
loc 44
rs 10
ccs 13
cts 13
cp 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getLanguage() 0 7 2
A checkLanguageParams() 0 7 3
A setLanguage() 0 4 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
}