ConfigLanguageProvider::init()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 4
nc 3
nop 0
1
<?php
2
/**
3
 * @link https://github.com/motion/yii2-language-provider
4
 * @copyright Copyright (c) 2017-2018 Motion Web Production
5
 * @license BSD 3-Clause License
6
 */
7
8
namespace motion\i18n;
9
10
use yii\base\BaseObject;
11
use yii\base\InvalidConfigException;
12
13
/**
14
 * Config language provider.
15
 *
16
 * @author Vladimir Kuprienko <[email protected]>
17
 * @since 1.0
18
 */
19
class ConfigLanguageProvider extends BaseObject implements LanguageProviderInterface
20
{
21
    /**
22
     * Application languages list.
23
     *
24
     * @var array
25
     */
26
    public $languages = [];
27
    /**
28
     * Default application language.
29
     *
30
     * @var array
31
     */
32
    public $defaultLanguage = [];
33
34
35
    /**
36
     * Check whether provider config is correct.
37
     *
38
     * @throws InvalidConfigException
39
     */
40
    public function init()
41
    {
42
        if (empty($this->languages)) {
43
            throw new InvalidConfigException("'languages' field cannot be empty");
44
        }
45
        if (empty($this->defaultLanguage)) {
46
            throw new InvalidConfigException("defaultLanguage' field cannot be empty");
47
        }
48
    }
49
50
    /**
51
     * @inheritdoc
52
     */
53
    public function getLanguages()
54
    {
55
        return $this->languages;
56
    }
57
58
    /**
59
     * @inheritdoc
60
     */
61
    public function getDefaultLanguage()
62
    {
63
        return $this->defaultLanguage;
64
    }
65
66
    /**
67
     * @inheritdoc
68
     */
69
    public function getLanguageLabel($locale)
70
    {
71
        foreach ($this->languages as $language) {
72
            if ($language['locale'] == $locale) {
73
                return $language['label'];
74
            }
75
        }
76
77
        return null;
78
    }
79
}
80