1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-09 16:26:59 +02:00

Add $sanitizer->checkbox() method

This commit is contained in:
Ryan Cramer
2019-03-29 15:30:32 -04:00
parent 2a108b4cd0
commit d9ab9c56d5

View File

@@ -3435,6 +3435,28 @@ class Sanitizer extends Wire {
return $this->bool($value) ? 1 : 0; return $this->bool($value) ? 1 : 0;
} }
/**
* Sanitize checkbox value
*
* #pw-group-other
*
* @param int|bool|string|mixed|null $value Value to check
* @param int|bool|string|mixed|null $yes Value to return if checked (default=true)
* @param int|bool|string|mixed|null $no Value to return if not checked (default=false)
* @return int|bool|string|mixed|null Return value, based on $checked or $unchecked argument
* @since 3.0.128
* @see Sanitizer::bool(), Sanitizer::bit()
*
*/
public function checkbox($value, $yes = true, $no = false) {
if($value === '' || $value === '0' || $value === null || $value === false) {
return $no;
} else if(empty($value)) {
return $no; // array or other empty value
}
return $yes;
}
/** /**
* Limit length of given value to that specified * Limit length of given value to that specified
* *
@@ -3452,6 +3474,7 @@ class Sanitizer extends Wire {
* @param null|int $maxBytes Maximum allowed bytes (used for string types only) * @param null|int $maxBytes Maximum allowed bytes (used for string types only)
* @return array|bool|float|int|string * @return array|bool|float|int|string
* @since 3.0.125 * @since 3.0.125
* @see Sanitizer::minLength()
* *
*/ */
public function maxLength($value, $maxLength = 128, $maxBytes = null) { public function maxLength($value, $maxLength = 128, $maxBytes = null) {
@@ -3521,6 +3544,7 @@ class Sanitizer extends Wire {
* @param string $padChar Pad string with this character if it does not meet minimum length (default='') * @param string $padChar Pad string with this character if it does not meet minimum length (default='')
* @param bool $padLeft Pad to left rather than right? (default=false) * @param bool $padLeft Pad to left rather than right? (default=false)
* @return string * @return string
* @see Sanitizer::maxLength()
* *
*/ */
public function minLength($value, $minLength = 1, $padChar = '', $padLeft = false) { public function minLength($value, $minLength = 1, $padChar = '', $padLeft = false) {