-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
125 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')); | ||
} | ||
} |