Hoa central
Public Member Functions | Static Public Member Functions | Public Attributes | Protected Member Functions | Protected Attributes | Static Protected Attributes | List of all members
Hoa\Locale\Locale Class Reference

Public Member Functions

 __construct ($localizer=null)
 
 setLocalizer (Localizer $localizer)
 
 getLocalizer ()
 
 getType ()
 
 getLanguage ()
 
 getScript ()
 
 getRegion ()
 
 getVariants ()
 
 getExtensions ()
 
 getPrivateUse ()
 
 getGrandfathered ()
 

Static Public Member Functions

static setDefault ($locale)
 
static getDefault ()
 
static parse ($locale)
 

Public Attributes

const TYPE_LANGTAG = 0
 
const TYPE_PRIVATEUSE = 1
 
const TYPE_GRANDFATHERED = 2
 

Protected Member Functions

 computeLocale ()
 
 reset ()
 

Protected Attributes

 $_localizer = null
 
 $_type = 0
 
 $_language = null
 
 $_script = null
 
 $_region = null
 
 $_variant = null
 
 $_extension = null
 
 $_privateuse = null
 
 $_grandfathered = null
 

Static Protected Attributes

static $_default = null
 

Detailed Description

Class .

Deduce, extract and format locales from different localizers.

Definition at line 49 of file Locale.php.

Constructor & Destructor Documentation

Hoa\Locale\Locale::__construct (   $localizer = null)

Compute the locale from a localizer.

Parameters
mixed$localizerLocalizer or locale.
Returns
void

Definition at line 150 of file Locale.php.

151  {
152  if (!is_object($localizer)) {
153  $localizer = new Localizer\Coerce($localizer);
154  }
155 
156  $this->setLocalizer($localizer);
157 
158  return;
159  }
setLocalizer(Localizer $localizer)
Definition: Locale.php:191

Here is the call graph for this function:

Member Function Documentation

Hoa\Locale\Locale::computeLocale ( )
protected

Compute locale.

Returns
void
Exceptions

Definition at line 219 of file Locale.php.

220  {
221  $locale = $this->getLocalizer()->getLocale() ?: static::getDefault();
222 
223  if (empty($locale)) {
224  throw new Exception('No locale was found.', 0);
225  }
226 
227  $parsed = static::parse($locale);
228 
229  if (false === $parsed) {
230  throw new Exception('Locale %s is not well-formed.', 1, $locale);
231  }
232 
233  if (isset($parsed['grandfathered'])) {
234  $this->_type = static::TYPE_GRANDFATHERED;
235  $this->_grandfathered = $parsed['grandfathered'];
236  } elseif (isset($parsed['privateuse'])) {
237  $this->_type = static::TYPE_PRIVATEUSE;
238  $this->_privateuse = $parsed['privateuse'];
239  } else {
240  $this->_type = static::TYPE_LANGTAG;
241  list(
242  $this->_language,
243  $this->_script,
244  $this->_region,
245  $this->_variant,
246  $this->_extension,
247  $this->_privateuse
248  ) = array_values($parsed['langtag']);
249  }
250 
251  return;
252  }

Here is the call graph for this function:

Here is the caller graph for this function:

static Hoa\Locale\Locale::getDefault ( )
static

Get default locale.

Returns
string

Definition at line 180 of file Locale.php.

181  {
182  return static::$_default;
183  }
Hoa\Locale\Locale::getExtensions ( )

Get extensions.

Returns
array

Definition at line 397 of file Locale.php.

398  {
399  return $this->_extension;
400  }
Hoa\Locale\Locale::getGrandfathered ( )

Get grand-fathered value.

Returns
string

Definition at line 417 of file Locale.php.

418  {
419  return $this->_grandfathered;
420  }
Hoa\Locale\Locale::getLanguage ( )

Get language.

Returns
string

Definition at line 357 of file Locale.php.

358  {
359  return $this->_language;
360  }
Hoa\Locale\Locale::getLocalizer ( )

Get localizer.

Returns

Definition at line 208 of file Locale.php.

209  {
210  return $this->_localizer;
211  }

Here is the caller graph for this function:

Hoa\Locale\Locale::getPrivateUse ( )

Get private use.

Returns
mixed

Definition at line 407 of file Locale.php.

408  {
409  return $this->_privateuse;
410  }
Hoa\Locale\Locale::getRegion ( )

Get region.

Returns
string

Definition at line 377 of file Locale.php.

378  {
379  return $this->_region;
380  }
Hoa\Locale\Locale::getScript ( )

Get script.

Returns
string

Definition at line 367 of file Locale.php.

368  {
369  return $this->_script;
370  }
Hoa\Locale\Locale::getType ( )

Get type. Please, see static::TYPE_* constants.

Returns
int

Definition at line 347 of file Locale.php.

348  {
349  return $this->_type;
350  }
Hoa\Locale\Locale::getVariants ( )

Get all variants.

Returns
array

Definition at line 387 of file Locale.php.

388  {
389  return $this->_variant;
390  }
static Hoa\Locale\Locale::parse (   $locale)
static

Parse a local. Please, see RFC4646, 2.1 Syntax.

Parameters
string$localeLocale.
Returns
array

Definition at line 261 of file Locale.php.

