Completed
Push — master ( 0080af...41899b )
by Loban
38:58
created

BaseTranslatedBehavior::getPrimaryLanguage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 2

Importance

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