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:
<?php
namespace MvcCore\Ext\Routers\Localization;
trait Preparing {
protected function prepareLocalization () {
if (!$this->defaultLocalization) {
throw new \InvalidArgumentException("[".get_class()."] No default localization configured.");
}
$this->originalRequestPath = $this->request->GetPath();
$this->allowedLocalizations = array_combine($this->allowedLocalizations, $this->allowedLocalizations);
$this->defaultLocalizationStr = implode(static::LANG_AND_LOCALE_SEPARATOR, $this->defaultLocalization);
$this->allowedLocalizations[$this->defaultLocalizationStr] = $this->defaultLocalizationStr;
if (count($this->allowedLocalizations) < 2) {
$this->localization = $this->defaultLocalization;
$this->requestLocalization = $this->defaultLocalization;
return;
}
if ($this->detectLocalizationOnlyByLang) {
foreach ($this->allowedLocalizations as $allowedLocalization => $allowedLocalizationValue) {
$separatorPos = strpos($allowedLocalization, static::LANG_AND_LOCALE_SEPARATOR);
if ($separatorPos !== FALSE) {
$lang = substr($allowedLocalization, 0, $separatorPos);
if (!isset($this->localizationEquivalents[$lang]))
$this->localizationEquivalents[$lang] = $allowedLocalization;
}
}
}
$sessStrictModeSwitchUrlParam = static::URL_PARAM_SWITCH_LOCALIZATION;
if (isset($this->requestGlobalGet[$sessStrictModeSwitchUrlParam])) {
$globalGetValue = strtolower($this->requestGlobalGet[$sessStrictModeSwitchUrlParam]);
$separatorPos = strpos($globalGetValue, static::LANG_AND_LOCALE_SEPARATOR);
if ($separatorPos !== FALSE)
$globalGetValue = substr($globalGetValue, 0, $separatorPos + 1)
. strtoupper(substr($globalGetValue, $separatorPos + 1));
if (isset($this->allowedLocalizations[$globalGetValue]))
$this->switchUriParamLocalization = $globalGetValue;
}
if (isset($this->session->{static::URL_PARAM_LOCALIZATION}))
$this->sessionLocalization = $this->session->{static::URL_PARAM_LOCALIZATION};
$this->prepareRequestLocalizationFromUrl();
}
protected function prepareRequestLocalizationFromUrl () {
$this->prepareRequestLocalizationFromUrlQueryString();
if ($this->requestLocalization === NULL && $this->anyRoutesConfigured)
$this->prepareRequestLocalizationFromUrlPath();
if ($this->requestLocalization === NULL) {
$requestPath = $this->request->GetPath(TRUE);
if (trim($requestPath, '/') === '' && $requestPath !== $this->request->GetScriptName()) {
$this->requestLocalization = $this->defaultLocalization;
$this->request->SetLang($this->requestLocalization[0]);
if ($this->requestLocalization[1])
$this->request->SetLocale($this->requestLocalization[1]);
}
}
}
protected function prepareRequestLocalizationFromUrlQueryString () {
$localizationUrlParam = static::URL_PARAM_LOCALIZATION;
$langAndLocaleSeparator = static::LANG_AND_LOCALE_SEPARATOR;
$requestLocalization = $this->request->GetParam(
$localizationUrlParam,
$langAndLocaleSeparator . 'a-zA-Z0-9'
);
$this->prepareSetUpRequestLocalizationIfValid($requestLocalization, FALSE);
}
protected function prepareRequestLocalizationFromUrlPath () {
$requestPath = $this->request->GetPath(TRUE);
$secondSlashPos = mb_strlen($requestPath) > 1
? mb_strpos($requestPath, '/', 1)
: FALSE;
if ($secondSlashPos === FALSE) {
$firstPathElm = $requestPath !== '/' ? mb_substr($requestPath, 1) : '';
} else {
$firstPathElm = mb_substr($requestPath, 1, $secondSlashPos - 1);
}
$localizationPart = preg_replace(
"#[^" . static::LANG_AND_LOCALE_SEPARATOR . "a-z0-9]#",
'', strtolower($firstPathElm)
);
$this->prepareSetUpRequestLocalizationIfValid($localizationPart, TRUE);
}
protected function prepareSetUpRequestLocalizationIfValid ($rawRequestLocalization, $correctRequestPath = FALSE) {
$result = FALSE;
$langAndLocaleSeparator = static::LANG_AND_LOCALE_SEPARATOR;
$requestLocalizationFormated = '';
$rawRequestLocalizationLength = $rawRequestLocalization ? strlen($rawRequestLocalization) : 0;
$requestLocalizationValidStr = $rawRequestLocalizationLength > 0;
if ($requestLocalizationValidStr) {
$requestLocalizationFormated = strtolower($rawRequestLocalization);
$separatorPos = strpos($rawRequestLocalization, static::LANG_AND_LOCALE_SEPARATOR);
if ($separatorPos !== FALSE)
$requestLocalizationFormated = substr($requestLocalizationFormated, 0, $separatorPos + 1)
. strtoupper(substr($requestLocalizationFormated, $separatorPos + 1));
}
if (isset($this->allowedLocalizations[$requestLocalizationFormated])) {
$this->requestLocalization = explode($langAndLocaleSeparator, $requestLocalizationFormated);
$this->requestLocalizationEquivalent = NULL;
$this->request->SetLang($this->requestLocalization[0]);
$result = TRUE;
if (count($this->requestLocalization) > 1)
$this->request->SetLocale($this->requestLocalization[1]);
if ($correctRequestPath) {
$requestPath = $this->request->GetPath(TRUE);
$newPath = mb_substr($requestPath, $rawRequestLocalizationLength + 1);
if ($newPath === '') $newPath = '/';
$this->request->SetPath($newPath);
}
} else if (isset($this->localizationEquivalents[$requestLocalizationFormated])) {
$targetLocalizationStr = $this->localizationEquivalents[$requestLocalizationFormated];
if (isset($this->allowedLocalizations[$targetLocalizationStr])) {
$targetLocalization = explode($langAndLocaleSeparator, $targetLocalizationStr);
if ($this->stricModeBySession && $this->sessionLocalization) {
$this->requestLocalizationEquivalent = $this->sessionLocalization;
$result = TRUE;
} else {
$this->requestLocalizationEquivalent = $targetLocalization;
$result = TRUE;
}
if ($correctRequestPath) {
$requestPath = $this->request->GetPath(TRUE);
$newPath = mb_substr($requestPath, $rawRequestLocalizationLength + 1);
if ($newPath === '') $newPath = '/';
$this->request->SetPath($newPath);
}
}
}
return $result;
}
}