Use variadics "AbstractFilterRule" rule

Since the library doesn't need to give support to version 5.4 or less of
PHP using variadics in the constructor of "AbstractFilterRule" seems
better than doing the whole validation.

This commit will also apply the contribution guidelines to
"AbstractFilterRule" and use a better naming for it.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
Henrique Moody 2018-09-24 21:48:41 +02:00
parent 6730576fe4
commit c99f3818b7
No known key found for this signature in database
GPG key ID: 221E9281655813A6
36 changed files with 80 additions and 312 deletions

View file

@ -1,7 +1,7 @@
# Alnum
- `Alnum()`
- `Alnum(string $additionalChars)`
- `Alnum(string ...$additionalChars)`
Validates whether the input is alphanumeric or not.
@ -13,6 +13,7 @@ v::alnum()->validate('foo 123'); // false
v::alnum(' ')->validate('foo 123'); // true
v::alnum()->validate('100%'); // false
v::alnum('%')->validate('100%'); // true
v::alnum('%', ',')->validate('10,5%'); // true
```
You can restrict case using the [Lowercase](Lowercase.md) and

View file

@ -1,7 +1,7 @@
# Alpha
- `Alpha()`
- `Alpha(string $additionalChars)`
- `Alpha(string ...$additionalChars)`
Validates whether the input contains only alphabetic characters. This is similar
to [Alnum](Alnum.md), but it does not allow numbers.
@ -11,6 +11,7 @@ v::alpha()->validate('some name'); // false
v::alpha(' ')->validate('some name'); // true
v::alpha()->validate('Cedric-Fabian'); // false
v::alpha('-')->validate('Cedric-Fabian'); // true
v::alpha('-', '\'')->validate('\'s-Gravenhage'); // true
```
You can restrict case using the [Lowercase](Lowercase.md) and

View file

@ -1,7 +1,7 @@
# Cntrl
- `Cntrl()`
- `Cntrl(string $additionalChars)`
- `Cntrl(string ...$additionalChars)`
This is similar to `Alnum()`, but only accepts control characters:

View file

@ -1,7 +1,7 @@
# Consonant
- `Consonant()`
- `Consonant(string $additionalChars)`
- `Consonant(string ...$additionalChars)`
Similar to `Alnum()`. Validates strings that contain only consonants:

View file

