Completed
Push — master ( da58d4...61a0f7 )
by Henry
06:34
created

Language::get()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 3
nc 2
nop 1
crap 3
1
<?php
2
namespace Redaxscript;
3
4
use function array_key_exists;
5
use function array_merge;
6
use function is_array;
7
use function is_file;
8
9
/**
10
 * children class to provide the language
11
 *
12
 * @since 2.2.0
13
 *
14
 * @package Redaxscript
15
 * @category Language
16
 * @author Henry Ruhs
17
 */
18
19
class Language extends Singleton
20
{
21
	/**
22
	 * array of the language
23
	 *
24
	 * @var array
25
	 */
26
27
	protected static $_languageArray = [];
28
29
	/**
30
	 * init the class
31
	 *
32
	 * @since 2.2.0
33
	 *
34
	 * @param string $language detected language to process
35
	 */
36
37 1
	public function init(string $language = 'en') : void
38
	{
39 1
		$this->load('languages/en.json');
40
41
		/* merge language */
42
43 1
		if ($language !== 'en')
44
		{
45 1
			$this->load('languages/' . $language . '.json');
46
		}
47 1
	}
48
49
	/**
50
	 * get the value from language
51
	 *
52
	 * @since 3.0.0
53
	 *
54
	 * @param string $key key of the item
55
	 *
56
	 * @return string|array|null
57
	 */
58
59
	public function get(string $key = null)
60 4
	{
61
		if (is_array(self::$_languageArray) && array_key_exists($key, self::$_languageArray))
62
		{
63
			return self::$_languageArray[$key];
64 4
		}
65
		return null;
66 1
	}
67
68
	/**
69
	 * get the array from language
70 3
	 *
71
	 * @since 4.0.0
72
	 *
73
	 * @return string|array|null
74
	 */
75 4
76
	public function getArray() : array
77 2
	{
78
		return self::$_languageArray;
79 2
	}
80
81 1
	/**
82
	 * set the value to language
83 1
	 *
84
	 * @since 2.4.0
85
	 *
86
	 * @param string $key key of the item
87
	 * @param string|array $value value of the item
88
	 */
89
90
	public function set(string $key = null, $value = null) : void
91
	{
92
		self::$_languageArray[$key] = $value;
93
	}
94
95 1
	/**
96
	 * load from language path
97 1
	 *
98 1
	 * @since 3.0.0
99
	 *
100
	 * @param string|array $path single or multiple language path
101
	 */
102
103
	public function load($path = null) : void
104
	{
105
		$reader = new Reader();
106
		$reader->init();
107
108 1
		/* process path */
109
110 1
		foreach ((array)$path as $file)
111 1
		{
112
			if (is_file($file))
113
			{
114
				$languageArray = $reader->loadJSON($file)->getArray();
115 1
				if (is_array($languageArray))
116
				{
117 1
					self::$_languageArray = array_merge(self::$_languageArray, $languageArray);
118
				}
119 1
			}
120 1
		}
121
	}
122
}
123