Hoa central
Http.php
Go to the documentation of this file.
1 <?php
2 
37 namespace Hoa\Locale\Localizer;
38 
39 use Hoa\Http as HHttp;
40 
52 class Http implements Localizer
53 {
59  protected $_value = null;
60 
61 
62 
70  public function __construct($headerValue = null)
71  {
72  $value = $headerValue ?: HHttp\Runtime::getHeader('accept-language');
73 
74  // Remove CFWS.
75  $this->_value = preg_replace('#\([^\)]+\)|\s#', '', $value);
76 
77  return;
78  }
79 
87  public function getLocale()
88  {
89  foreach (explode(',', $this->_value) as $language) {
90  $match = preg_match(
91  '#^(?<language>[^;]+)(;q=(?<q>0(?:\.\d{0,3})|1(?:\.0{0,3})))?$#',
92  $language,
93  $matches
94  );
95 
96  if (0 !== $match) {
97  break;
98  }
99  }
100 
101  if (empty($matches)) {
102  return null;
103  }
104 
105  return $matches['language'];
106  }
107 }
static getHeader($header)
Definition: Runtime.php:181
__construct($headerValue=null)
Definition: Http.php:70