1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-12 17:54:44 +02:00

Add $sanitizer->textdomain() method

This commit is contained in:
Ryan Cramer
2021-07-02 12:29:51 -04:00
parent ecd491c5d8
commit 48a80058e4

View File

@@ -293,6 +293,7 @@ class Sanitizer extends Wire {
'templateName' => 's', 'templateName' => 's',
'text' => 's', 'text' => 's',
'textarea' => 's', 'textarea' => 's',
'textdomain' => 's',
'trim' => 's', 'trim' => 's',
'truncate' => 's', 'truncate' => 's',
'unentities' => 's', 'unentities' => 's',
@@ -3583,6 +3584,62 @@ class Sanitizer extends Wire {
return empty($value) ? null : $value; return empty($value) ? null : $value;
} }
/**
* Sanitize as language textdomain
*
* #pw-internal
*
* @param string $value
* @return string
* @since 3.0.181
*
*/
public function textdomain($value) {
$value = $this->line($value, 1024);
$value = trim(strtolower($value));
$slash = false;
$dot = false;
if(!strlen($value)) return $value;
if(strpos($value, '\\') !== false) {
$value = str_replace('\\', '/', $value);
}
if(strpos($value, '/') !== false) {
$slash = true;
$config = $this->wire()->config;
$value = str_replace(ltrim($config->paths->root, '/'), '', $value);
$value = trim($value, '/');
}
if(strpos($value, '.') !== false) {
$dot = true;
while(strpos($value, '..') !== false) {
$value = str_replace('..', '.', $value);
}
}
if($dot) {
$value = str_replace(array('/.', './', '-.', '.-'), array('/', '/', '.', '.'), $value);
}
if($slash || $dot) {
$value = str_replace(array('/', '.'), array('--', '-'), $value);
}
while(strpos($value, '---') !== false) {
$value = str_replace('---', '--', $value);
}
if(!ctype_alnum(str_replace(array('-', '_'), '', $value))) {
$value = preg_replace('/[^-_a-z0-9]/', '_', $value);
}
return trim($value, '-');
}
/** /**
* Validate that given value matches regex pattern. * Validate that given value matches regex pattern.
* *