Skip to content

Commit

Permalink
BooleanChecker for check concrete boolean value (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
gam6itko authored Mar 17, 2023
1 parent 3bf2102 commit f26e6c8
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Bootloader/ValidatorBootloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ private function initDefaultConfig(TokenizerBootloader $tokenizer): void
'image' => Checker\ImageChecker::class,
'datetime' => Checker\DatetimeChecker::class,
'array' => Checker\ArrayChecker::class,
'boolean' => Checker\BooleanChecker::class,
],

// Enable/disable validation conditions
Expand Down
26 changes: 26 additions & 0 deletions src/Checker/BooleanChecker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Spiral\Validator\Checker;

use Spiral\Core\Container\SingletonInterface;
use Spiral\Validator\AbstractChecker;

final class BooleanChecker extends AbstractChecker implements SingletonInterface
{
public const MESSAGES = [
'isTrue' => '[[Should be true.]]',
'isFalse' => '[[Should be false.]]',
];

public function isTrue(mixed $value): bool
{
return true === $value;
}

public function isFalse(mixed $value): bool
{
return false === $value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public function dataHasCheckerByDefault(): \Traversable
yield ['image'];
yield ['datetime'];
yield ['array'];
yield ['boolean'];
}

public function dataHasConditionByDefault(): \Traversable
Expand Down
57 changes: 57 additions & 0 deletions tests/src/Unit/Checkers/BooleanCheckerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

namespace Spiral\Validator\Tests\Unit\Checkers;

use Spiral\Validator\Checker\BooleanChecker;
use Spiral\Validator\Tests\Unit\BaseTest;

final class BooleanCheckerTest extends BaseTest
{
/**
* @dataProvider dataIsTrue
*/
public function testIsAssoc(mixed $value, bool $expectedResult): void
{
$checker = new BooleanChecker();
self::assertSame($expectedResult, $checker->isTrue($value));
}

public function dataIsTrue(): iterable
{
yield [true, true];
yield [false, false];
// not boolean values
yield [[], false];
yield ['', false];
yield [1, false];
yield [2.0, false];
yield ['foo', false];
yield [null, false];
yield [new \stdClass(), false];
}

/**
* @dataProvider dataIsFalse
*/
public function testIsFalse(mixed $value, bool $expectedResult): void
{
$checker = new BooleanChecker();
self::assertSame($expectedResult, $checker->isFalse($value));
}

public function dataIsFalse(): iterable
{
yield [true, false];
yield [false, true];
// not boolean values
yield [[], false];
yield ['', false];
yield [1, false];
yield [2.0, false];
yield ['foo', false];
yield [null, false];
yield [new \stdClass(), false];
}
}

0 comments on commit f26e6c8

Please sign in to comment.