@ -1,7 +1,7 @@
# Digit
- `Digit()`
- `Digit(string $additionalChars)`
- `Digit(string ...$additionalChars)`
Validates whether the input contains only digits.
@ -9,7 +9,7 @@ Validates whether the input contains only digits.
v::digit()->validate('020 612 1851'); // false
v::digit(' ')->validate('020 612 1851'); // true
v::digit()->validate('172.655.537-21'); // false
v::digit('.-')->validate('172.655.537-21'); // true
v::digit('.', '-')->validate('172.655.537-21'); // true
```
## Changelog

View file

@ -1,7 +1,7 @@
# Graph
- `Graph()`
- `Graph(string $additionalChars)`
- `Graph(string ...$additionalChars)`
Validates all characters that are graphically represented.

View file

@ -1,7 +1,7 @@
# Printable
- `Printable()`
- `Printable(string $additionalChars)`
- `Printable(string ...$additionalChars)`
Similar to `Graph` but accepts whitespace.

View file

@ -1,7 +1,7 @@
# Punct
- `Punct()`
- `Punct(string $additionalChars)`
- `Punct(string ...$additionalChars)`
Accepts only punctuation characters:

View file

@ -1,7 +1,7 @@
# Space
- `Space()`
- `Space(string $additionalChars)`
- `Space(string ...$additionalChars)`
Accepts only whitespace:

View file

@ -1,6 +1,7 @@
# Vowel
- `Vowel()`
- `Vowel(string ...$additionalChars)`
Similar to `Alnum()`. Validates strings that contains only vowels:

View file

@ -1,6 +1,7 @@
# Xdigit
- `Xdigit()`
- `Xdigit(string ...$additionalChars)`
Accepts an hexadecimal number:

View file

@ -13,28 +13,37 @@ declare(strict_types=1);
namespace Respect\Validation\Rules;
use Respect\Validation\Exceptions\ComponentException;
use function implode;
use function is_scalar;
use function str_replace;
use function str_split;
/**
* @author Henrique Moody <henriquemoody@gmail.com>
* @author Nick Lombard <github@jigsoft.co.za>
*/
abstract class AbstractFilterRule extends AbstractRule
{
public $additionalChars = '';
/**
* @var string
*/
private $additionalChars;
abstract protected function validateClean($input);
public function __construct($additionalChars = '')
/**
* Initializes the rule with a list of characters to be ignored by the validation.
*
* @param string ...$additionalChars
*/
public function __construct(string ...$additionalChars)
{
if (!is_string($additionalChars)) {
throw new ComponentException('Invalid list of additional characters to be loaded');
}
$this->additionalChars .= $additionalChars;
$this->additionalChars = implode($additionalChars);
}
protected function filter($input)
{
return str_replace(str_split($this->additionalChars), '', $input);
}
abstract protected function validateFilteredInput(string $input): bool;
/**
* {@inheritdoc}
*/
public function validate($input): bool
{
if (!is_scalar($input)) {
@ -46,8 +55,13 @@ abstract class AbstractFilterRule extends AbstractRule
return false;
}
$cleanInput = $this->filter($stringInput);
$filteredInput = $this->filter($stringInput);
return '' === $cleanInput || $this->validateClean($cleanInput);
return '' === $filteredInput || $this->validateFilteredInput($filteredInput);
}
private function filter(string $input): string
{
return str_replace(str_split($this->additionalChars), '', $input);
}
}

View file

@ -30,7 +30,7 @@ final class Alnum extends AbstractFilterRule
/**
* {@inheritdoc}
*/
protected function validateClean($input)
protected function validateFilteredInput(string $input): bool
{
return ctype_alnum($input);
}

View file

@ -27,7 +27,7 @@ final class Alpha extends AbstractFilterRule
/**
* {@inheritdoc}
*/
protected function validateClean($input)
protected function validateFilteredInput(string $input): bool
{
return ctype_alpha($input);
}

View file

@ -15,7 +15,7 @@ namespace Respect\Validation\Rules;
class Cntrl extends AbstractFilterRule
{
protected function validateClean($input)
protected function validateFilteredInput(string $input): bool
{
return ctype_cntrl($input);
}

View file

@ -17,7 +17,7 @@ use function preg_match;
class Consonant extends AbstractFilterRule
{
protected function validateClean($input)
protected function validateFilteredInput(string $input): bool
{
return preg_match('/^(\s|[b-df-hj-np-tv-zB-DF-HJ-NP-TV-Z])*$/', $input) > 0;
}

View file

@ -27,7 +27,7 @@ final class Digit extends AbstractFilterRule
/**
* {@inheritdoc}
*/
public function validateClean($input)
protected function validateFilteredInput(string $input): bool
{
return ctype_digit($input);
}

View file

@ -15,7 +15,7 @@ namespace Respect\Validation\Rules;
class Graph extends AbstractFilterRule
{
protected function validateClean($input)
protected function validateFilteredInput(string $input): bool
{
return ctype_graph($input);
}

View file

@ -25,7 +25,7 @@ final class Printable extends AbstractFilterRule
/**
* {@inheritdoc}
*/
protected function validateClean($input)
protected function validateFilteredInput(string $input): bool
{
return ctype_print($input);
}

View file

@ -15,7 +15,7 @@ namespace Respect\Validation\Rules;
class Punct extends AbstractFilterRule
{
protected function validateClean($input)
protected function validateFilteredInput(string $input): bool
{
return ctype_punct($input);
}

View file

@ -15,7 +15,7 @@ namespace Respect\Validation\Rules;
class Space extends AbstractFilterRule
{
protected function validateClean($input)
protected function validateFilteredInput(string $input): bool
{
return ctype_space($input);
}

View file

@ -17,7 +17,7 @@ use function preg_match;
class Vowel extends AbstractFilterRule
{
protected function validateClean($input)
protected function validateFilteredInput(string $input): bool
{
return preg_match('/^(\s|[aeiouAEIOU])*$/', $input) > 0;
}

View file

@ -15,7 +15,7 @@ namespace Respect\Validation\Rules;
class Xdigit extends AbstractFilterRule
{
public function validateClean($input)
protected function validateFilteredInput(string $input): bool
{
return ctype_xdigit($input);
}

View file

@ -23,8 +23,8 @@ use Symfony\Component\Validator\Validator\ValidatorInterface;
/**
* @method static Validator allOf(Validatable ...$rule)
* @method static Validator alnum(string $additionalChars = null)
* @method static Validator alpha(string $additionalChars = null)
* @method static Validator alnum(string ...$additionalChars)
* @method static Validator alpha(string ...$additionalChars)
* @method static Validator alwaysInvalid()
* @method static Validator alwaysValid()
* @method static Validator anyOf(Validatable ...$rule)
@ -44,7 +44,8 @@ use Symfony\Component\Validator\Validator\ValidatorInterface;
* @method static Validator charset(string ...$charset)
* @method static Validator cnh()
* @method static Validator cnpj()
* @method static Validator consonant(string $additionalChars = null)
* @method static Validator cntrl(string ...$additionalChars)
* @method static Validator consonant(string ...$additionalChars)
* @method static Validator contains($containsValue, bool $identical = false)
* @method static Validator countable()
* @method static Validator countryCode(string $set = null)
@ -53,7 +54,7 @@ use Symfony\Component\Validator\Validator\ValidatorInterface;
* @method static Validator creditCard(string $brand = null)
* @method static Validator date(string $format = 'Y-m-d')
* @method static Validator dateTime(string $format = null)
* @method static Validator digit(string $additionalChars = null)
* @method static Validator digit(string ...$additionalChars)
* @method static Validator directory()
* @method static Validator domain(bool $tldCheck = true)
* @method static Validator each(Validatable $rule)
@ -73,7 +74,7 @@ use Symfony\Component\Validator\Validator\ValidatorInterface;
* @method static Validator finite()
* @method static Validator floatVal()
* @method static Validator floatType()
* @method static Validator graph(string $additionalChars = null)
* @method static Validator graph(string ...$additionalChars)
* @method static Validator greaterThan($compareTo)
* @method static Validator hexRgbColor()
* @method static Validator identical($value)
@ -132,8 +133,8 @@ use Symfony\Component\Validator\Validator\ValidatorInterface;
* @method static Validator positive()
* @method static Validator postalCode(string $countryCode)
* @method static Validator primeNumber()
* @method static Validator printable(string $additionalChars = null)
* @method static Validator punct(string $additionalChars = null)
* @method static Validator printable(string ...$additionalChars)
* @method static Validator punct(string ...$additionalChars)
* @method static Validator readable()
* @method static Validator regex(string $regex)
* @method static Validator resourceType()
@ -142,7 +143,7 @@ use Symfony\Component\Validator\Validator\ValidatorInterface;
* @method static Validator sf(Constraint $constraint, ValidatorInterface $validator = null)
* @method static Validator size(string $minSize = null, string $maxSize = null)
* @method static Validator slug()
* @method static Validator space(string $additionalChars = null)
* @method static Validator space(string ...$additionalChars)
* @method static Validator startsWith($startValue, bool $identical = false)
* @method static Validator stringType()
* @method static Validator stringVal()
@ -161,10 +162,10 @@ use Symfony\Component\Validator\Validator\ValidatorInterface;
* @method static Validator vatin(string $countryCode)
* @method static Validator version()
* @method static Validator videoUrl(string $service = null)
* @method static Validator vowel()
* @method static Validator vowel(string ...$additionalChars)
* @method static Validator when(Validatable $if, Validatable $then, Validatable $when = null)
* @method static Validator writable()
* @method static Validator xdigit(string $additionalChars = null)
* @method static Validator xdigit(string ...$additionalChars)
* @method static Validator yes($useLocale = false)
* @method static Validator zend($validator, array $params = null)
*/

View file

@ -1,60 +0,0 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the "LICENSE.md"
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Respect\Validation\Rules;
use PHPUnit\Framework\TestCase;
/**
* @covers \Respect\Validation\Rules\AbstractFilterRule
*/
class AbstractFilterRuleTest extends TestCase
{
/**
* @expectedException \Respect\Validation\Exceptions\ComponentException
* @expectedExceptionMessage Invalid list of additional characters to be loaded
*
* @test
*/
public function constructorShouldThrowExceptionIfParamIsNotString(): void
{
$this->getMockForAbstractClass(AbstractFilterRule::class, [1]);
}
/**
* @test
*/
public function validateShouldReturnTrueForValidArguments(): void
{
$filterRuleMock = $this->getMockForAbstractClass(AbstractFilterRule::class);
$filterRuleMock->expects(self::any())
->method('validateClean')
->will(self::returnValue(true));
self::assertTrue($filterRuleMock->validate('hey'));
}
/**
* @test
*/
public function validateShouldReturnFalseForInvalidArguments(): void
{
$filterRuleMock = $this->getMockForAbstractClass(AbstractFilterRule::class);
$filterRuleMock->expects(self::any())
->method('validateClean')
->will(self::returnValue(true));
self::assertFalse($filterRuleMock->validate(''));
self::assertFalse($filterRuleMock->validate([]));
}
}

View file

@ -19,6 +19,7 @@ use stdClass;
/**
* @group rule
*
* @covers \Respect\Validation\Rules\AbstractFilterRule
* @covers \Respect\Validation\Rules\Alnum
*
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
@ -30,26 +31,6 @@ use stdClass;
*/
final class AlnumTest extends RuleTestCase
{
/**
* @dataProvider providerForInvalidParams
* @expectedException \Respect\Validation\Exceptions\ComponentException
*
* @test
*/
public function invalidConstructorParamsShouldThrowComponentExceptionUponInstantiation($additional): void
{
new Alnum($additional);
}
public function providerForInvalidParams()
{
return [
[new stdClass()],
[[]],
[0x2],
];
}
/**
* {@inheritdoc}
*/
@ -69,6 +50,7 @@ final class AlnumTest extends RuleTestCase
[new Alnum('!@#$%^&*(){}'), '!@#$%^&*(){}abc123'],
[new Alnum('[]?+=/\\-_|"\',<>.'), '[]?+=/\\-_|"\',<>.abc123'],
[new Alnum("[]?+=/\\-_|\"',<>. \t\n"), "abc[]?+=/\\-_|\"',<>. \t\n123"],
[new Alnum('-', '*'), 'a-1*d'],
];
}

View file

@ -19,6 +19,7 @@ use stdClass;
/**
* @group rule
*
* @covers \Respect\Validation\Rules\AbstractFilterRule
* @covers \Respect\Validation\Rules\Alpha
*
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
@ -29,26 +30,6 @@ use stdClass;
*/
final class AlphaTest extends RuleTestCase
{
/**
* @dataProvider providerForInvalidParams
* @expectedException \Respect\Validation\Exceptions\ComponentException
*
* @test
*/
public function invalidConstructorParamsShouldThrowComponentException($additional): void
{
new Alpha($additional);
}
public function providerForInvalidParams(): array
{
return [
[new stdClass()],
[[]],
[0x2],
];
}
/**
* {@inheritdoc}
*/
@ -59,6 +40,7 @@ final class AlphaTest extends RuleTestCase
'alphabetic with one exception' => [new Alpha('.'), 'google.com'],
'alphabetic with multiple exceptions' => [new Alpha('0-9'), '0alg-anet9'],
'non-alphabetic with only exceptions' => [new Alpha('!@#$%^&*(){}'), '!@#$%^&*(){}'],
'multiple characters to ignore' => [new Alpha('-', ' '), 'a-b c'],
];
}

View file

@ -18,6 +18,7 @@ use PHPUnit\Framework\TestCase;
/**
* @group rule
* @covers \Respect\Validation\Exceptions\CntrlException
* @covers \Respect\Validation\Rules\AbstractFilterRule
* @covers \Respect\Validation\Rules\Cntrl
*/
class CntrlTest extends TestCase
@ -46,17 +47,6 @@ class CntrlTest extends TestCase
$validator->assert($invalidCntrl);
}
/**
* @dataProvider providerForInvalidParams
* @expectedException \Respect\Validation\Exceptions\ComponentException
*
* @test
*/
public function invalidConstructorParamsShouldThrowComponentExceptionUponInstantiation($additional): void
{
$validator = new Cntrl($additional);
}
/**
* @dataProvider providerAdditionalChars
*
@ -76,15 +66,6 @@ class CntrlTest extends TestCase
];
}
public function providerForInvalidParams()
{
return [
[new \stdClass()],
[[]],
[0x2],
];
}
public function providerForValidCntrl()
{
return [

View file

@ -18,6 +18,7 @@ use PHPUnit\Framework\TestCase;
/**
* @group rule
* @covers \Respect\Validation\Exceptions\ConsonantException
* @covers \Respect\Validation\Rules\AbstractFilterRule
* @covers \Respect\Validation\Rules\Consonant
*/
class ConsonantTest extends TestCase
@ -46,17 +47,6 @@ class ConsonantTest extends TestCase
$validator->assert($invalidConsonants);
}
/**
* @dataProvider providerForInvalidParams
* @expectedException \Respect\Validation\Exceptions\ComponentException
*
* @test
*/
public function invalidConstructorParamsShouldThrowComponentExceptionUponInstantiation($additional): void
{
$validator = new Consonant($additional);
}
/**
* @dataProvider providerAdditionalChars
*
@ -76,15 +66,6 @@ class ConsonantTest extends TestCase
];
}
public function providerForInvalidParams()
{
return [
[new \stdClass()],
[[]],
[0x2],
];
}
public function providerForValidConsonants()
{
return [

View file

@ -14,11 +14,11 @@ declare(strict_types=1);
namespace Respect\Validation\Rules;
use Respect\Validation\Test\RuleTestCase;
use stdClass;
/**
* @group rule
*
* @covers \Respect\Validation\Rules\AbstractFilterRule
* @covers \Respect\Validation\Rules\Digit
*
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
@ -29,29 +29,6 @@ use stdClass;
*/
final class DigitTest extends RuleTestCase
{
/**
* @test
*
* @expectedException \Respect\Validation\Exceptions\ComponentException
*
* @dataProvider providerForInvalidParams
*
* @param mixed $additional
*/
public function invalidConstructorParamsShouldThrowComponentExceptionUponInstantiation($additional): void
{
new Digit($additional);
}
public function providerForInvalidParams(): array
{
return [
[new stdClass()],
[[]],
[0x2],
];
}
/**
* @throws \Respect\Validation\Exceptions\ComponentException
*
@ -65,6 +42,7 @@ final class DigitTest extends RuleTestCase
'positive string-integer with one exception' => [new Digit('-'), '16-50'],
'positive string-integer with multiple exceptions' => [new Digit('.-'), '16-5.0'],
'only exceptions' => [new Digit('!@#$%^&*(){}'), '!@#$%^&*(){}'],
'multiple exceptions' => [new Digit('.', '-'), '012.071.070-69'],
'float' => [new Digit(), 1.0],
'boolean true' => [new Digit(), true],
'octal' => [new Digit(), 01],

View file

@ -18,6 +18,7 @@ use PHPUnit\Framework\TestCase;
/**
* @group rule
* @covers \Respect\Validation\Exceptions\GraphException
* @covers \Respect\Validation\Rules\AbstractFilterRule
* @covers \Respect\Validation\Rules\Graph
*/
class GraphTest extends TestCase
@ -46,17 +47,6 @@ class GraphTest extends TestCase
$validator->assert($invalidGraph);
}
/**
* @dataProvider providerForInvalidParams
* @expectedException \Respect\Validation\Exceptions\ComponentException
*
* @test
*/
public function invalidConstructorParamsShouldThrowComponentExceptionUponInstantiation($additional): void
{
$validator = new Graph($additional);
}
/**
* @dataProvider providerAdditionalChars
*
@ -76,15 +66,6 @@ class GraphTest extends TestCase
];
}
public function providerForInvalidParams()
{
return [
[new \stdClass()],
[[]],
[0x2],
];
}
public function providerForValidGraph()
{
return [

View file

@ -18,6 +18,7 @@ use Respect\Validation\Test\RuleTestCase;
/**
* @group rule
*
* @covers \Respect\Validation\Rules\AbstractFilterRule
* @covers \Respect\Validation\Rules\Printable
*/
final class PrintableTest extends RuleTestCase
@ -56,25 +57,4 @@ final class PrintableTest extends RuleTestCase
[$rule, 'foo'.chr(10).'bar'],
];
}
/**
* @test
*
* @dataProvider providerForInvalidParams
*
* @expectedException \Respect\Validation\Exceptions\ComponentException
*/
public function invalidConstructorParamsShouldThrowComponentExceptionUponInstantiation($additional): void
{
(new Printable($additional));
}
public function providerForInvalidParams(): array
{
return [
[new \stdClass()],
[[]],
[0x2],
];
}
}

View file

@ -18,6 +18,7 @@ use PHPUnit\Framework\TestCase;
/**
* @group rule
* @covers \Respect\Validation\Exceptions\PunctException
* @covers \Respect\Validation\Rules\AbstractFilterRule
* @covers \Respect\Validation\Rules\Punct
*/
class PunctTest extends TestCase
@ -46,17 +47,6 @@ class PunctTest extends TestCase
$validator->assert($invalidPunct);
}
/**
* @dataProvider providerForInvalidParams
* @expectedException \Respect\Validation\Exceptions\ComponentException
*
* @test
*/
public function invalidConstructorParamsShouldThrowComponentExceptionUponInstantiation($additional): void
{
$validator = new Punct($additional);
}
/**
* @dataProvider providerAdditionalChars
*
@ -76,15 +66,6 @@ class PunctTest extends TestCase
];
}
public function providerForInvalidParams()
{
return [
[new \stdClass()],
[[]],
[0x2],
];
}
public function providerForValidPunct()
{
return [

View file

@ -18,6 +18,7 @@ use PHPUnit\Framework\TestCase;
/**
* @group rule
* @covers \Respect\Validation\Exceptions\SpaceException
* @covers \Respect\Validation\Rules\AbstractFilterRule
* @covers \Respect\Validation\Rules\Space
*/
class SpaceTest extends TestCase
@ -46,17 +47,6 @@ class SpaceTest extends TestCase
$validator->assert($invalidSpace);
}
/**
* @dataProvider providerForInvalidParams
* @expectedException \Respect\Validation\Exceptions\ComponentException
*
* @test
*/
public function invalidConstructorParamsShouldThrowComponentExceptionUponInstantiation($additional): void
{
$validator = new Space($additional);
}
/**
* @dataProvider providerAdditionalChars
*
@ -76,15 +66,6 @@ class SpaceTest extends TestCase
];
}
public function providerForInvalidParams()
{
return [
[new \stdClass()],
[[]],
[0x2],
];
}
public function providerForValidSpace()
{
return [

View file

@ -18,6 +18,7 @@ use PHPUnit\Framework\TestCase;
/**
* @group rule
* @covers \Respect\Validation\Exceptions\VowelException
* @covers \Respect\Validation\Rules\AbstractFilterRule
* @covers \Respect\Validation\Rules\Vowel
*/
class VowelTest extends TestCase
@ -46,17 +47,6 @@ class VowelTest extends TestCase
$validator->assert($invalidVowels);
}
/**
* @dataProvider providerForInvalidParams
* @expectedException \Respect\Validation\Exceptions\ComponentException
*
* @test
*/
public function invalidConstructorParamsShouldThrowComponentExceptionUponInstantiation($additional): void
{
$validator = new Vowel($additional);
}
/**
* @dataProvider providerAdditionalChars
*
@ -76,15 +66,6 @@ class VowelTest extends TestCase
];
}
public function providerForInvalidParams()
{
return [
[new \stdClass()],
[[]],
[0x2],
];
}
public function providerForValidVowels()
{
return [

View file

@ -18,6 +18,7 @@ use PHPUnit\Framework\TestCase;
/**
* @group rule
* @covers \Respect\Validation\Exceptions\XdigitException
* @covers \Respect\Validation\Rules\AbstractFilterRule
* @covers \Respect\Validation\Rules\Xdigit
*/
class XdigitTest extends TestCase