Completed
Push — master ( d3870a...2c6c1e )
by Loban
06:16
created

BaseTranslatedBehavior   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Test Coverage

Coverage 100%

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A isAttribute() 0 4 1
A isSourceLanguage() 0 4 1
A setTranslateAttributes() 0 8 3
A getTranslateAttributeName() 0 4 2
A init() 0 10 3
A getTranslateAttributes() 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;
10
11
use Yii;
12
use yii\base\Behavior;
13
14
/**
15
 * Class BaseTranslatedBehavior
16
 * @package lav45\translate
17
 *
18
 * @property array $translateAttributes
19
 */
20
class BaseTranslatedBehavior extends Behavior
21
{
22
    use LocaleHelperTrait;
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 = $this->getPrimaryLanguage(Yii::$app->language);
46 13
        }
47 14
        if ($this->sourceLanguage === null) {
48 14
            $this->sourceLanguage = $this->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
}