From e93290ad8b25723c6e100cf145551937fb482392 Mon Sep 17 00:00:00 2001 From: Ken Lalobo Date: Tue, 2 Jan 2018 18:32:28 +0000 Subject: [PATCH] add boolean --- src/TypeValidator/BooleanValidator.php | 39 +++++++++++++ src/Validator.php | 12 ++-- .../phpunit/src/Functional/ValidatorTest.php | 2 +- .../TypeValidator/BooleanValidatorTest.php | 56 +++++++++++++++++++ 4 files changed, 103 insertions(+), 6 deletions(-) create mode 100644 src/TypeValidator/BooleanValidator.php create mode 100644 tests/phpunit/src/Unit/TypeValidator/BooleanValidatorTest.php diff --git a/src/TypeValidator/BooleanValidator.php b/src/TypeValidator/BooleanValidator.php new file mode 100644 index 0000000..e8e67d0 --- /dev/null +++ b/src/TypeValidator/BooleanValidator.php @@ -0,0 +1,39 @@ + + */ + +namespace Mooti\Validator\TypeValidator; + +use Mooti\Factory\Factory; +use Mooti\Validator\Exception\DataValidationException; + +class BooleanValidator extends AbstractTypeValidator +{ + use Factory; + + /** + * Validate some data and throw an exception if the data invalid + * + * @param array $constraints The rules + * @param mixed $data The data to validate + * @param mixed $prettyName Human readable name for the data being validated + * + * @throws DataValidationException + */ + public function validate(array $constraints, $data, $prettyName = 'This value') + { + if (!is_bool($data)) { + $message = $constraints['message'] ?? '%s must be boolean'; + throw new DataValidationException(sprintf($message, $prettyName)); + } + + parent::validate($constraints, $data, $prettyName); + } +} diff --git a/src/Validator.php b/src/Validator.php index 365f9ec..d7e8ffb 100644 --- a/src/Validator.php +++ b/src/Validator.php @@ -24,16 +24,18 @@ class Validator { use Factory; - const TYPE_STRING = 'string'; - const TYPE_NUMBER = 'number'; - const TYPE_OBJECT = 'object'; - const TYPE_ARRAY = 'array'; + const TYPE_STRING = 'string'; + const TYPE_NUMBER = 'number'; + const TYPE_OBJECT = 'object'; + const TYPE_ARRAY = 'array'; + const TYPE_BOOLEAN = 'boolean'; protected $allowedTypeValidators = [ self::TYPE_STRING, self::TYPE_NUMBER, self::TYPE_OBJECT, - self::TYPE_ARRAY + self::TYPE_ARRAY, + self::TYPE_BOOLEAN ]; protected $errors = []; diff --git a/tests/phpunit/src/Functional/ValidatorTest.php b/tests/phpunit/src/Functional/ValidatorTest.php index 6ac563b..085d495 100644 --- a/tests/phpunit/src/Functional/ValidatorTest.php +++ b/tests/phpunit/src/Functional/ValidatorTest.php @@ -117,7 +117,7 @@ public function validate($data, $valid, $errors) public function dataToValidate() { - return [ + return [ [[], false, ['name' => ['Name is required'], 'title' => ['Title is required']]], [['title' => 1], false, ['name' => ['Name is required'], 'title' => ['Title must be a string']]], [['title' => null], false, ['name' => ['Name is required']]], diff --git a/tests/phpunit/src/Unit/TypeValidator/BooleanValidatorTest.php b/tests/phpunit/src/Unit/TypeValidator/BooleanValidatorTest.php new file mode 100644 index 0000000..79881a9 --- /dev/null +++ b/tests/phpunit/src/Unit/TypeValidator/BooleanValidatorTest.php @@ -0,0 +1,56 @@ +validate($constraints, $data); + } + + /** + * @test + * @dataProvider goodBooleans + */ + public function validateDataSuceeds($data) + { + $constraints = []; + + $typeValidator = new BooleanValidator(); + + $typeValidator->validate($constraints, $data); + } + + public function badBooleans() + { + return [ + ['foobar'], + [1], + [''], + [null] + ]; + } + + public function goodBooleans() + { + return [ + [true], + [false] + ]; + } +}