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:
<?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\Routers\Media;
use MvcCore\Ext\Routers;
trait PropsGettersSetters {
/*************************************************************************************
* Configurable Properties *
************************************************************************************/
/**
* Url prefixes prepended before request URL path to describe media site version in url.
* Keys are media site version values and values in array are URL prefixes, how
* to describe media site version in url.
* Full version with possible empty string prefix is necessary to put as last item.
* If you do not want to use rewrite routes, just put under your allowed keys any values.
* @var array
*/
protected $allowedMediaVersionsAndUrlValues = [
Routers\IMedia::MEDIA_VERSION_MOBILE => 'm',
Routers\IMedia::MEDIA_VERSION_TABLET => 't',
Routers\IMedia::MEDIA_VERSION_FULL => '',
];
/***************************************************************************
* Internal Properties *
**************************************************************************/
/**
* Media site version to switch user into.
* Default value is `NULL`. If there is any media site version
* under key `media_version` in `$_GET` array, this property
* has string with this value.
* @var string|NULL
*/
protected $switchUriParamMediaSiteVersion = NULL;
/**
* Resolved media site version, used in `\MvcCore\Request`
* object and possible to use in controller or view.
* Possible values are always: `"full" | "tablet" | "mobile" | NULL`.
* @var string|NULL
*/
protected $mediaSiteVersion = NULL;
/**
* Media site version founded in session.
* @var string|NULL
*/
protected $sessionMediaSiteVersion = NULL;
/**
* Requested media site version.
* @var string|NULL
*/
protected $requestMediaSiteVersion = NULL;
/**
* If `NULL`, request was not first, there was something in session stored by
* previous requests.
* If `TRUE`, request was first, nothing was in session from previous requests
* and detected version is the same as requested media site version.
*
* If `FALSE`, request was first, nothing was in session from previous requests
* and detected version is different from requested media site version.
* There is necessary to redirect user to detected version from first request.
* @var bool|NULL
*/
protected $firstRequestMediaDetection = NULL;
/***************************************************************************
* Public Methods *
**************************************************************************/
/**
* Get resolved media site version, used in `\MvcCore\Request`
* object and possible to use in controller or view.
* Possible values are always: `"full" | "tablet" | "mobile" | NULL`.
* @return string|NULL
*/
public function GetMediaSiteVersion () {
/** @var $this \MvcCore\Ext\Routers\Media */
return $this->mediaSiteVersion;
}
/**
* Set media site version, used in `\MvcCore\Request`
* object and possible to use in controller or view.
* Possible values are always: `"full" | "tablet" | "mobile" | NULL`.
* @param string|NULL $mediaSiteVersion
* @return \MvcCore\Ext\Routers\Media
*/
public function SetMediaSiteVersion ($mediaSiteVersion) {
/** @var $this \MvcCore\Ext\Routers\Media */
$this->mediaSiteVersion = $mediaSiteVersion;
return $this;
}
/**
* Get URL prefixes prepended before request URL path to describe media site
* version in url. Keys are media site version values and values in array are
* URL prefixes, how to describe media site version in URL. Full version with
* possible empty string prefix is necessary to have as last item. If you do
* not want to use rewrite routes, just have under your allowed keys any values.
* Example:
* ```
* [
* 'mobile' => 'm', // to have `/m` substring in every mobile URL begin.
* 'full' => '', // to have nothing extra in URL for full site version.
* ];
* ```
* @return array
*/
public function & GetAllowedMediaVersionsAndUrlValues () {
/** @var $this \MvcCore\Ext\Routers\Media */
return $this->allowedMediaVersionsAndUrlValues;
}
/**
* Set URL prefixes prepended before request URL path to describe media site
* version in URL. Keys are media site version values and values in array are
* URL prefixes, how to describe media site version in URL. Full version with
* possible empty string prefix is necessary to put as last item. If you do
* not want to use rewrite routes, just put under your allowed keys any values.
* Example:
* ```
* \MvcCore\Ext\Routers\Media::GetInstance()->SetAllowedMediaVersionsAndUrlValues([
* 'mobile' => 'm', // to have `/m` substring in every mobile URL begin.
* 'full' => '', // to have nothing extra in URL for full site version.
* ]);
* ```
* @param array $allowedMediaVersionsAndUrlValues
* @return \MvcCore\Ext\Routers\Media
*/
public function SetAllowedMediaVersionsAndUrlValues ($allowedMediaVersionsAndUrlValues = []) {
/** @var $this \MvcCore\Ext\Routers\Media */
$this->allowedMediaVersionsAndUrlValues = $allowedMediaVersionsAndUrlValues;
return $this;
}
/***************************************************************************
* Protected Methods *
**************************************************************************/
/**
* Return media site version string value for redirection URL but if media
* site version is defined by `GET` query string param, return `NULL` and set
* target media site version string into `GET` params to complete query
* string params into redirect URL later. But if the target media site version
* string is the same as full media site version (default value), unset this
* param from `GET` params array and return `NULL` in query string media
* site version definition case.
* @param string $targetMediaSiteVersion Media site version string.
* @return string|NULL
*/
protected function redirectMediaGetUrlValueAndUnsetGet ($targetMediaSiteVersion) {
/** @var $this \MvcCore\Ext\Routers\Media */
$mediaVersionUrlParam = static::URL_PARAM_MEDIA_VERSION;
if (isset($this->requestGlobalGet[$mediaVersionUrlParam])) {
if ($targetMediaSiteVersion === static::MEDIA_VERSION_FULL) {
unset($this->requestGlobalGet[$mediaVersionUrlParam]);
} else {
$this->requestGlobalGet[$mediaVersionUrlParam] = $targetMediaSiteVersion;
}
$targetMediaUrlValue = NULL;
} else {
$targetMediaUrlValue = $this->allowedMediaVersionsAndUrlValues[$targetMediaSiteVersion];
}
return $targetMediaUrlValue;
}
}