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

BaseTranslatedBehavior   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 100%

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getTranslateAttributes() 0 4 1
A isAttribute() 0 4 1
A isSourceLanguage() 0 4 1
A init() 0 10 3
A setTranslateAttributes() 0 8 3
A getTranslateAttributeName() 0 4 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;
10
11
use Yii;
12
use yii\base\Behavior;
13
use Locale;
14
15
/**
16
 * Class BaseTranslatedBehavior
17
 * @package lav45\translate
18
 *
19
 * @property array $translateAttributes
20
 */
21
class BaseTranslatedBehavior extends Behavior
22
{
23
    /**
24
     * @var string the current translate language. If not set, it will use the value of
25
     * [[\yii\base\Application::language]].
26
     */
27
    public $language;
28
    /**
29
     * @var string the language that the original messages are in. If not set, it will use the value of
30
     * [[\yii\base\Application::sourceLanguage]].
31
     */
32
    public $sourceLanguage;
33
    /**
34
     * @var array
35
     */
36
    private $_attributes = [];
37
38
    /**
39
     * Initializes this behavior.
40
     */
41 14
    public function init()
42
    {
43 14
        parent::init();
44 14
        if ($this->language === null) {
45 13
            $this->language = Locale::getPrimaryLanguage(Yii::$app->language);
46 13
        }
47 14
        if ($this->sourceLanguage === null) {
48 14
            $this->sourceLanguage = Locale::getPrimaryLanguage(Yii::$app->sourceLanguage);
49 14
        }
50 14
    }
51
52
    /**
53
     * @return array
54
     */
55 1
    public function getTranslateAttributes()
56
    {
57 1
        return $this->_attributes;
58
    }
59
60
    /**
61
     * @param array $attributes the list of translateAttributes to be translated
62
     */
63 14
    public function setTranslateAttributes($attributes)
64
    {
65 14
        $this->_attributes = [];
66 14
        foreach ((array) $attributes as $key => $value) {
67 14
            $key = is_int($key) ? $value : $key;
68 14
            $this->_attributes[$key] = $value;
69 14
        }
70 14
    }
71
72
    /**
73
     * @param string $name
74
     * @return bool
75
     */
76 13
    protected function isAttribute($name)
77
    {
78 13
        return isset($this->_attributes[$name]);
79
    }
80
81
    /**
82
     * @param string $name
83
     * @return string
84
     */
85 9
    public function getTranslateAttributeName($name)
86
    {
87 9
        return $this->isAttribute($name) ? $this->_attributes[$name] : null;
88
    }
89
90
    /**
91
     * @return bool
92
     */
93 2
    public function isSourceLanguage()
94
    {
95 2
        return $this->language === $this->sourceLanguage;
96
    }
97
}