Skip to content

Commit

Permalink
MixedChecker add accepted adn declined for handle forms and GET b…
Browse files Browse the repository at this point in the history
…ool-like values (#13)
  • Loading branch information
gam6itko authored Oct 24, 2023
1 parent d99a6dc commit 8fc21d0
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 2 deletions.
18 changes: 18 additions & 0 deletions src/Checker/MixedChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ final class MixedChecker extends AbstractChecker implements SingletonInterface
public const MESSAGES = [
'cardNumber' => '[[Please enter valid card number.]]',
'match' => '[[Fields {1} and {2} do not match.]]',
'accepted' => '[[Is not accepted.]]',
'declined' => '[[Is not declined.]]',
];

/**
Expand Down Expand Up @@ -56,4 +58,20 @@ public function match(mixed $value, string $field, bool $strict = false): bool

return $value == $this->getValidator()->getValue($field, null);
}

/**
* This is useful for validating "Terms of Service" acceptance or similar fields.
*/
public function accepted(mixed $value): bool
{
return in_array($value, [true, 1, '1', 'yes', 'on'], true);
}

/**
* Opposite version of `accepted`.
*/
public function declined(mixed $value): bool
{
return in_array($value, [false, 0, '0', 'no', 'off'], true);
}
}
70 changes: 68 additions & 2 deletions tests/src/Unit/Checkers/MixedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
namespace Spiral\Validator\Tests\Unit\Checkers;

use PHPUnit\Framework\TestCase;
use Spiral\Validator\Checker\MixedChecker;
use Spiral\Validation\ValidatorInterface;
use Spiral\Validator\Checker\MixedChecker;

final class MixedTest extends TestCase
{
/**
* @dataProvider cardsProvider
* @param bool $expected
* @param bool $expected
* @param mixed $card
*/
public function testCardNumber(bool $expected, $card): void
Expand Down Expand Up @@ -63,4 +63,70 @@ public function cardsProvider(): array
[false, []],
];
}

/**
* @dataProvider dataAccepted
*/
public function testAccepted(mixed $value, bool $expectedResult = true): void
{
$checker = new MixedChecker();
self::assertSame($expectedResult, $checker->accepted($value));
}

public function dataAccepted(): iterable
{
yield [true];
yield [1];
yield ['1'];
yield ['yes'];
yield ['on'];
// declined values
yield [false, false];
yield [0, false];
yield ['0', false];
yield ['no', false];
yield ['off', false];
// invalid values
yield [2, false];
yield ['2', false];
yield ['', false];
yield [" \n \t ", false];
yield [null, false];
yield [1.0, false];
yield [[], false];
yield [new \stdClass(), false];
}

/**
* @dataProvider dataDeclined
*/
public function testDeclined(mixed $value, bool $expectedResult = true): void
{
$checker = new MixedChecker();
self::assertSame($expectedResult, $checker->declined($value));
}

public function dataDeclined(): iterable
{
yield [false];
yield [0];
yield ['0'];
yield ['no'];
yield ['off'];
// accepted values
yield [true, false];
yield [1, false];
yield ['1', false];
yield ['yes', false];
yield ['on', false];
// invalid values
yield [2, false];
yield ['2', false];
yield ['', false];
yield [" \n \t ", false];
yield [null, false];
yield [1.0, false];
yield [[], false];
yield [new \stdClass(), false];
}
}

0 comments on commit 8fc21d0

Please sign in to comment.