Skip to content

Commit

Permalink
Optimize duplicate codes, path exclusion rules null patterns filtering.
Browse files Browse the repository at this point in the history
  • Loading branch information
diazoxide committed Jan 27, 2020
1 parent 273e752 commit afa61bb
Showing 1 changed file with 41 additions and 36 deletions.
77 changes: 41 additions & 36 deletions src/component/translation/type/URL.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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
Expand Down Expand Up @@ -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);
}

/**
Expand Down

0 comments on commit afa61bb

Please sign in to comment.