Passed
Pull Request — master (#332)
by Arman
02:44
created

Lang   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 89
rs 10
wmc 7

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getLang() 0 3 1
A getTranslation() 0 3 1
A __construct() 0 5 1
A isEnabled() 0 3 1
A setLang() 0 4 1
A load() 0 3 1
A flush() 0 3 1
1
<?php
2
3
/**
4
 * Quantum PHP Framework
5
 *
6
 * An open source software development framework for PHP
7
 *
8
 * @package Quantum
9
 * @author Arman Ag. <[email protected]>
10
 * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
11
 * @link http://quantum.softberg.org/
12
 * @since 2.9.9
13
 */
14
15
namespace Quantum\Libraries\Lang;
16
17
use Quantum\Config\Exceptions\ConfigException;
18
use Quantum\App\Exceptions\BaseException;
19
use Quantum\Di\Exceptions\DiException;
20
use ReflectionException;
21
22
/**
23
 * Class Lang
24
 * @package Quantum\Libraries\Lang
25
 */
26
class Lang
27
{
28
29
    /**
30
     * @var string|null 
31
     */
32
    private $currentLang = null;
33
34
    /**
35
     * @var Translator 
36
     */
37
    private $translator;
38
39
    /**
40
     * @var bool 
41
     */
42
    private $isEnabled;
43
44
    /**
45
     * @param string $lang
46
     * @param bool $isEnabled
47
     * @param Translator $translator
48
     */
49
    public function __construct(string $lang, bool $isEnabled, Translator $translator)
50
    {
51
        $this->isEnabled = $isEnabled;
52
        $this->translator = $translator;
53
        $this->setLang($lang);
54
    }
55
56
    /**
57
     * Set current language
58
     * @param string $lang
59
     * @return $this
60
     */
61
    public function setLang(string $lang): self
62
    {
63
        $this->currentLang = $lang;
64
        return $this;
65
    }
66
67
    /**
68
     * Get current language
69
     * @return string|null
70
     */
71
    public function getLang(): ?string
72
    {
73
        return $this->currentLang;
74
    }
75
76
    /**
77
     * Is multilang enabled
78
     * @return bool
79
     */
80
    public function isEnabled(): bool
81
    {
82
        return $this->isEnabled;
83
    }
84
85
    /**
86
     * Load translations
87
     * @throws Exceptions\LangException
88
     * @throws BaseException
89
     * @throws ConfigException
90
     * @throws DiException
91
     * @throws ReflectionException
92
     */
93
    public function load(): void
94
    {
95
        $this->translator->loadTranslations();
96
    }
97
98
    /**
99
     * Get translation by key
100
     * @param string $key
101
     * @param $params
102
     * @return string|null
103
     */
104
    public function getTranslation(string $key, $params = null): ?string
105
    {
106
        return $this->translator->get($key, $params);
107
    }
108
109
    /**
110
     * Flush loaded translations
111
     */
112
    public function flush(): void
113
    {
114
        $this->translator->flush();
115
    }
116
}