LanguageHelper::getDefault()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
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\helpers;
9
10
use Yii;
11
use yii\helpers\ArrayHelper;
12
13
/**
14
 * Helper for making work with language provider easily.
15
 *
16
 * @author Vladimir Kuprienko <[email protected]>
17
 * @since 2.1
18
 */
19
class LanguageHelper
20
{
21
    /**
22
     * @var \motion\i18n\LanguageProviderInterface
23
     */
24
    protected $provider;
25
26
    /**
27
     * @var LanguageHelper
28
     */
29
    private static $_instance;
30
31
32
    /**
33
     * Gets the instance via lazy initialization.
34
     *
35
     * @return LanguageHelper
36
     *
37
     * @throws \yii\base\InvalidConfigException
38
     * @throws \yii\di\NotInstantiableException
39
     */
40
    public static function getInstance()
41
    {
42
        if (null === self::$_instance) {
43
            self::$_instance = new self();
44
            self::$_instance->provider = Yii::$container->get('motion\i18n\LanguageProviderInterface');
45
        }
46
47
        return self::$_instance;
48
    }
49
50
    /**
51
     * Returns list of languages.
52
     *
53
     * @return array
54
     */
55
    public function getAll()
56
    {
57
        return $this->provider->getLanguages();
58
    }
59
60
    /**
61
     * Returns default application language.
62
     *
63
     * @return array
64
     */
65
    public function getDefault()
66
    {
67
        return $this->provider->getDefaultLanguage();
68
    }
69
70
    /**
71
     * Returns language label.
72
     *
73
     * @param null|string $locale By default uses current application language.
74
     *
75
     * @return string
76
     */
77
    public function getLabel($locale = null)
78
    {
79
        $locale = null === $locale ? Yii::$app->language : $locale;
80
81
        return $this->provider->getLanguageLabel($locale);
82
    }
83
84
    /**
85
     * Returns language locales.
86
     *
87
     * @return array
88
     */
89
    public function getLocales()
90
    {
91
        return ArrayHelper::getColumn($this->provider->getLanguages(), 'locale');
92
    }
93
94
    /**
95
     * Is not allowed to call from outside to prevent from creating multiple instances,
96
     * to use the singleton, you have to obtain the instance from getInstance() instead.
97
     */
98
    private function __construct()
99
    {
100
    }
101
102
    /**
103
     * Prevent the instance from being cloned (which would create a second instance of it).
104
     */
105
    private function __clone()
106
    {
107
    }
108
109
    /**
110
     * Prevent from being unserialized (which would create a second instance of it).
111
     */
112
    private function __wakeup()
113
    {
114
    }
115
}
116