Apply contribution guidelines to "In" rule

This commit is contained in:
Danilo Benevides 2018-08-18 21:06:44 -03:00
parent d022a71f54
commit 2b8acfd790
4 changed files with 119 additions and 73 deletions

View file

@ -13,8 +13,16 @@ declare(strict_types=1);
namespace Respect\Validation\Exceptions;
class InException extends ValidationException
/**
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
* @author Danilo Benevides <danilobenevides01@gmail.com>
* @author Henrique Moody <henriquemoody@gmail.com>
*/
final class InException extends ValidationException
{
/**
* {@inheritdoc}
*/
public static $defaultTemplates = [
self::MODE_DEFAULT => [
self::STANDARD => '{{name}} must be in {{haystack}}',

View file

@ -13,18 +13,43 @@ declare(strict_types=1);
namespace Respect\Validation\Rules;
class In extends AbstractRule
{
public $haystack;
public $compareIdentical;
use function in_array;
use function mb_detect_encoding;
use function mb_stripos;
use function mb_strpos;
public function __construct($haystack, $compareIdentical = false)
/**
* Validates if the input can be found in a defined array or string.
*
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
* @author Danilo Benevides <danilobenevides01@gmail.com>
* @author Henrique Moody <henriquemoody@gmail.com>
*/
final class In extends AbstractRule
{
/**
* @var array|string
*/
private $haystack;
/**
* @var bool
*/
private $compareIdentical;
/**
* Initializes the rule with the haystack and optionally compareIdentical flag.
*
* @param array|string $haystack
* @param bool $compareIdentical
*/
public function __construct($haystack, bool $compareIdentical = false)
{
$this->haystack = $haystack;
$this->compareIdentical = $compareIdentical;
}
protected function validateEquals($input)
private function validateEquals($input): bool
{
if (is_array($this->haystack)) {
return in_array($input, $this->haystack);
@ -39,7 +64,7 @@ class In extends AbstractRule
return false !== mb_stripos($this->haystack, $inputString, 0, mb_detect_encoding($inputString));
}
protected function validateIdentical($input)
private function validateIdentical($input): bool
{
if (is_array($this->haystack)) {
return in_array($input, $this->haystack, true);
@ -54,6 +79,9 @@ class In extends AbstractRule
return false !== mb_strpos($this->haystack, $inputString, 0, mb_detect_encoding($inputString));
}
/**
* {@inheritdoc}
*/
public function validate($input): bool
{
if ($this->compareIdentical) {

View file

@ -0,0 +1,37 @@
--FILE--
<?php
require 'vendor/autoload.php';
use Respect\Validation\Exceptions\InException;
use Respect\Validation\Exceptions\NestedValidationException;
use Respect\Validation\Validator as v;
try {
v::in([3, 2])->check(1);
} catch (InException $exception) {
echo $exception->getMessage().PHP_EOL;
}
try {
v::not(v::in('foobar'))->check('foo');
} catch (InException $exception) {
echo $exception->getMessage().PHP_EOL;
}
try {
v::in([2, '1', 3], true)->assert('2');
} catch (NestedValidationException $exception) {
echo $exception->getFullMessage().PHP_EOL;
}
try {
v::not(v::in([2, '1', 3], true))->assert('1');
} catch (NestedValidationException $exception) {
echo $exception->getFullMessage().PHP_EOL;
}
?>
--EXPECTF--
1 must be in `{ 3, 2 }`
"foo" must not be in "foobar"
- "2" must be in `{ 2, "1", 3 }`
- "1" must not be in `{ 2, "1", 3 }`

View file

@ -13,85 +13,58 @@ declare(strict_types=1);
namespace Respect\Validation\Rules;
use PHPUnit\Framework\TestCase;
use Respect\Validation\Test\RuleTestCase;
/**
* @group rule
* @covers \Respect\Validation\Exceptions\InException
* @group rule
*
* @covers \Respect\Validation\Rules\In
*
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
* @author Danilo Benevides <danilobenevides01@gmail.com>
* @author Gabriel Caruso <carusogabriel34@gmail.com>
* @author Henrique Moody <henriquemoody@gmail.com>
*/
class InTest extends TestCase
final class InTest extends RuleTestCase
{
/**
* @dataProvider providerForIn
*
* @test
* {@inheritdoc}
*/
public function successInValidatorCases($input, $options = null): void
{
$v = new In($options);
self::assertTrue($v->__invoke($input));
$v->check($input);
$v->assert($input);
}
/**
* @dataProvider providerForNotIn
* @expectedException \Respect\Validation\Exceptions\InException
*
* @test
*/
public function invalidInChecksShouldThrowInException($input, $options, $strict = false): void
{
$v = new In($options, $strict);
self::assertFalse($v->__invoke($input));
$v->assert($input);
}
/**
* @expectedException \Respect\Validation\Exceptions\InException
* @expectedExceptionMessage "x" must be in `{ "foo", "bar" }`
*
* @test
*/
public function inCheckExceptionMessageWithArray(): void
{
$v = new In(['foo', 'bar']);
$v->assert('x');
}
public function providerForIn()
public function providerForValidInput(): array
{
return [
['', ['']],
[null, [null]],
['0', ['0']],
[0, [0]],
['foo', ['foo', 'bar']],
['foo', 'barfoobaz'],
['foo', 'foobarbaz'],
['foo', 'barbazfoo'],
['1', [1, 2, 3]],
['1', ['1', 2, 3], true],
[new In(''), ''],
[new In([null]), null],
[new In(['0']), '0'],
[new In([0]), 0],
[new In(['foo', 'bar']), 'foo'],
[new In('barfoobaz'), 'foo'],
[new In('foobarbaz'), 'foo'],
[new In('barbazfoo'), 'foo'],
[new In([1, 2, 3]), '1'],
[new In(['1', 2, 3], true), '1'],
];
}
public function providerForNotIn()
/**
* {@inheritdoc}
*/
public function providerForInvalidInput(): array
{
return [
[null, '0'],
[null, 0, true],
[null, '', true],
[null, []],
['', 'barfoobaz'],
[null, 'barfoobaz'],
[0, 'barfoobaz'],
['0', 'barfoobaz'],
['bat', ['foo', 'bar']],
['foo', 'barfaabaz'],
['foo', 'faabarbaz'],
['foo', 'baabazfaa'],
['1', [1, 2, 3], true],
[new In('0'), null],
[new In(0, true), null],
[new In('', true), null],
[new In([], true), null],
[new In('barfoobaz'), ''],
[new In('barfoobaz'), null],
[new In('barfoobaz'), 0],
[new In('barfoobaz'), '0'],
[new In(['foo', 'bar']), 'bat'],
[new In('barfaabaz'), 'foo'],
[new In('faabarbaz'), 'foo'],
[new In('baabazfaa'), 'foo'],
[new In([1, 2, 3], true), '1'],
];
}
}