Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MixedChecker add accepted and declined #13

Merged
merged 1 commit into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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];
}
}