11<?php
2-
32namespace Browser ;
43
4+ use InvalidArgumentException ;
5+
56/**
67 * Language Detection
78 *
89 * @package browser
910 */
1011class Language
1112{
12- private static $ acceptLanguage ;
13- private static $ languages ;
14-
1513 /**
16- * Detect a user's languages and order them by priority
14+ * @var AcceptLanguage
1715 */
18- private static function checkLanguages ()
19- {
20- $ acceptLanguage = self ::getAcceptLanguage ();
21- self ::$ languages = array ();
22-
23- if (!empty ($ acceptLanguage )) {
24- $ httpLanguages = preg_split ('/q=([\d\.]*)/ ' , $ acceptLanguage , -1 , PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE );
25-
26- $ languages = array ();
27- $ key = 0 ;
28- foreach (array_reverse ($ httpLanguages ) as $ value ) {
29- $ value = trim ($ value , ',; . ' );
30- if (is_numeric ($ value )) {
31- $ key = $ value ;
32- } else {
33- $ languages [$ key ] = explode (', ' , $ value );
34- }
35- }
36- krsort ($ languages );
37-
38- foreach ($ languages as $ value ) {
39- self ::$ languages = array_merge (self ::$ languages , $ value );
40- }
41- }
42- }
16+ private $ acceptLanguage ;
4317
4418 /**
45- * Get the accept language value in use to determine the language.
46- *
47- * @return string
19+ * @var array
4820 */
49- public static function getAcceptLanguage ()
50- {
51- if (!isset (self ::$ acceptLanguage )) {
52- self ::setAcceptLanguage (isset ($ _SERVER ['HTTP_ACCEPT_LANGUAGE ' ]) ? $ _SERVER ['HTTP_ACCEPT_LANGUAGE ' ] : "" );
53- }
54-
55- return self ::$ acceptLanguage ;
56- }
21+ private $ languages ;
5722
5823 /**
59- * Set the accept language value in use to determine the browser.
60- *
61- * @param string $acceptLanguage
24+ * @param null|string|AcceptLanguage $acceptLanguage
25+ * @throws InvalidArgumentException
6226 */
63- public static function setAcceptLanguage ($ acceptLanguage )
27+ public function __construct ($ acceptLanguage = null )
6428 {
65- self ::$ acceptLanguage = $ acceptLanguage ;
29+ if ($ acceptLanguage instanceof AcceptLanguage) {
30+ $ this ->setAcceptLanguage ($ acceptLanguage );
31+ } elseif (null === $ acceptLanguage || is_string ($ acceptLanguage )) {
32+ $ this ->setAcceptLanguage (new AcceptLanguage ($ acceptLanguage ));
33+ } else {
34+ throw new InvalidArgumentException ;
35+ }
6636 }
6737
6838 /**
6939 * Get all user's languages
7040 *
7141 * @return array
7242 */
73- public static function getLanguages ()
43+ public function getLanguages ()
7444 {
75- if (!is_array (self :: $ languages )) {
76- self :: checkLanguages ( );
45+ if (!is_array ($ this -> languages )) {
46+ LanguageDetection:: detect ( $ this , $ this -> getAcceptLanguage () );
7747 }
7848
79- return self :: $ languages ;
49+ return $ this -> languages ;
8050 }
8151
8252 /**
8353 * Set languages.
8454 *
85- * @param string $value
55+ * @param string $languages
56+ * @return $this
8657 */
87- public static function setLanguages ($ value )
58+ public function setLanguages ($ languages )
8859 {
89- self ::$ languages = $ value ;
60+ $ this ->languages = $ languages ;
61+ return $ this ;
9062 }
9163
9264 /**
9365 * Get a user's language
9466 *
9567 * @return string
9668 */
97- public static function getLanguage ()
69+ public function getLanguage ()
9870 {
99- if (!is_array (self :: $ languages )) {
100- self :: checkLanguages ( );
71+ if (!is_array ($ this -> languages )) {
72+ LanguageDetection:: detect ( $ this , $ this -> getAcceptLanguage () );
10173 }
10274
103- return strtolower (substr (reset (self :: $ languages ), 0 , 2 ));
75+ return strtolower (substr (reset ($ this -> languages ), 0 , 2 ));
10476 }
10577
10678 /**
@@ -109,14 +81,14 @@ public static function getLanguage()
10981 * @param string $separator
11082 * @return string
11183 */
112- public static function getLanguageLocale ($ separator = '- ' )
84+ public function getLanguageLocale ($ separator = '- ' )
11385 {
114- if (!is_array (self :: $ languages )) {
115- self :: checkLanguages ( );
86+ if (!is_array ($ this -> languages )) {
87+ LanguageDetection:: detect ( $ this , $ this -> getAcceptLanguage () );
11688 }
11789
118- $ userLanguage = self :: getLanguage ();
119- foreach (self :: $ languages as $ language ) {
90+ $ userLanguage = $ this -> getLanguage ();
91+ foreach ($ this -> languages as $ language ) {
12092 if (strlen ($ language ) === 5 && strpos ($ language , $ userLanguage ) === 0 ) {
12193 $ locale = substr ($ language , -2 );
12294 break ;
@@ -129,4 +101,22 @@ public static function getLanguageLocale($separator = '-')
129101 return $ userLanguage ;
130102 }
131103 }
132- }
104+
105+ /**
106+ * @param AcceptLanguage $acceptLanguage
107+ * @return $this
108+ */
109+ public function setAcceptLanguage (AcceptLanguage $ acceptLanguage )
110+ {
111+ $ this ->acceptLanguage = $ acceptLanguage ;
112+ return $ this ;
113+ }
114+
115+ /**
116+ * @return AcceptLanguage
117+ */
118+ public function getAcceptLanguage ()
119+ {
120+ return $ this ->acceptLanguage ;
121+ }
122+ }
0 commit comments