mirror of
https://github.com/Respect/Validation.git
synced 2024-06-08 16:52:16 +02:00
Apply contribution guidelines to "No" rule
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
parent
22f3f80f4c
commit
7b53d54931
|
@ -16,7 +16,27 @@ v::no()->validate('Not'); // true
|
|||
|
||||
This rule is case insensitive.
|
||||
|
||||
If `$locale` is TRUE, uses the value of [nl_langinfo()](http://php.net/nl_langinfo) with `NOEXPR` constant.
|
||||
If `$locale` is `TRUE`, it will use the value of [nl_langinfo][] with `NOEXPR`
|
||||
constant, meaning that it will validate the input using your current location:
|
||||
|
||||
```php
|
||||
setlocale(LC_ALL, 'ru_RU');
|
||||
v::no(true)->validate('нет'); // true
|
||||
```
|
||||
|
||||
Be careful when using `$locale` as `TRUE` because the it's very permissive:
|
||||
|
||||
```php
|
||||
v::no(true)->validate('Never gonna give you up 🎵'); // true
|
||||
```
|
||||
|
||||
Besides that, with `$locale` as `TRUE` it will consider any character starting
|
||||
with "N" as valid:
|
||||
|
||||
```php
|
||||
setlocale(LC_ALL, 'es_ES');
|
||||
v::no(true)->validate('Yes'); // true
|
||||
```
|
||||
|
||||
## Changelog
|
||||
|
||||
|
|
|
@ -13,11 +13,12 @@ declare(strict_types=1);
|
|||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
use function defined;
|
||||
use function nl_langinfo;
|
||||
use const NOEXPR;
|
||||
|
||||
/**
|
||||
* Validates if value is considered as "No".
|
||||
*
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
*/
|
||||
final class No extends AbstractEnvelope
|
||||
|
@ -25,7 +26,7 @@ final class No extends AbstractEnvelope
|
|||
public function __construct(bool $useLocale = false)
|
||||
{
|
||||
$pattern = '^n(o(t|pe)?|ix|ay)?$';
|
||||
if ($useLocale && defined('NOEXPR')) {
|
||||
if ($useLocale) {
|
||||
$pattern = nl_langinfo(NOEXPR);
|
||||
}
|
||||
|
||||
|
|
42
tests/integration/rules/no.phpt
Normal file
42
tests/integration/rules/no.phpt
Normal file
|
@ -0,0 +1,42 @@
|
|||
--CREDITS--
|
||||
Henrique Moody <henriquemoody@gmail.com>
|
||||
--FILE--
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Exceptions\NestedValidationException;
|
||||
use Respect\Validation\Exceptions\NoException;
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
try {
|
||||
v::not(v::no())->check('No');
|
||||
} catch (NoException $exception) {
|
||||
echo $exception->getMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::no()->check('Yes');
|
||||
} catch (NoException $exception) {
|
||||
echo $exception->getMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::no())->assert('No');
|
||||
} catch (NestedValidationException $exception) {
|
||||
echo $exception->getFullMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::no()->assert('Yes');
|
||||
} catch (NestedValidationException $exception) {
|
||||
echo $exception->getFullMessage().PHP_EOL;
|
||||
}
|
||||
?>
|
||||
--EXPECT--
|
||||
"No" is considered as "No"
|
||||
"Yes" is not considered as "No"
|
||||
- "No" is considered as "No"
|
||||
- "Yes" is not considered as "No"
|
|
@ -13,7 +13,9 @@ declare(strict_types=1);
|
|||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
use Respect\Validation\Test\TestCase;
|
||||
use Respect\Validation\Test\RuleTestCase;
|
||||
use function setlocale;
|
||||
use const LC_ALL;
|
||||
|
||||
/**
|
||||
* @group rule
|
||||
|
@ -23,58 +25,109 @@ use Respect\Validation\Test\TestCase;
|
|||
* @author Gabriel Caruso <carusogabriel34@gmail.com>
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
*/
|
||||
final class NoTest extends TestCase
|
||||
final class NoTest extends RuleTestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider validNoProvider
|
||||
*
|
||||
* @test
|
||||
* @var string
|
||||
*/
|
||||
public function shouldValidatePatternAccordingToTheDefinedLocale(string $input): void
|
||||
{
|
||||
$rule = new No();
|
||||
private $locale;
|
||||
|
||||
self::assertTrue($rule->validate($input));
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->locale = setlocale(LC_ALL, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
protected function tearDown(): void
|
||||
{
|
||||
setlocale(LC_ALL, $this->locale);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function providerForValidInput(): array
|
||||
{
|
||||
$sut = new No();
|
||||
|
||||
return [
|
||||
[$sut, 'N'],
|
||||
[$sut, 'Nay'],
|
||||
[$sut, 'Nix'],
|
||||
[$sut, 'No'],
|
||||
[$sut, 'Nope'],
|
||||
[$sut, 'Not'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function providerForInvalidInput(): array
|
||||
{
|
||||
$sut = new No();
|
||||
|
||||
return [
|
||||
[$sut, 'Donnot'],
|
||||
[$sut, 'Never'],
|
||||
[$sut, 'Niet'],
|
||||
[$sut, 'Noooooooo'],
|
||||
[$sut, 'Não'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[][]
|
||||
*/
|
||||
public function validNoProvider(): array
|
||||
public function providerForValidInputWithLocale(): array
|
||||
{
|
||||
return [
|
||||
['N'],
|
||||
['Nay'],
|
||||
['Nix'],
|
||||
['No'],
|
||||
['Nope'],
|
||||
['Not'],
|
||||
'nl' => ['nl_NL.UTF-8', 'Nee'],
|
||||
'pt' => ['pt_BR.UTF-8', 'Não'],
|
||||
'ru' => ['ru_RU.UTF-8', 'нет'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider invalidNoProvider
|
||||
*
|
||||
* @test
|
||||
*/
|
||||
public function shouldNotValidatePatternAccordingToTheDefinedLocale(string $input): void
|
||||
{
|
||||
$rule = new No();
|
||||
|
||||
self::assertFalse($rule->validate($input));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[][]
|
||||
*/
|
||||
public function invalidNoProvider(): array
|
||||
public function providerForInvalidInputWithLocale(): array
|
||||
{
|
||||
return [
|
||||
['Donnot'],
|
||||
['Never'],
|
||||
['Niet'],
|
||||
['Noooooooo'],
|
||||
['Não'],
|
||||
'nl' => ['nl_NL.UTF-8', 'Ez'],
|
||||
'pt' => ['pt_BR.UTF-8', 'нет'],
|
||||
'ru' => ['pt_BR.UTF-8', 'Οχι'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @dataProvider providerForValidInputWithLocale
|
||||
*/
|
||||
public function itShouldValidateInputAccordingToTheLocale(string $locale, string $input): void
|
||||
{
|
||||
setlocale(LC_ALL, $locale);
|
||||
|
||||
self::assertEquals($locale, setlocale(LC_ALL, 0));
|
||||
self::assertValidInput(new No(true), $input);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @dataProvider providerForInvalidInputWithLocale
|
||||
*/
|
||||
public function itShouldInvalidateInputAccordingToTheLocale(string $locale, string $input): void
|
||||
{
|
||||
setlocale(LC_ALL, $locale);
|
||||
|
||||
self::assertEquals($locale, setlocale(LC_ALL, 0));
|
||||
self::assertInvalidInput(new No(true), $input);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue