From 25662aee2653f45e8f4a918b8be6999cc6445b75 Mon Sep 17 00:00:00 2001 From: Luis Nobrega Date: Tue, 20 Feb 2018 00:06:50 +0000 Subject: [PATCH] Added the following validators: isTrue, isFalse and isArray. --- CHANGELOG.md | 6 +- README.md | 3 + composer.json | 2 +- src/Helpers/ValidatorHelper.php | 46 ++++++++ tests/Helpers/ValidatorHelperTest.php | 159 +++++++++++++++++++++++--- 5 files changed, 196 insertions(+), 20 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ffe6762..798fe63 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,11 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). ## [Unreleased] -- Add the following validators: isArray, isEmail and isUrl. +- Add the following validators: isEmail and isUrl. + +## [1.0.3] - 2018-02-20 +### Added +- Added the following validators: isTrue, isFalse and isArray. ## [1.0.2] - 2018-02-17 ### Added diff --git a/README.md b/README.md index 73a7830..b409c45 100644 --- a/README.md +++ b/README.md @@ -15,8 +15,11 @@ This is a Base Model that can be extended to define Models. It helps handling da * isFloat * isString * isBoolean + * isTrue + * isFalse * isNull * isNotNull + * isArray * Model to array and JSON, preserving hidden attributes. * Define attributes using arrays of data. * Can define, when validation fails, if an exception is thrown. diff --git a/composer.json b/composer.json index a764626..ae5d4f4 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "lfbn/base-model", - "version": "1.0.2", + "version": "1.0.3", "description": "This is a Base Model that can be extended to define Models. It helps handling data validation, and data conversion.", "keywords": ["validation", "model"], "homepage": "https://github.com/lfbn/base-model", diff --git a/src/Helpers/ValidatorHelper.php b/src/Helpers/ValidatorHelper.php index b6fef56..b634310 100644 --- a/src/Helpers/ValidatorHelper.php +++ b/src/Helpers/ValidatorHelper.php @@ -126,6 +126,39 @@ public function isBoolean($value) return true; } + /** + * @param boolean $value + * @return bool + */ + public function isTrue($value) + { + if (true === $value || + 1 === $value || + '1' === $value + ) { + return true; + } + + return false; + } + + /** + * @param boolean $value + * @return bool + */ + public function isFalse($value) + { + if (null === $value || + false === $value || + 0 === $value || + '0' === $value + ) { + return true; + } + + return false; + } + /** * @param null $value * @return boolean @@ -151,4 +184,17 @@ public function isNotNull($value) return false; } + + /** + * @param array $value + * @return boolean + */ + public function isArray($value) + { + if (is_array($value)) { + return true; + } + + return false; + } } diff --git a/tests/Helpers/ValidatorHelperTest.php b/tests/Helpers/ValidatorHelperTest.php index 8430b7d..56c9e76 100644 --- a/tests/Helpers/ValidatorHelperTest.php +++ b/tests/Helpers/ValidatorHelperTest.php @@ -178,6 +178,50 @@ public function testIfIsBooleanSucceeds($input) ); } + /** + * @param mixed $input + * @dataProvider notTrueProvider + */ + public function testIfIsTrueFails($input) + { + $this->assertFalse( + $this->validatorHelperMock->isTrue($input) + ); + } + + /** + * @param boolean $input + * @dataProvider trueProvider + */ + public function testIfIsTrueSucceeds($input) + { + $this->assertTrue( + $this->validatorHelperMock->isBoolean($input) + ); + } + + /** + * @param mixed $input + * @dataProvider notFalseProvider + */ + public function testIfIsFalseFails($input) + { + $this->assertFalse( + $this->validatorHelperMock->isFalse($input) + ); + } + + /** + * @param boolean $input + * @dataProvider falseProvider + */ + public function testIfIsFalseSucceeds($input) + { + $this->assertTrue( + $this->validatorHelperMock->isFalse($input) + ); + } + /** * @param mixed $input * @dataProvider notNullProvider @@ -222,6 +266,28 @@ public function testIfIsNotNullSucceeds($input) ); } + /** + * @param mixed $input + * @dataProvider notArrayProvider + */ + public function testIfIsArrayFails($input) + { + $this->assertFalse( + $this->validatorHelperMock->isArray($input) + ); + } + + /** + * @param null $input + * @dataProvider arrayProvider + */ + public function testIfIsArraySucceeds($input) + { + $this->assertTrue( + $this->validatorHelperMock->isArray($input) + ); + } + /** * @return array */ @@ -249,22 +315,29 @@ public function notEmptyValuesProvider() /** * @return array */ - public function notNumericProvider() + public function numericProvider() { return [ - 'Not empty string' => ['test'], - 'Object' => [new \stdClass()], + 'Integer' => [1], + 'Float' => [6.3], ]; } /** * @return array */ - public function numericProvider() + public function notNumericProvider() { return [ - 'Integer' => [1], - 'Float' => [6.3], + 'Not empty string' => ['test'], + 'Object' => [new \stdClass()], + ]; + } + + public function integerProvider() + { + return [ + 'Integer' => [6] ]; } @@ -275,10 +348,10 @@ public function notIntegerProvider() ]; } - public function integerProvider() + public function floatProvider() { return [ - 'Integer' => [6] + 'Float' => [6.5] ]; } @@ -289,10 +362,10 @@ public function notFloatProvider() ]; } - public function floatProvider() + public function stringProvider() { return [ - 'Float' => [6.5] + 'String' => ['test'] ]; } @@ -304,10 +377,17 @@ public function notStringProvider() ]; } - public function stringProvider() + public function booleanProvider() { return [ - 'String' => ['test'] + 'String true' => ['true'], + 'String number one' => ['1'], + 'String number zero' => ['0'], + 'Number one' => [1], + 'Number zero' => [0], + 'String false' => ['false'], + 'True' => [true], + 'False' => [false] ]; } @@ -319,17 +399,42 @@ public function notBooleanProvider() ]; } - public function booleanProvider() + public function trueProvider() { return [ - 'String true' => ['true'], 'String number one' => ['1'], - 'String number zero' => ['0'], 'Number one' => [1], - 'Number zero' => [0], + 'True' => [true] + ]; + } + + public function notTrueProvider() + { + return [ 'String false' => ['false'], - 'True' => [true], - 'False' => [false] + 'String number one' => ['0'], + 'Number zero' => [0], + 'False' => [false], + 'Null' => [null] + ]; + } + + public function falseProvider() + { + return [ + 'String number zero' => ['0'], + 'Number zero' => [0], + 'False' => [false], + 'Null' => [null] + ]; + } + + public function notFalseProvider() + { + return [ + 'String number one' => ['1'], + 'Number one' => [1], + 'True' => [true] ]; } @@ -348,4 +453,22 @@ public function notNullProvider() 'Integer' => [123] ]; } + + public function arrayProvider() + { + return [ + 'Array' => [array()], + 'Array brackets' => [[]], + 'Associative array' => [array('teste' => 1)] + ]; + } + + public function notArrayProvider() + { + return [ + 'String' => [''], + 'Number' => [1], + 'Boolean' => [true] + ]; + } }