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

BaseTranslatedBehavior::getAttributeName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 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
}