diff --git a/src/Validator/Boolean.php b/src/Validator/Boolean.php index bc91cc0a..30779941 100644 --- a/src/Validator/Boolean.php +++ b/src/Validator/Boolean.php @@ -24,6 +24,19 @@ */ class Boolean extends Validator { + /** + * @var bool + */ + protected $strings = false; + + /** + * @param array $strings + */ + public function __construct(bool $strings = false) + { + $this->strings = $strings; + } + /** * Get Description * @@ -46,10 +59,14 @@ public function getDescription() */ public function isValid($value) { - if (!\is_bool($value)) { - return false; + if($this->strings && ($value === 'true' || $value === 'false')) { + return true; + } + + if (\is_bool($value)) { + return true; } - return true; + return false; } } \ No newline at end of file diff --git a/tests/Validator/BooleanTest.php b/tests/Validator/BooleanTest.php index c0a682b6..3e9073e2 100755 --- a/tests/Validator/BooleanTest.php +++ b/tests/Validator/BooleanTest.php @@ -24,7 +24,6 @@ class BooleanTest extends TestCase public function setUp() { - $this->boolean = new Boolean(); } public function tearDown() @@ -34,9 +33,23 @@ public function tearDown() public function testIsValid() { - // Assertions + $this->boolean = new Boolean(); + + $this->assertEquals(true, $this->boolean->isValid(true)); + $this->assertEquals(true, $this->boolean->isValid(false)); + $this->assertEquals(false, $this->boolean->isValid('false')); + $this->assertEquals(false, $this->boolean->isValid('true')); + $this->assertEquals(false, $this->boolean->isValid(['string', 'string'])); + $this->assertEquals(false, $this->boolean->isValid('string')); + $this->assertEquals(false, $this->boolean->isValid(1)); + $this->assertEquals(false, $this->boolean->isValid(1.2)); + + $this->boolean = new Boolean(true); + $this->assertEquals(true, $this->boolean->isValid(true)); $this->assertEquals(true, $this->boolean->isValid(false)); + $this->assertEquals(true, $this->boolean->isValid('false')); + $this->assertEquals(true, $this->boolean->isValid('true')); $this->assertEquals(false, $this->boolean->isValid(['string', 'string'])); $this->assertEquals(false, $this->boolean->isValid('string')); $this->assertEquals(false, $this->boolean->isValid(1));