Skip to content

Commit

Permalink
Added the following validators: isTrue, isFalse and isArray.
Browse files Browse the repository at this point in the history
  • Loading branch information
Luis Nobrega committed Feb 20, 2018
1 parent 9b1199f commit 25662ae
Show file tree
Hide file tree
Showing 5 changed files with 196 additions and 20 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
46 changes: 46 additions & 0 deletions src/Helpers/ValidatorHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}
}
159 changes: 141 additions & 18 deletions tests/Helpers/ValidatorHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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]
];
}

Expand All @@ -275,10 +348,10 @@ public function notIntegerProvider()
];
}

public function integerProvider()
public function floatProvider()
{
return [
'Integer' => [6]
'Float' => [6.5]
];
}

Expand All @@ -289,10 +362,10 @@ public function notFloatProvider()
];
}

public function floatProvider()
public function stringProvider()
{
return [
'Float' => [6.5]
'String' => ['test']
];
}

Expand All @@ -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]
];
}

Expand All @@ -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]
];
}

Expand All @@ -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]
];
}
}

0 comments on commit 25662ae

Please sign in to comment.