From d9ab9c56d5f4c6aac5ee302742907d5863bbe61d Mon Sep 17 00:00:00 2001 From: Ryan Cramer Date: Fri, 29 Mar 2019 15:30:32 -0400 Subject: [PATCH] Add $sanitizer->checkbox() method --- wire/core/Sanitizer.php | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/wire/core/Sanitizer.php b/wire/core/Sanitizer.php index da79f5e2..8fb0f916 100644 --- a/wire/core/Sanitizer.php +++ b/wire/core/Sanitizer.php @@ -3435,6 +3435,28 @@ class Sanitizer extends Wire { 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 * @@ -3452,6 +3474,7 @@ class Sanitizer extends Wire { * @param null|int $maxBytes Maximum allowed bytes (used for string types only) * @return array|bool|float|int|string * @since 3.0.125 + * @see Sanitizer::minLength() * */ 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 bool $padLeft Pad to left rather than right? (default=false) * @return string + * @see Sanitizer::maxLength() * */ public function minLength($value, $minLength = 1, $padChar = '', $padLeft = false) {