1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-12 01:34:31 +02:00

Add 'trim' option to $sanitizer->array() method which trims string values in an array of leading/trailing whitespace. Default value for this option is true.

This commit is contained in:
Ryan Cramer
2021-12-03 10:48:12 -05:00
parent 025e75116b
commit b49a44e813

View File

@@ -3992,6 +3992,7 @@ class Sanitizer extends Wire {
* @param array $options Optional modifications to default behavior: * @param array $options Optional modifications to default behavior:
* - `maxItems` (int): Maximum items allowed in each array (default=0, which means no limit) * - `maxItems` (int): Maximum items allowed in each array (default=0, which means no limit)
* - `maxDepth` (int): Max nested array depth (default=0, which means no nesting allowed) Since 3.0.160 * - `maxDepth` (int): Max nested array depth (default=0, which means no nesting allowed) Since 3.0.160
* - `trim` (bool): Trim whitespace from front/back of each string item in array? (default=true) Since 3.0.190
* - `sanitizer` (string): Optionally specify sanitizer for array values as option rather than argument (default='') Since 3.0.165 * - `sanitizer` (string): Optionally specify sanitizer for array values as option rather than argument (default='') Since 3.0.165
* - `keySanitizer` (string): Optionally sanitize associative array keys with this method (default='') Since 3.0.167 * - `keySanitizer` (string): Optionally sanitize associative array keys with this method (default='') Since 3.0.167
* - The following options are only used if the provided $value is a string: * - The following options are only used if the provided $value is a string:
@@ -4009,11 +4010,12 @@ class Sanitizer extends Wire {
$defaults = array( $defaults = array(
'maxItems' => 0, 'maxItems' => 0,
'maxDepth' => 0, 'maxDepth' => 0,
'csv' => true, 'csv' => true,
'delimiter' => null, 'delimiter' => null,
'delimiters' => array('|', ','), 'delimiters' => array('|', ','),
'enclosure' => '"', 'enclosure' => '"',
'trim' => true,
'sanitizer' => null, 'sanitizer' => null,
'keySanitizer' => null, 'keySanitizer' => null,
); );
@@ -4036,22 +4038,27 @@ class Sanitizer extends Wire {
$value = array(get_class($value)); $value = array(get_class($value));
} }
} }
if(is_string($value) && $options['csv']) { if(is_string($value)) {
// value is string // value is string
$hasDelimiter = null; if($options['trim']) $value = trim($value);
$delimiters = is_null($options['delimiter']) ? $options['delimiters'] : array($options['delimiter']); if(!strlen($value)) {
foreach($delimiters as $delimiter) { return array();
if(strpos($value, $delimiter)) { } else if($options['csv']) {
$hasDelimiter = $delimiter; $hasDelimiter = null;
break; $delimiters = is_null($options['delimiter']) ? $options['delimiters'] : array($options['delimiter']);
foreach($delimiters as $delimiter) {
if(strpos($value, $delimiter)) {
$hasDelimiter = $delimiter;
break;
}
}
if($hasDelimiter !== null) {
$value = str_getcsv($value, $hasDelimiter, $options['enclosure']);
} else {
$value = array($value);
} }
} }
if($hasDelimiter !== null) { }
$value = str_getcsv($value, $hasDelimiter, $options['enclosure']);
} else {
$value = array($value);
}
}
if(!is_array($value)) { if(!is_array($value)) {
$value = array($value); $value = array($value);
} }
@@ -4059,13 +4066,16 @@ class Sanitizer extends Wire {
$depth++; $depth++;
foreach($value as $k => $v) { foreach($value as $k => $v) {
if(!is_array($v)) continue; if(is_array($v)) {
if($depth <= $options['maxDepth']) { if($depth <= $options['maxDepth']) {
// sanitize nested array recursively // sanitize nested array recursively
$value[$k] = $this->___array($v, $sanitizer, $options); $value[$k] = $this->___array($v, $sanitizer, $options);
} else { } else {
// remove nested array // remove nested array
unset($value[$k]); unset($value[$k]);
}
} else if(is_string($v)) {
if($options['trim']) $value[$k] = trim($v);
} }
} }
$depth--; $depth--;