Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonTheAdams committed May 31, 2023
2 parents 67b0a4d + 33704b5 commit 3ba8604
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 0 deletions.
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}
*
* @since 1.4.0
*/
public static function id(): string
{
return 'boolean';
}

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

/**
* {@inheritDoc}
*
* @since 1.4.0
*/
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}
*
* @since 1.4.0
*/
public function serializeOption()
{
return null;
}

/**
* {@inheritDoc}
*
* @since 1.4.0
*/
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
{
/**
* @since 1.4.0
*
* @dataProvider booleansProvider
*/
public function testRuleValidatesBooleans($value, $pass)
{
$rule = new Boolean();

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

/**
* @since 1.4.0
*/
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],
];
}

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

0 comments on commit 3ba8604

Please sign in to comment.