Completed
Push — master ( 52cf02...a2427e )
by Loban
02:24
created

BaseTranslatedBehavior   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 100%

Importance

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