diff --git a/src/component/translation/type/URL.php b/src/component/translation/type/URL.php index dffbf8a..a7336da 100644 --- a/src/component/translation/type/URL.php +++ b/src/component/translation/type/URL.php @@ -204,24 +204,19 @@ protected function validateAfterTranslate( } /** - * Validate before translate - * Take parts that must be preserved to concat - * after translate paths - * - * Removing script name from url to make avoid - * that translatable part of url is only working path + * Prepare Url to process + * Remove all exclusions from URL + * Use validation rules to validate parts of uri * - * @param string $url Translatable url + * @param string $url URL * * @return bool */ - protected function validateBeforeTranslate(&$url): bool + protected function prepareUrlToProcess(string &$url):bool { $url = trim($url, ' '); - foreach ($this->path_exclusion_patterns as $pattern) { - $url = preg_replace($pattern, '', $url); - } + $url = $this->removeExclusionsFromUrl($url); $parts = parse_url($url); @@ -245,6 +240,23 @@ protected function validateBeforeTranslate(&$url): bool return true; } + /** + * Validate before translate + * Take parts that must be preserved to concat + * after translate paths + * + * Removing script name from url to make avoid + * that translatable part of url is only working path + * + * @param string $url Translatable url + * + * @return bool + */ + protected function validateBeforeTranslate(string &$url): bool + { + return $this->prepareUrlToProcess($url); + } + /** * Building translation result that @@ -399,40 +411,33 @@ private function _preparePathPart($url) } /** - * Validate URL before ReTranslate + * Remove exclusions from url * - * @param string $url Re translatable URL + * @param string $url URL * - * @return bool + * @return string */ - protected function validateBeforeReTranslate(&$url): bool + protected function removeExclusionsFromUrl(string $url): string { - $url = trim($url, ' '); + $path_exclusion_patterns = array_filter($this->path_exclusion_patterns); - foreach ($this->path_exclusion_patterns as $pattern) { + foreach ($path_exclusion_patterns as $pattern) { $url = preg_replace($pattern, '', $url); } - $parts = parse_url($url); - - foreach ($this->url_validation_rules as $key => $rules) { - if (!isset($parts[$key])) { - $parts[$key] = ''; - } - foreach ($rules as $rule) { - if (!preg_match("/$rule/", $parts[$key])) { - return false; - } - } - } - - $url = isset($parts['path']) ? $parts['path'] : ''; - - $url = $this->context->context->languages->removeScriptNameFromUrl($url); - - $url = rtrim($url, '/'); + return $url ?? ''; + } - return true; + /** + * Validate URL before ReTranslate + * + * @param string $url Re translatable URL + * + * @return bool + */ + protected function validateBeforeReTranslate(&$url): bool + { + return $this->prepareUrlToProcess($url); } /**