/** * Translates url into set of sanitazed params (separated by `/`) as specified below:<br> * <ul> * <li>Controller (<strong>1st argument</strong>) - name of key in routing array refering to appropiate routing path</li> * <li>Method (<strong>2nd argument</strong>) - name of method in controller to run</li> * <li><strong>lang-{code}</strong> - Language, code must be specifed accoridng to Lang class codes list.<br> * Default set to English * </li> * <li><strong>page-{number}</strong> - Page number starting from 1.<br> * Defult set to 1 * </li> * <li><strong>Method arguments</strong> - any other values unfitting translation rules will be trated as following arguments of method</li> * </ul> * * @param string $url * Url string * @param bool $sanitaze * If extracted params should be sanitized * @return self */ public function translateUrl(string $url, bool $sanitize = true) : RouterInterface { $return = []; $this->url = $url; $params = explode("/", $sanitize ? Formatter::removeTrailingSlash(Formatter::removeLeadingSlash($url)) : $url); $params = $sanitize ? Formatter::cleanData($params) : $params; if ($params !== false && count($params) != 0) { foreach ($params as $param) { $matches = []; if (preg_match(self::PARAMS_REGEX_PAGE, $param, $matches)) { // Page $page = intval($matches[1]); $return['page'] = $page > 0 ? $page : 1; } elseif (preg_match(self::PARAMS_REGEX_LANG, $param, $matches) && in_array($matches[1], LangCode::CODES_LIST)) { // Language $return['lang'] = $matches[1]; } elseif (!isset($return['controller'])) { // Module $return['controller'] = (string) $param; } elseif (!isset($return['method'])) { // Method $return['method'] = $param; } elseif ($param !== '') { // Method argument if (!array_key_exists('methodArguments', $return)) { $return['methodArguments'] = []; } $this->addMethodArgument($return['methodArguments'], $param); } } } foreach ($return as $paramName => $paramValue) { $this->{$paramName} = $paramValue; } $controller = $this->getController(); $method = $this->getMethod(); // Process method name if (method_exists($controller, $methodNormalized = strtr($method, '-', '_'))) { $this->method = $methodNormalized; } else { throw new \DomainException("Cannot find method {$methodNormalized} inside " . get_class($controller) . " controller"); } // Set missing method arguments as null $reflection = new \ReflectionMethod($this->getController(), $this->getMethod()); $numRequired = $reflection->getNumberOfRequiredParameters(); $numCurrent = count($this->getMethodArguments()); if ($numRequired > $numCurrent) { // If request doesn't have enough arguments for method set it as invalid $this->getController()->getRequest()->setValid(false); for ($i = $numRequired - $numCurrent; $i--; $i > 0) { array_push($this->methodArguments, null); } } return $this; }