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

includes/Language.php (1 issue)

Check for loose comparison of strings.

Best Practice Bug Major

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
	 * @param string $index index of the array
56
	 *
57
	 * @return string|array|null
58
	 */
59
60 4
	public function get(string $key = null, string $index = null)
61
	{
62
		/* handle index */
63
64 4
		if (is_array(self::$_languageArray) && array_key_exists($index, self::$_languageArray))
65
		{
66 1
			$languageArray = self::$_languageArray[$index];
67
		}
68
		else
69
		{
70 3
			$languageArray = self::$_languageArray;
71
		}
72
73
		/* values as needed */
74
75 4
		if (is_array($languageArray) && array_key_exists($key, $languageArray))
76
		{
77 2
			return $languageArray[$key];
78
		}
79 2
		if (!$key)
0 ignored issues
show
Bug Best Practice introduced by
The expression $key of type null|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
80
		{
81 1
			return $languageArray;
82
		}
83 1
		return null;
84
	}
85
86
	/**
87
	 * set the value to language
88
	 *
89
	 * @since 2.4.0
90
	 *
91
	 * @param string $key key of the item
92
	 * @param string|array $value value of the item
93
	 */
94
95 1
	public function set(string $key = null, $value = null) : void
96
	{
97 1
		self::$_languageArray[$key] = $value;
98 1
	}
99
100
	/**
101
	 * load from language path
102
	 *
103
	 * @since 3.0.0
104
	 *
105
	 * @param string|array $path single or multiple language path
106
	 */
107
108 1
	public function load($path = null) : void
109
	{
110 1
		$reader = new Reader();
111 1
		$reader->init();
112
113
		/* process path */
114
115 1
		foreach ((array)$path as $file)
116
		{
117 1
			if (is_file($file))
118
			{
119 1
				$languageArray = $reader->loadJSON($file)->getArray();
120 1
				if (is_array($languageArray))
121
				{
122 1
					self::$_languageArray = array_merge(self::$_languageArray, $languageArray);
123
				}
124
			}
125
		}
126 1
	}
127
}
128