Bonjour à tous,
J'aimerais recueillir vos avis critiques sur cette classe.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
 
/* This class checks available languages of browser
 * and handle choice of preferred language
 * 
 * Version 2.0
 * Author Marc Paris
 * Language identifiers are according to ISO 639-1 standard
*/
 
 
namespace Languages;
 
class Languages
{
 
	/*
	 string language selected language
	 */
	private $language;
 
	/*
	 string allowedLanguages
	  (languages which are available on the website)
	 */
	private $allowedLanguages;
 
 
	/*
	 __construct save language in cookie
	 if days is <0 cookie is deleted
	 if days = 0 previous cookie is hold
	 if days >0 cookie is overwrite
	 */
	public function __construct(int $days=0, $lang='')
	{
		if (empty($lang))
			$lang = $this->setLanguage($this->getLanguages()[0][0]);
 
		if (isset($_COOKIE['language']) && $days <0)
		{
			unset($_COOKIE['language']);
		}
		if (isset($_COOKIE['language']) && $days == 0)
		{
			if (strlen($_COOKIE['language']) == 2)
				$this->setLanguage($_COOKIE['language']);
			else
				throw new \Exception(sprintf("Cookie language '%s' is unknown in %", $_COOKIE['language'], __METHOD__));
		}
		if ($days > 0)
		{
			setcookie('language', $lang, $days*24*3600);
			$this->setLanguage($lang);
		}
 
	}
 
	/*
	 getLanguages search all available browser languages
	 return userLanguages
	 */
	public static function getLanguages()
	{
		//check to see if browser languages are setted
		if ( isset( $_SERVER["HTTP_ACCEPT_LANGUAGE"] ) )
		{
			$languages = strtolower( $_SERVER["HTTP_ACCEPT_LANGUAGE"] );
			// need to remove spaces from strings to avoid error
			$languages = str_replace( ' ', '', $languages );
			$languages = explode( ",", $languages );
 
			foreach ( $languages as $language )
			{
				// pull out the language, place languages into array of full and primary
				// string structure:
				$temp_array = [];
				// slice out the part before ; on first step, the part before - on second, place into array
				$temp_array[0] = substr( $language, 0, strcspn( $language, ';' ) );//full language
				$temp_array[1] = substr( $language, 0, 2 );// cut out primary language
				//place this array into main $userLanguages language array
				$userLanguages[] = $temp_array;
			}
			unset($language);
		}
		// else if no language found
		else
		{
			$userLanguages[0] = array( '','','','' ); //return blank array
		}
 
		return $userLanguages;
	}
 
	/*
	 setAllowedLanguages
	 parameter langs: languages to be allowed
	 */
	public function addAllowedLanguages(string|array $lang)
	{
		if (is_string($lang))
			$lang = (array) $lang;
		foreach($lang as $lng)
		{
			if (strlen($lng) != 2)
				throw new \Exception(sprintf("Wrong parameter value in %s. Each item must be 2 characters long.",__METHOD__));
		}
		$this->allowedLanguages[] = $lang;
	}
 
	/*
	 getAllowedLanguages
	 return allowed languages
	 */
	public function getAllowedLanguages()
	{
		return $this->allowedLanguages;
	}
 
	/*
	 setLanguage set selected Language
	 parameter lang: language to be setted
	 */
	public function setLanguage(string $lang)
	{
		if (strlen($lang) != 2)
			throw new \Exception(sprintf("Wrong parameter value in %s. Parameter must be 2 characters long.",__METHOD__));
		$this->language = $lang;
		if(!checkLanguage($lang))
			return false;
		return true;
	}
 
	/*
	 checkLanguage checks if selected Language is allowed
	 parameter lang: language to be checked
	 */
	public function checkLanguage(string $lang)
	{
		if(in_array($lang,$this->allowedLanguages))
			return true;
		return false;
	}
 
	/*
	 getLanguage
	 return selected language
	 */
	public function getLanguage()
	{
		return $this->language;
	}
 
}
 
// make the class directly available on the global namespace
class_alias('Languages\Languages', 'Languages', false);