Completed
Push — master ( 41899b...614894 )
by Loban
36:50 queued 34:00
created

BaseTranslatedBehavior::isSourceLanguage()   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
rs 10
ccs 2
cts 2
cp 1
cc 1
eloc 2
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
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 = $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
     * @param string $locale `en-EN`, `ru-RU`
54
     * @return string
55
     */
56 14
    private function getPrimaryLanguage($locale)
57
    {
58 14
        return extension_loaded('intl') ?
59 14
            Locale::getPrimaryLanguage($locale) : substr($locale, 0, 2);
60
    }
61
62
    /**
63
     * @return array
64
     */
65 1
    public function getTranslateAttributes()
66
    {
67 1
        return $this->_attributes;
68
    }
69
70
    /**
71
     * @param array $attributes the list of translateAttributes to be translated
72
     */
73 14
    public function setTranslateAttributes($attributes)
74
    {
75 14
        $this->_attributes = [];
76 14
        foreach ((array) $attributes as $key => $value) {
77 14
            $key = is_int($key) ? $value : $key;
78 14
            $this->_attributes[$key] = $value;
79 14
        }
80 14
    }
81
82
    /**
83
     * @param string $name
84
     * @return bool
85
     */
86 13
    protected function isAttribute($name)
87
    {
88 13
        return isset($this->_attributes[$name]);
89
    }
90
91
    /**
92
     * @param string $name
93
     * @return string
94
     */
95 9
    public function getTranslateAttributeName($name)
96
    {
97 9
        return $this->isAttribute($name) ? $this->_attributes[$name] : null;
98
    }
99
100
    /**
101
     * @return bool
102
     */
103 2
    public function isSourceLanguage()
104
    {
105 2
        return $this->language === $this->sourceLanguage;
106
    }
107
}