Skip to content

Commit

Permalink
Fixed loose boolean validation
Browse files Browse the repository at this point in the history
  • Loading branch information
eldadfux committed Jun 21, 2020
1 parent 1d01ede commit 1a4ce3f
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/Validator/Boolean.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,17 @@ class Boolean extends Validator
/**
* @var bool
*/
protected $strings = false;
protected $loose = false;

/**
* Pass true to accept true and false strings as valid booleans
* Pass true to accept true and false strings and integers 0 and 1 as valid boolean values
* This option is good for validating query string params.
*
* @param bool $strings
* @param bool $loose
*/
public function __construct(bool $strings = false)
public function __construct(bool $loose = false)
{
$this->strings = $strings;
$this->loose = $loose;
}

/**
Expand All @@ -62,7 +62,11 @@ public function getDescription()
*/
public function isValid($value)
{
if($this->strings && ($value === 'true' || $value === 'false')) {
if($this->loose && ($value === 'true' || $value === 'false')) { // Accept strings
return true;
}

if($this->loose && ($value === 1 || $value === 0)) { // Accept integers
return true;
}

Expand Down

0 comments on commit 1a4ce3f

Please sign in to comment.