mirror of
https://github.com/Respect/Validation.git
synced 2024-05-29 03:42:25 +02:00
Apply contribution guidelines to "FilterVar" rule
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
parent
8c41838abe
commit
1255532501
|
@ -3,7 +3,7 @@
|
|||
- `FilterVar(int $filter)`
|
||||
- `FilterVar(int $filter, mixed $options)`
|
||||
|
||||
A wrapper for PHP's [filter_var()](http://php.net/filter_var) function.
|
||||
Validates the input with the PHP's [filter_var()](http://php.net/filter_var) function.
|
||||
|
||||
```php
|
||||
v::filterVar(FILTER_VALIDATE_EMAIL)->validate('bob@example.com'); // true
|
||||
|
|
|
@ -13,14 +13,9 @@ declare(strict_types=1);
|
|||
|
||||
namespace Respect\Validation\Exceptions;
|
||||
|
||||
class FilterVarException extends ValidationException
|
||||
/**
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
*/
|
||||
final class FilterVarException extends ValidationException
|
||||
{
|
||||
public static $defaultTemplates = [
|
||||
self::MODE_DEFAULT => [
|
||||
self::STANDARD => '{{name}} must be valid',
|
||||
],
|
||||
self::MODE_NEGATIVE => [
|
||||
self::STANDARD => '{{name}} must not be valid',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
|
|
@ -14,37 +14,45 @@ declare(strict_types=1);
|
|||
namespace Respect\Validation\Rules;
|
||||
|
||||
use Respect\Validation\Exceptions\ComponentException;
|
||||
use const FILTER_VALIDATE_BOOLEAN;
|
||||
use const FILTER_VALIDATE_EMAIL;
|
||||
use const FILTER_VALIDATE_FLOAT;
|
||||
use const FILTER_VALIDATE_INT;
|
||||
use const FILTER_VALIDATE_IP;
|
||||
use const FILTER_VALIDATE_REGEXP;
|
||||
use const FILTER_VALIDATE_URL;
|
||||
|
||||
class FilterVar extends Callback
|
||||
/**
|
||||
* Validates the input with the PHP's filter_var() function.
|
||||
*
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
*/
|
||||
final class FilterVar extends AbstractEnvelope
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
$arguments = func_get_args();
|
||||
if (!isset($arguments[0])) {
|
||||
throw new ComponentException('Cannot validate without filter flag');
|
||||
}
|
||||
private const ALLOWED_FILTERS = [
|
||||
FILTER_VALIDATE_BOOLEAN,
|
||||
FILTER_VALIDATE_EMAIL,
|
||||
FILTER_VALIDATE_FLOAT,
|
||||
FILTER_VALIDATE_INT,
|
||||
FILTER_VALIDATE_IP,
|
||||
FILTER_VALIDATE_REGEXP,
|
||||
FILTER_VALIDATE_URL,
|
||||
];
|
||||
|
||||
if (!$this->isValidFilter($arguments[0])) {
|
||||
/**
|
||||
* Initializes the rule.
|
||||
*
|
||||
* @param int $filter
|
||||
* @param mixed $options
|
||||
*
|
||||
* @throws ComponentException
|
||||
*/
|
||||
public function __construct(int $filter, $options = null)
|
||||
{
|
||||
if (!in_array($filter, self::ALLOWED_FILTERS)) {
|
||||
throw new ComponentException('Cannot accept the given filter');
|
||||
}
|
||||
|
||||
$this->callback = 'filter_var';
|
||||
$this->arguments = $arguments;
|
||||
}
|
||||
|
||||
private function isValidFilter($filter)
|
||||
{
|
||||
return in_array(
|
||||
$filter,
|
||||
[
|
||||
FILTER_VALIDATE_BOOLEAN,
|
||||
FILTER_VALIDATE_EMAIL,
|
||||
FILTER_VALIDATE_FLOAT,
|
||||
FILTER_VALIDATE_INT,
|
||||
FILTER_VALIDATE_IP,
|
||||
FILTER_VALIDATE_REGEXP,
|
||||
FILTER_VALIDATE_URL,
|
||||
]
|
||||
);
|
||||
parent::__construct(new Callback('filter_var', $filter, $options), []);
|
||||
}
|
||||
}
|
||||
|
|
37
tests/integration/rules/filterVar.phpt
Normal file
37
tests/integration/rules/filterVar.phpt
Normal file
|
@ -0,0 +1,37 @@
|
|||
--FILE--
|
||||
<?php
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Exceptions\FilterVarException;
|
||||
use Respect\Validation\Exceptions\NestedValidationException;
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
try {
|
||||
v::filterVar(FILTER_VALIDATE_IP)->check(42);
|
||||
} catch (FilterVarException $exception) {
|
||||
echo $exception->getMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::filterVar(FILTER_VALIDATE_BOOLEAN))->check('On');
|
||||
} catch (FilterVarException $exception) {
|
||||
echo $exception->getMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::filterVar(FILTER_VALIDATE_EMAIL)->assert(1.5);
|
||||
} catch (NestedValidationException $exception) {
|
||||
echo $exception->getFullMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::filterVar(FILTER_VALIDATE_FLOAT))->assert(1.0);
|
||||
} catch (NestedValidationException $exception) {
|
||||
echo $exception->getFullMessage().PHP_EOL;
|
||||
}
|
||||
?>
|
||||
--EXPECTF--
|
||||
42 must be valid
|
||||
"On" must not be valid
|
||||
- 1.5 must be valid
|
||||
- 1.0 must not be valid
|
|
@ -13,64 +13,57 @@ declare(strict_types=1);
|
|||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Respect\Validation\Test\RuleTestCase;
|
||||
use const FILTER_FLAG_QUERY_REQUIRED;
|
||||
use const FILTER_VALIDATE_BOOLEAN;
|
||||
use const FILTER_VALIDATE_EMAIL;
|
||||
use const FILTER_VALIDATE_FLOAT;
|
||||
use const FILTER_VALIDATE_INT;
|
||||
use const FILTER_VALIDATE_URL;
|
||||
|
||||
/**
|
||||
* @group rule
|
||||
* @group rule
|
||||
*
|
||||
* @covers \Respect\Validation\Rules\FilterVar
|
||||
* @covers \Respect\Validation\Exceptions\FilterVarException
|
||||
*
|
||||
* @author Gabriel Caruso <carusogabriel34@gmail.com>
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
*/
|
||||
class FilterVarTest extends TestCase
|
||||
final class FilterVarTest extends RuleTestCase
|
||||
{
|
||||
/**
|
||||
* @expectedException \Respect\Validation\Exceptions\ComponentException
|
||||
* @expectedExceptionMessage Cannot validate without filter flag
|
||||
*/
|
||||
public function testShouldThrowsExceptionWhenFilterIsNotDefined(): void
|
||||
{
|
||||
new FilterVar();
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @expectedException \Respect\Validation\Exceptions\ComponentException
|
||||
* @expectedExceptionMessage Cannot accept the given filter
|
||||
*/
|
||||
public function testShouldThrowsExceptionWhenFilterIsNotValid(): void
|
||||
public function itShouldThrowsExceptionWhenFilterIsNotValid(): void
|
||||
{
|
||||
new FilterVar(FILTER_SANITIZE_EMAIL);
|
||||
}
|
||||
|
||||
public function testShouldDefineFilterOnConstructor(): void
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function providerForValidInput(): array
|
||||
{
|
||||
$rule = new FilterVar(FILTER_VALIDATE_REGEXP);
|
||||
|
||||
$actualArguments = $rule->arguments;
|
||||
$expectedArguments = [FILTER_VALIDATE_REGEXP];
|
||||
|
||||
self::assertEquals($expectedArguments, $actualArguments);
|
||||
return [
|
||||
[new FilterVar(FILTER_VALIDATE_INT), '12345'],
|
||||
[new FilterVar(FILTER_VALIDATE_EMAIL), 'example@example.com'],
|
||||
[new FilterVar(FILTER_VALIDATE_FLOAT), 1.5],
|
||||
[new FilterVar(FILTER_VALIDATE_BOOLEAN), 'On'],
|
||||
[new FilterVar(FILTER_VALIDATE_URL, FILTER_FLAG_QUERY_REQUIRED), 'http://example.com?foo=bar'],
|
||||
];
|
||||
}
|
||||
|
||||
public function testShouldDefineFilterOptionsOnConstructor(): void
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function providerForInvalidInput(): array
|
||||
{
|
||||
$rule = new FilterVar(FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED);
|
||||
|
||||
$actualArguments = $rule->arguments;
|
||||
$expectedArguments = [FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED];
|
||||
|
||||
self::assertEquals($expectedArguments, $actualArguments);
|
||||
}
|
||||
|
||||
public function testShouldUseDefineFilterToValidate(): void
|
||||
{
|
||||
$rule = new FilterVar(FILTER_VALIDATE_EMAIL);
|
||||
|
||||
self::assertTrue($rule->validate('henriquemoody@users.noreply.github.com'));
|
||||
}
|
||||
|
||||
public function testShouldUseDefineFilterOptionsToValidate(): void
|
||||
{
|
||||
$rule = new FilterVar(FILTER_VALIDATE_URL, FILTER_FLAG_QUERY_REQUIRED);
|
||||
|
||||
self::assertTrue($rule->validate('http://example.com?foo=bar'));
|
||||
return [
|
||||
[new FilterVar(FILTER_VALIDATE_INT), 1.4],
|
||||
[new FilterVar(FILTER_VALIDATE_URL, FILTER_FLAG_QUERY_REQUIRED), 'http://example.com'],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue