Skip to content

Feature: add boolean rule #12

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

Merged
merged 1 commit into from
May 31, 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
63 changes: 63 additions & 0 deletions src/Rules/Boolean.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace StellarWP\Validation\Rules;

use Closure;
use StellarWP\Validation\Contracts\Sanitizer;
use StellarWP\Validation\Contracts\ValidatesOnFrontEnd;
use StellarWP\Validation\Contracts\ValidationRule;

class Boolean implements ValidationRule, ValidatesOnFrontEnd, Sanitizer
{
/**
* {@inheritDoc}
*
* @unreleased
*/
public static function id(): string
{
return 'boolean';
}

/**
* {@inheritDoc}
*
* @unreleased
*/
public static function fromString(string $options = null): ValidationRule
{
return new self();
}

/**
* {@inheritDoc}
*
* @unreleased
*/
public function __invoke($value, Closure $fail, string $key, array $values)
{
if (!filter_var($value, FILTER_VALIDATE_BOOLEAN)) {
$fail(sprintf(__('%s must be an boolean', '%TEXTDOMAIN%'), '{field}'));
}
}

/**
* {@inheritDoc}
*
* @unreleased
*/
public function serializeOption()
{
return null;
}

/**
* {@inheritDoc}
*
* @unreleased
*/
public function sanitize($value)
{
return filter_var($value, FILTER_VALIDATE_BOOLEAN);
}
}
2 changes: 2 additions & 0 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace StellarWP\Validation;

use StellarWP\Validation\Rules\Boolean;
use StellarWP\Validation\Rules\Currency;
use StellarWP\Validation\Rules\DateTime;
use StellarWP\Validation\Rules\Email;
Expand Down Expand Up @@ -48,6 +49,7 @@ class ServiceProvider
OptionalIf::class,
OptionalUnless::class,
DateTime::class,
Boolean::class,
];

/**
Expand Down
60 changes: 60 additions & 0 deletions tests/unit/Rules/BooleanTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace unit\Rules;

use StellarWP\Validation\Rules\Boolean;
use StellarWP\Validation\Tests\TestCase;

class BooleanTest extends TestCase
{
/**
* @unreleased
*
* @dataProvider booleansProvider
*/
public function testRuleValidatesBooleans($value, $pass)
{
$rule = new Boolean();

if ($pass) {
self::assertValidationRulePassed($rule, $value);
} else {
self::assertValidationRuleFailed($rule, $value);
}
}

/**
* @unreleased
*/
public function booleansProvider(): array
{
return [
// values that pass
[true, true],
[1, true],
['1', true],
['true', true],
['yes', true],
['on', true],

// values that fail
[false, false],
[0, false],
['0', false],
['false', false],
['no', false],
['off', false],
['abc', false],
];
}

/**
* @unreleased
*/
public function testCastsToBoolean()
{
$rule = new Boolean();
self::assertSame(true, $rule->sanitize('1'));
self::assertSame(false, $rule->sanitize('0'));
}
}