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

Update $sanitizer->selectorValue() method to also accept array for its value argument. When array ['a','b','c'] is present, it converts to a sanitized a|b|c string.

This commit is contained in:
Ryan Cramer
2019-02-21 09:58:31 -05:00
parent 65e283055a
commit 774c2152ee

View File

@@ -1772,11 +1772,16 @@ class Sanitizer extends Wire {
* // Sanitize text for a search on title and body fields
* $q = $input->get->text('q'); // text search query
* $results = $pages->find("title|body%=" . $sanitizer->selectorValue($q));
*
* // In 3.0.127 you can also provide an array for the $value argument
* $val = $sanitizer->selectorValue([ 'foo', 'bar', 'baz' ]);
* echo $val; // outputs: foo|bar|baz
* ~~~~~
*
* #pw-group-strings
*
* @param string $value String value to sanitize (assumed to be UTF-8).
* @param string|array $value String value to sanitize (assumed to be UTF-8),
* or in 3.0.127+ you may use an array and it will be sanitized to an OR value string.
* @param array|int $options Options to modify behavior:
* - `maxLength` (int): Maximum number of allowed characters (default=100). This may also be specified instead of $options array.
* - `useQuotes` (bool): Allow selectorValue() function to add quotes if it deems them necessary? (default=true)
@@ -1797,6 +1802,18 @@ class Sanitizer extends Wire {
$options = array();
}
$options = array_merge($defaults, $options);
// if given an array, convert to an OR selector string
if(is_array($value)) {
$a = array();
foreach($value as $v) {
$v = $this->selectorValue($v, $options);
if($options['useQuotes'] && !strlen($v)) $v = '""';
$a[] = $v;
}
return implode('|', $a);
}
if(!is_string($value)) $value = $this->string($value);
$value = trim($value);
$quoteChar = '"';