Skip to content

Commit

Permalink
add boolean
Browse files Browse the repository at this point in the history
  • Loading branch information
lalobo committed Jan 2, 2018
1 parent 4d8b4ab commit e93290a
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 6 deletions.
39 changes: 39 additions & 0 deletions src/TypeValidator/BooleanValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
/**
* Boolean Validator
*
* Array Validator class
*
* @package Mooti
* @subpackage Validator
* @author Ken Lalobo <ken@mooti.io>
*/

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);
}
}
12 changes: 7 additions & 5 deletions src/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/src/Functional/ValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']]],
Expand Down
56 changes: 56 additions & 0 deletions tests/phpunit/src/Unit/TypeValidator/BooleanValidatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Mooti\Test\PHPUnit\Validator\Unit\TypeValidator;

use Mooti\Validator\Exception\DataValidationException;
use Mooti\Validator\TypeValidator\BooleanValidator;

class BooleanValidatorTest extends \PHPUnit_Framework_TestCase
{

/**
* @test
* @expectedException \Mooti\Validator\Exception\DataValidationException
* @expectedExceptionMessage This value must be boolean
* @dataProvider badBooleans
*/
public function validateDataThrowsDataValidationException($data)
{
$constraints = [];

$typeValidator = new BooleanValidator();

$typeValidator->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]
];
}
}

0 comments on commit e93290a

Please sign in to comment.