BaseTranslatedBehavior::getTranslateAttributes()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
cc 1
nc 1
nop 0
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
 * @property string $language
20
 * @property string $sourceLanguage
21
 */
22
class BaseTranslatedBehavior extends Behavior
23
{
24
    use LocaleHelperTrait;
25
    /**
26
     * @var string the current translate language. If not set, it will use the value of
27
     * [[\yii\base\Application::language]].
28
     */
29
    private $language;
30
    /**
31
     * @var string the language that the original messages are in. If not set, it will use the value of
32
     * [[\yii\base\Application::sourceLanguage]].
33
     */
34
    private $sourceLanguage;
35
    /**
36
     * @var array
37
     */
38
    private $attributes = [];
39
40
    /**
41
     * @return string
42
     */
43 13
    public function getLanguage()
44
    {
45 13
        if (empty($this->language)) {
46 12
            $this->setLanguage(Yii::$app->language);
47
        }
48 13
        return $this->language;
49
    }
50
51
    /**
52
     * @param string $locale
53
     */
54 13
    public function setLanguage($locale)
55
    {
56 13
        $this->language = $this->getPrimaryLanguage($locale);
57 13
    }
58
59
    /**
60
     * @return string
61
     */
62 13
    public function getSourceLanguage()
63
    {
64 13
        if (empty($this->sourceLanguage)) {
65 13
            $this->setSourceLanguage(Yii::$app->sourceLanguage);
66
        }
67 13
        return $this->sourceLanguage;
68
    }
69
70
    /**
71
     * @param string $locale
72
     */
73 13
    public function setSourceLanguage($locale)
74
    {
75 13
        $this->sourceLanguage = $this->getPrimaryLanguage($locale);
76 13
    }
77
78
    /**
79
     * @return array
80
     */
81 1
    public function getTranslateAttributes()
82
    {
83 1
        return $this->attributes;
84
    }
85
86
    /**
87
     * @param array $attributes the list of translateAttributes to be translated
88
     */
89 17
    public function setTranslateAttributes(array $attributes)
90
    {
91 17
        $this->attributes = [];
92 17
        foreach ((array) $attributes as $key => $value) {
93 17
            $key = is_int($key) ? $value : $key;
94 17
            $this->attributes[$key] = $value;
95
        }
96 17
    }
97
98
    /**
99
     * @param string $name
100
     * @return bool
101
     */
102 15
    protected function isAttribute($name)
103
    {
104 15
        return isset($this->attributes[$name]);
105
    }
106
107
    /**
108
     * @param string $name
109
     * @return string
110
     */
111 11
    public function getTranslateAttributeName($name)
112
    {
113 11
        return $this->isAttribute($name) ? $this->attributes[$name] : null;
114
    }
115
116
    /**
117
     * @return bool
118
     */
119 2
    public function isSourceLanguage()
120
    {
121 2
        return $this->getLanguage() === $this->getSourceLanguage();
122
    }
123
}