262  {
263 
264  // RFC4646
265  $match = preg_match(
266  '#^
267  (
268  (?<r_langtag>
269  (?<language>[a-z]{2,3})
270  (?<script>\-[a-z]{4})?
271  (?<region>\-(?:[a-z]{2}|[0-9]{4}))?
272  (?<variant>(?:\-(?:[a-z]{2}|[0-9]{3}))+)?
273  (?<extension>(?:\-(?:[a-wy-z]|\d)\-[a-z0-9]{2,8})+)?
274  (?<privateuse>\-x\-[a-z0-9]{1,8})?
275  )
276  | (?<r_privateuse>x\-[a-z0-9]{1,8})
277  | (?<r_grandfathered>[a-z]{1,3}(\-[a-z0-9]{2,8}){1,2})
278  )
279  $#ix',
280  $locale,
281  $matches
282  );
283 
284  if (0 === $match) {
285  return false;
286  }
287 
288  if (isset($matches['r_grandfathered'])) {
289  return [
290  'grandfathered' => $matches['r_grandfathered']
291  ];
292  }
293 
294  if (isset($matches['r_privateuse'])) {
295  return [
296  'privateuse' => substr($matches['r_privateuse'], 2)
297  ];
298  }
299 
300  $out = [
301  'language' => $matches['language'],
302  'script' => null,
303  'region' => null,
304  'variant' => [],
305  'extension' => [],
306  'privateuse' => null
307  ];
308 
309  if (!empty($matches['script'])) {
310  $out['script'] = substr($matches['script'], 1);
311  }
312 
313  if (!empty($matches['region'])) {
314  $out['region'] = substr($matches['region'], 1);
315  }
316 
317  if (!empty($matches['variant'])) {
318  $out['variant'] = explode('-', substr($matches['variant'], 1));
319  }
320 
321  if (!empty($matches['extension'])) {
322  $handle = preg_split(
323  '/-(?=.-)/',
324  $matches['extension'],
325  -1,
326  PREG_SPLIT_NO_EMPTY
327  );
328 
329  foreach ($handle as $value) {
330  list($extensionName, $extensionValue) = explode('-', $value);
331  $out['extension'][$extensionName] = $extensionValue;
332  }
333  }
334 
335  if (!empty($matches['privateuse'])) {
336  $out['privateuse'] = substr($matches['privateuse'], 3);
337  }
338 
339  return ['langtag' => $out];
340  }
Hoa\Locale\Locale::reset ( )
protected

Reset the object.

Returns
void

Definition at line 427 of file Locale.php.

428  {
429  $class = new \ReflectionClass(get_class($this));
430  $object = new \ReflectionObject($this);
431  $defaultProperties = $class->getDefaultProperties();
432  $properties = $object->getProperties();
433 
434  foreach ($properties as $property) {
435  $name = $property->getName();
436 
437  if ('_default' === $name) {
438  continue;
439  }
440 
441  $property->setAccessible(true);
442  $property->setValue(
443  $this,
444  array_key_exists($name, $defaultProperties)
445  ? $defaultProperties[$name]
446  : null
447  );
448  }
449 
450  return;
451  }

Here is the caller graph for this function:

static Hoa\Locale\Locale::setDefault (   $locale)
static

Set default locale.

Parameters
string$localeLocale.
Returns
string

Definition at line 167 of file Locale.php.

168  {
169  $old = static::$_default;
170  static::$_default = $locale;
171 
172  return $old;
173  }
Hoa\Locale\Locale::setLocalizer ( Localizer  $localizer)

Set localizer.

Parameters
\Hoa\Locale\Localizer$localizerLocalizer.
Returns

Definition at line 191 of file Locale.php.

192  {
193  $this->reset();
194 
195  $old = $this->_localizer;
196  $this->_localizer = $localizer;
197 
198  $this->computeLocale();
199 
200  return $old;
201  }

Here is the call graph for this function:

Here is the caller graph for this function:

Member Data Documentation

Hoa\Locale\Locale::$_default = null
staticprotected

Definition at line 77 of file Locale.php.

Hoa\Locale\Locale::$_extension = null
protected

Definition at line 126 of file Locale.php.

Hoa\Locale\Locale::$_grandfathered = null
protected

Definition at line 140 of file Locale.php.

Hoa\Locale\Locale::$_language = null
protected

Definition at line 98 of file Locale.php.

Hoa\Locale\Locale::$_localizer = null
protected

Definition at line 84 of file Locale.php.

Hoa\Locale\Locale::$_privateuse = null
protected

Definition at line 133 of file Locale.php.

Hoa\Locale\Locale::$_region = null
protected

Definition at line 112 of file Locale.php.

Hoa\Locale\Locale::$_script = null
protected

Definition at line 105 of file Locale.php.

Hoa\Locale\Locale::$_type = 0
protected

Definition at line 91 of file Locale.php.

Hoa\Locale\Locale::$_variant = null
protected

Definition at line 119 of file Locale.php.

const Hoa\Locale\Locale::TYPE_GRANDFATHERED = 2

Type: grandfathered.

int

Definition at line 70 of file Locale.php.

const Hoa\Locale\Locale::TYPE_LANGTAG = 0

Type: langtag (the real one).

int

Definition at line 56 of file Locale.php.

const Hoa\Locale\Locale::TYPE_PRIVATEUSE = 1

Type: private use.

int

Definition at line 63 of file Locale.php.


The documentation for this class was generated from the following file: