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:
<?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\Localizations\Route;
trait Instancing {
/**
* Create new localized route instance. First argument could be configuration
* array with all necessary constructor values or all separated arguments -
* first is route pattern value to parse into match and reverse values, then
* controller with action, params default values and constraints.
* Example:
* `new Route([
* "pattern" => [
* "en" => "/products-list/<name>/<color>",
* "de" => "/produkt-liste/<name>/<color>"
* ],
* "controllerAction" => "Products:List",
* "defaults" => [
* "en" => ["name" => "default-name", "color" => "red"],
* "de" => ["name" => "standard-name","color" => "rot"],
* ],
* "constraints" => ["name" => "[^/]+", "color" => "[a-z]+"]
* ]);`
* or:
* `new Route(
* "/products-list/<name>/<color>",
* "Products:List",
* [
* "en" => ["name" => "default-name", "color" => "red"],
* "de" => ["name" => "standard-name","color" => "rot"],
* ],
* ["name" => "[^/]+", "color" => "[a-z]+"]
* );`
* or:
* `new Route([
* "name" => "products_list",
* "match" => [
* "en" => "#^/products\-list/(?<name>[^/]*)/(?<color>[a-z]*)(?=/$|$)#",
* "de" => "#^/produkt\-liste/(?<name>[^/]*)/(?<color>[a-z]*)(?=/$|$)#"
* ],
* "reverse" => [
* "en" => "/products-list/<name>/<color>",
* "de" => "/produkt-liste/<name>/<color>"
* ],
* "controller" => "Products",
* "action" => "List",
* "defaults" => [
* "en" => ["name" => "default-name", "color" => "red"],
* "de" => ["name" => "standard-name","color" => "rot"],
* ],
* ]);`
* @param string|array $patternOrConfig
* Required, configuration array or route pattern value to
* parse into match and reverse patterns for all localizations.
* @param string $controllerAction
* Optional, controller and action name in pascal case like:
* `"Photogallery:List"`.
* @param array $defaults
* Optional, default param values like:
* `["name" => "default-name", "page" => 1]`
* or in localized version like:
* `["en" => ["name" => "default-name", "page" => 1]],...`
* @param array $constraints
* Optional, params regular expression constraints for regular
* expression match if `"match"` property in configuration
* array as first argument defined.
* @param array $filters
* Optional, callable function(s) under keys `"in" | "out"`
* to filter in and out params accepting arguments:
* `array $params, array $defaultParams, \MvcCore\Request $request`.
* @param array $method
* Optional, http method to only match requests by this method.
* If `NULL` (by default), request with any http method could
* be matched by this route. Given value is automatically
* converted to upper case.
* @return void
*/
public function __construct (
$patternOrConfig = NULL,
$controllerAction = NULL,
$defaults = [],
$constraints = [],
$advancedConfiguration = []
) {
/** @var $this \MvcCore\Ext\Routers\Localizations\Route */
$argsCount = count(func_get_args());
if ($argsCount === 0) return;
if (is_array($patternOrConfig) && $argsCount == 1) {
$data = (object) $patternOrConfig;
$this->constructDataPatternsDefaultsConstraintsFilters($data);
$this->constructDataCtrlActionName($data);
$this->constructDataAdvConf($data);
$this->config = $patternOrConfig;
} else {
$this->constructVarsPatternDefaultsConstraintsFilters(
$patternOrConfig, $defaults, $constraints, $advancedConfiguration
);
$this->constructVarCtrlActionNameByData($controllerAction);
$this->constructVarAdvConf($advancedConfiguration);
$this->config = $advancedConfiguration;
}
$this->constructCtrlOrActionByName();
}
/**
* If route is initialized by single array argument with all data,
* initialize following properties if those exist in given object:
* `pattern`, `match` and `reverse`. If properties `defaults`, `constraints`
* and `filters` exist in given object, initialize them by setter methods.
* @param \stdClass $data Object containing properties `pattern`, `match`,
* `reverse`, `filters`, `constraints` and `defaults`.
* @return void
*/
protected function constructDataPatternsDefaultsConstraintsFilters (& $data) {
/** @var $this \MvcCore\Ext\Routers\Localizations\Route */
if (isset($data->pattern)) {
if (is_array($data->pattern)) {
$this->patternLocalized = $data->pattern;
} else {
$this->pattern = $data->pattern;
}
}
if (isset($data->match)) {
if (is_array($data->match)) {
$this->matchLocalized = $data->match;
} else {
$this->match = $data->match;
}
}
if (isset($data->reverse)) {
if (is_array($data->reverse)) {
$this->reverseLocalized = $data->reverse;
} else {
$this->reverse = $data->reverse;
}
}
if (isset($data->defaults))
$this->SetDefaults($data->defaults);
if (isset($data->constraints))
$this->SetConstraints($data->constraints);
if (isset($data->filters) && is_array($data->filters))
$this->SetFilters($data->filters);
}
/**
* If route is initialized by each constructor function arguments,
* initialize `pattern` if it is an array or not `NULL`. Also initialize
* `defaults` and `constraints` by setter methods if those properties are
* not `NULL` and initialize filter-in and filter-out by filter setter
* method from `$advCfg` array, if there are those filter keys found.
* @param string|NULL $pattern Route pattern string.
* @param array|NULL $defaults Route defaults array, keys are param
* names, values are default values.
* @param array|NULL $constraints Route params regular expression
* constraints array, keys are param
* names, values are allowed regular
* expression rules.
* @param array $advCfg An array with possible keys `in` and
* `out` to define route filter in and
* filter out callable.
* @return void
*/
protected function constructVarsPatternDefaultsConstraintsFilters (& $pattern, & $defaults, & $constraints, & $advCfg) {
/** @var $this \MvcCore\Ext\Routers\Localizations\Route */
if (is_array($pattern)) {
$this->patternLocalized = $pattern;
} else if ($pattern !== NULL) {
$this->pattern = $pattern;
}
if ($defaults !== NULL)
$this->SetDefaults($defaults);
if ($constraints !== NULL)
$this->SetConstraints($constraints);
$filterInParam = static::CONFIG_FILTER_IN;
if (isset($advCfg[$filterInParam]))
$this->SetFilter($advCfg[$filterInParam], $filterInParam);
$filterOutParam = static::CONFIG_FILTER_OUT;
if (isset($advCfg[$filterOutParam]))
$this->SetFilter($advCfg[$filterOutParam], $filterOutParam);
}
/**
* Get `TRUE` if given route record contains only allowed localization keys.
* @param mixed $record
* @return bool
*/
protected function recordIsLocalized ($record) {
/** @var $this \MvcCore\Ext\Routers\Localizations\Route */
static $allowedLocalizationKeys = [];
// init local static property `$allowedLocalizationKeys` only once:
if (count($allowedLocalizationKeys) === 0) {
$router = $this->router;
if ($router === NULL) {
static $routerStat = NULL;
if ($routerStat === NULL)
$routerStat = \MvcCore\Application::GetInstance()->GetRouter();
$router = $routerStat;
}
$allowedLocalizations = $router->GetAllowedLocalizations();
if (!$router->GetRouteRecordsByLanguageAndLocale()) {
foreach ($allowedLocalizations as $allowedLocalization) {
$dashPos = strpos($allowedLocalization, $router::LANG_AND_LOCALE_SEPARATOR);
if ($dashPos === FALSE) continue;
$allowedLocalization = substr($allowedLocalization, 0, $dashPos);
$allowedLocalizationKeys[$allowedLocalization] = $allowedLocalization;
}
}
}
$localizationKeys = TRUE;
$recordKeys = array_keys($record);
foreach ($recordKeys as $recordKey) {
if (!isset($allowedLocalizationKeys[$recordKey])) {
$localizationKeys = FALSE;
break;
}
}
return $localizationKeys;
}
}