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: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237:
<?php
/**
* MvcCore
*
* This source file is subject to the BSD 3 License
* For the full copyright and license information, please view
* the LICENSE.md file that are distributed with this source code.
*
* @copyright Copyright (c) 2016 Tom Flidr (https://github.com/mvccore)
* @license https://mvccore.github.io/docs/mvccore/5.0.0/LICENCE.md
*/
namespace MvcCore\Ext\Views\Helpers;
use \MvcCore\Ext\Tools;
/**
* Responsibility - abstract class to process date, number or money formatting by `Intl` extension or by locale formatting conventions.
* - Formatting processed by `Intl` extension if installed or (automatically) configured system locale settings.
* - System locale settings automatically configured by request language and request locale.
* - Encoding result string to always return it in response encoding, in UTF-8 by default.
* @method \MvcCore\Ext\Views\Helpers\InternationalizedHelper GetInstance()
*/
abstract class InternationalizedHelper extends \MvcCore\Ext\Views\Helpers\AbstractHelper {
/**
* MvcCore Extension - View Helper - Assets - version:
* Comparison by PHP function version_compare();
* @see http://php.net/manual/en/function.version-compare.php
*/
const VERSION = '5.0.0';
/**
* Boolean about if `Intl` (PHP Internationalization Functions) has installed.
* @see http://php.net/manual/en/book.intl.php
* @var bool|NULL
*/
protected $intlExtensionFormatting = NULL;
/**
* Automatically assigned language from controller request object.
* @var string|NULL
*/
protected $lang = NULL;
/**
* Automatically assigned locale from controller request object.
* @var string|NULL
*/
protected $locale = NULL;
/**
* Automatically assigned lang and locale combination from controller request object.
* @var string|NULL
*/
protected $langAndLocale = NULL;
/**
* Storage with `Intl` datetime and number formatter instances.
* @var \IntlDateFormatter[]|\NumberFormatter[]
*/
protected $intlFormatters = [];
/**
* Default encoding to use if there is no response encoding configured.
* @var string
*/
protected $defaultEncoding = 'UTF-8';
/**
* System `setlocale()` category to set up system locale automatically in `SetView()` method.
* @var \int[]
*/
protected $localeCategories = [LC_ALL];
/**
* `TRUE` if there necessary to process any `iconv()` conversion from `strftime()`
* or from `number_format()` etc... results into view helper result rendered into
* application response. This variable is automatically resolved in every `SetView()` call.
* @var bool|NULL
*/
protected $encodingConversion = NULL;
/**
* System `setlocale()` encoding, automatically configured by application request object.
* If there is no language and locale in request object, there is set UTF-8 by default.
* This variable is automatically resolved in every `SetView()` call.
* @var string|NULL
*/
protected $systemEncoding = NULL;
/**
* Target encoding, automatically assigned from application response.
* If there is no response encoding, there is set UTF-8 by default.
* This variable is automatically resolved in every `SetView()` call.
* @var string|NULL
*/
protected $responseEncoding = NULL;
/**
* Default language and locale used for `Intl` formatting fallback,
* when is not possible to configure system locale value
* and when there is necessary to define some default formatting rules.
* @var string[]
*/
protected $defaultLangAndLocale = ['en', 'US'];
/**
* Create new helper instance, set boolean about `Intl` extension formatting by loaded extension.
* @return void
*/
public function __construct () {
if ($this->intlExtensionFormatting === NULL)
$this->intlExtensionFormatting = extension_loaded('Intl');
}
/**
* Set `TRUE` if you want to use explicitly `Intl` extension formatting
* (PHP Internationalization Functions) or set `FALSE` if you want to use explicitly
* `strftime()`, `number_format()`, `money_format()` etc... old fashion functions formatting.
* @see http://php.net/manual/en/book.intl.php
* @see http://php.net/strftime
* @see http://php.net/number_format
* @see http://php.net/money_format
* @param bool $intlExtensionFormatting `TRUE` by default.
* @return \MvcCore\Ext\Views\Helpers\InternationalizedHelper
*/
public function SetIntlExtensionFormatting ($intlExtensionFormatting = TRUE) {
$this->intlExtensionFormatting = $intlExtensionFormatting;
return $this;
}
/**
* Set language code and locale (territory) code manually.
* Use this function only if there is no language and locale
* codes presented in request object.
* @param string $lang `"en" | "de" ...`
* @param string $locale `"US" | "GB" ...`
* @return \MvcCore\Ext\Views\Helpers\InternationalizedHelper
*/
public function SetLangAndLocale ($lang = NULL, $locale = NULL) {
$this->lang = $lang;
$this->locale = $locale;
$this->encodingConversion = NULL;
if ($this->lang !== NULL) {
$this->langAndLocale = $this->lang;
if ($this->locale !== NULL)
$this->langAndLocale .= '_' . $this->locale;
}
return $this;
}
/**
* Set default encoding if there is no response encoding configured.
* Use this function is used only if `Intl` extension is not installed
* and if there is necessary to use `strftime()` or `number_format()` etc.. formatting as a fallback.
* @param string $encoding
* @return \MvcCore\Ext\Views\Helpers\InternationalizedHelper
*/
public function SetDefaultEncoding ($encoding = 'UTF-8') {
$this->defaultEncoding = strtoupper($encoding);
return $this;
}
/**
* Set default language and locale used for `Intl` formatting fallback,
* when is not possible to configure system locale value
* and when there is necessary to define some default formatting rules.
* @param string[] $defaultLangAndLocale
* @return \MvcCore\Ext\Views\Helpers\InternationalizedHelper
*/
public function SetDefaultLangAndLocale ($defaultLangAndLocale = ['en', 'US']) {
$this->defaultLangAndLocale = $defaultLangAndLocale;
return $this;
}
/**
* Set up view properties and language and locale by request object in every view rendering change.
* @param \MvcCore\View $view
* @return \MvcCore\Ext\Views\Helpers\InternationalizedHelper
*/
public function SetView (\MvcCore\IView $view) {
parent::SetView($view);
return $this->SetLangAndLocale($this->request->GetLang(), $this->request->GetLocale());
}
/**
* Try to define system locale and it's system locale conversion information.
* This function is used only if `Intl` extension is not installed
* and if there is necessary to use `strftime()` or `number_format()` etc.. formatting as a fallback.
* @return void
*/
protected function setUpSystemLocaleAndEncodings () {
if ($this->responseEncoding === NULL) {
$this->responseEncoding = $this->response ? $this->response->GetEncoding() : NULL;
$this->responseEncoding = ($this->responseEncoding === NULL
? $this->defaultEncoding
: strtoupper($this->responseEncoding));
}
$this->systemEncoding = NULL;
if ($this->lang !== NULL && $this->locale !== NULL) {
$systemEncodings = [];
foreach ($this->localeCategories as $localeCategory) {
$newRawSystemLocaleValue = \MvcCore\Ext\Tools\Locale::SetLocale(
$localeCategory,
$this->langAndLocale . '.' . $this->responseEncoding
);
if ($newRawSystemLocaleValue === FALSE) continue;
$systemParsedLocale = \MvcCore\Ext\Tools\Locale::GetLocale($localeCategory);
if ($systemParsedLocale !== NULL && $systemParsedLocale->encoding !== NULL)
$systemEncodings[] = $systemParsedLocale->encoding;
}
if ($systemEncodings && count($systemEncodings) == count($this->localeCategories))
$this->systemEncoding = $systemEncodings[0];
}
$this->encodingConversion = (
$this->systemEncoding !== $this->responseEncoding &&
$this->systemEncoding !== NULL &&
$this->responseEncoding !== NULL
);
}
/**
* Encode given string from system encoding into response encoding if necessary.
* This function is used only if `Intl` extension is not installed
* and if there is necessary to use `strftime()` or `number_format()` etc.. formatting as a fallback.
* @param string $str
* @return string
*/
protected function encode ($str = '') {
return $this->encodingConversion
? iconv($this->systemEncoding, $this->responseEncoding, $str)
: $str;
}
}