Completed
Push — master ( d462df...186049 )
by Vladimir
02:16
created

LanguageHelper::__wakeup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

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