Language   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 32
dl 0
loc 90
rs 10
c 0
b 0
f 0
wmc 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getFlagCode() 0 16 2
A rules() 0 6 1
A getByCodes() 0 4 1
A getByCode() 0 7 2
A tableName() 0 3 1
A getFlag() 0 3 1
A label() 0 2 1
A attributeLabels() 0 7 1
1
<?php
2
3
namespace andmemasin\language\models;
4
5
use andmemasin\myabstract\MyActiveRecord;
6
use yii;
7
use kartik\icons\Icon;
8
9
/**
10
 * This is the model class for table "language".
11
 *
12
 * @property integer $language_id
13
 * @property string $language
14
 * @property string $name
15
 * @property string $native_name
16
 *
17
 * @property string $flag flag image tag
18
 * @package andmemasin\language\models
19
 * @author Tonis Ormisson <[email protected]>
20
 */
21
class Language extends MyActiveRecord
22
{
23
    /**
24
     * {@inheritdoc}
25
     */
26
    public static function tableName()
27
    {
28
        return 'language';
29
    }
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function label() {
34
        return $this->name;
35
    }
36
    
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function rules()
41
    {
42
        return [
43
            [['language', 'name', 'native_name'], 'required'],
44
            [['language'], 'string', 'max' => 16],
45
            [['name', 'native_name'], 'string', 'max' => 255]
46
        ];
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function attributeLabels()
53
    {
54
        return [
55
            'language_id' => Yii::t('app', 'ID'),
56
            'language' => Yii::t('app', 'Language code'),
57
            'name' => Yii::t('app', 'Name'),
58
            'native_name' => Yii::t('app', 'Native name'),
59
        ];
60
    }
61
62
    /**
63
     * This maps the language to a country (FLAG).
64
     * @param string $language As in language\Language->language
65
     * @return string The county / flag code that matches the county code. If no match is found then the initial country code is returned
66
     */
67
    public static function getFlagCode($language){
68
        $map = [
69
            'en-US'=>'GB',
70
            'et'=>'EE',
71
            'ru'=>'RU',
72
            'fi'=>'FI',
73
            'se'=>'SV',
74
            'lt'=>'LT',
75
            'lv'=>'LV',
76
            'pl'=>'PL',
77
        ];
78
        
79
        if(isset($map[$language])){
80
            return $map[$language];
81
        }
82
        return strtoupper($language);
83
    }
84
85
    /**
86
     * @param string $languageCode language code
87
     * @return array|null|Language
88
     */
89
    public static function getByCode($languageCode){
90
        if($languageCode === 'en'){
91
            $languageCode = 'en-US';
92
        }
93
        /* @var Language $language */
94
        $language =Language::find()->andWhere(['language'=>$languageCode])->one(); ;
95
        return $language;
96
    }
97
98
    public static function getByCodes($languageCodes){
99
        return Language::find()
100
            ->andWhere(['in','language',$languageCodes])
101
            ->all();
102
    }
103
104
    /**
105
     * @return string
106
     * @deprecated this does not map assets
107
     */
108
    public function getFlag()
109
    {
110
        return Icon::show(strtolower(Language::getFlagCode($this->language)),['framework' =>  Icon::FI]);
111
    }
112
113
114
}
115