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

LanguageUrlTrait   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 1 Features 1
Metric Value
wmc 5
c 1
b 1
f 1
lcom 1
cbo 1
dl 0
loc 35
ccs 10
cts 10
cp 1
rs 10

2 Methods

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