mirror of
https://github.com/Respect/Validation.git
synced 2024-05-29 03:42:25 +02:00
Apply contribution guidelines to "Contains" rule
Co-Authored-By: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
parent
d1932b2e7f
commit
4976fbaf48
|
@ -13,14 +13,22 @@ declare(strict_types=1);
|
|||
|
||||
namespace Respect\Validation\Exceptions;
|
||||
|
||||
class ContainsException extends ValidationException
|
||||
/**
|
||||
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
* @author William Espindola <oi@williamespindola.com.br>
|
||||
*/
|
||||
final class ContainsException extends ValidationException
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $defaultTemplates = [
|
||||
self::MODE_DEFAULT => [
|
||||
self::STANDARD => '{{name}} must contain the value "{{containsValue}}"',
|
||||
self::STANDARD => '{{name}} must contain the value {{containsValue}}',
|
||||
],
|
||||
self::MODE_NEGATIVE => [
|
||||
self::STANDARD => '{{name}} must not contain the value "{{containsValue}}"',
|
||||
self::STANDARD => '{{name}} must not contain the value {{containsValue}}',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
|
|
@ -13,17 +13,47 @@ declare(strict_types=1);
|
|||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
class Contains extends AbstractRule
|
||||
{
|
||||
public $containsValue;
|
||||
public $identical;
|
||||
use function in_array;
|
||||
use function is_array;
|
||||
use function mb_detect_encoding;
|
||||
use function mb_stripos;
|
||||
use function mb_strpos;
|
||||
|
||||
public function __construct($containsValue, $identical = false)
|
||||
/**
|
||||
* Validates if the input contains some value.
|
||||
*
|
||||
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
* @author Marcelo Araujo <msaraujo@php.net>
|
||||
* @author William Espindola <oi@williamespindola.com.br>
|
||||
*/
|
||||
final class Contains extends AbstractRule
|
||||
{
|
||||
/**
|
||||
* @var mixed
|
||||
*/
|
||||
private $containsValue;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private $identical;
|
||||
|
||||
/**
|
||||
* Initializes the Contains rule.
|
||||
*
|
||||
* @param mixed $containsValue Value that will be sought
|
||||
* @param bool $identical Defines whether the value is identical, default is false
|
||||
*/
|
||||
public function __construct($containsValue, bool $identical = false)
|
||||
{
|
||||
$this->containsValue = (string) $containsValue;
|
||||
$this->identical = $identical;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function validate($input): bool
|
||||
{
|
||||
if ($this->identical) {
|
||||
|
@ -33,7 +63,7 @@ class Contains extends AbstractRule
|
|||
return $this->validateEquals($input);
|
||||
}
|
||||
|
||||
protected function validateEquals($input)
|
||||
private function validateEquals($input): bool
|
||||
{
|
||||
if (is_array($input)) {
|
||||
return in_array($this->containsValue, $input);
|
||||
|
@ -44,7 +74,7 @@ class Contains extends AbstractRule
|
|||
return false !== mb_stripos($inputString, $this->containsValue, 0, mb_detect_encoding($inputString));
|
||||
}
|
||||
|
||||
protected function validateIdentical($input)
|
||||
private function validateIdentical($input): bool
|
||||
{
|
||||
if (is_array($input)) {
|
||||
return in_array($this->containsValue, $input, true);
|
||||
|
|
38
tests/integration/rules/contains.phpt
Normal file
38
tests/integration/rules/contains.phpt
Normal file
|
@ -0,0 +1,38 @@
|
|||
--FILE--
|
||||
<?php
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Exceptions\ContainsException;
|
||||
use Respect\Validation\Exceptions\NestedValidationException;
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
try {
|
||||
v::contains('foo')->check('bar');
|
||||
} catch (ContainsException $exception) {
|
||||
echo $exception->getMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::contains('foo'))->check('fool');
|
||||
} catch (ContainsException $exception) {
|
||||
echo $exception->getMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::contains('foo')->assert(['bar']);
|
||||
} catch (NestedValidationException $exception) {
|
||||
echo $exception->getFullMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::contains('foo', true))->assert(['bar', 'foo']);
|
||||
} catch (NestedValidationException $exception) {
|
||||
echo $exception->getFullMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
?>
|
||||
--EXPECTF--
|
||||
"bar" must contain the value "foo"
|
||||
"fool" must not contain the value "foo"
|
||||
- `{ "bar" }` must contain the value "foo"
|
||||
- `{ "bar", "foo" }` must not contain the value "foo"
|
|
@ -13,93 +13,57 @@ declare(strict_types=1);
|
|||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Respect\Validation\Test\RuleTestCase;
|
||||
|
||||
/**
|
||||
* @group rule
|
||||
* @group rule
|
||||
*
|
||||
* @covers \Respect\Validation\Rules\Contains
|
||||
* @covers \Respect\Validation\Exceptions\ContainsException
|
||||
*
|
||||
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
|
||||
* @author Gabriel Caruso <carusogabriel34@gmail.com>
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
* @author Nawarian <nickolas@phpsp.org.br>
|
||||
* @author William Espindola <oi@williamespindola.com.br>
|
||||
*/
|
||||
class ContainsTest extends TestCase
|
||||
final class ContainsTest extends RuleTestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider providerForContainsIdentical
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function testStringsContainingExpectedIdenticalValueShouldPass($start, $input): void
|
||||
{
|
||||
$v = new Contains($start, true);
|
||||
self::assertTrue($v->validate($input));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForContains
|
||||
*/
|
||||
public function testStringsContainingExpectedValueShouldPass($start, $input): void
|
||||
{
|
||||
$v = new Contains($start, false);
|
||||
self::assertTrue($v->validate($input));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForNotContainsIdentical
|
||||
*/
|
||||
public function testStringsNotContainsExpectedIdenticalValueShouldNotPass($start, $input): void
|
||||
{
|
||||
$v = new Contains($start, true);
|
||||
self::assertFalse($v->validate($input));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForNotContains
|
||||
*/
|
||||
public function testStringsNotContainsExpectedValueShouldNotPass($start, $input): void
|
||||
{
|
||||
$v = new Contains($start, false);
|
||||
self::assertFalse($v->validate($input));
|
||||
}
|
||||
|
||||
public function providerForContains()
|
||||
public function providerForValidInput(): array
|
||||
{
|
||||
return [
|
||||
['foo', ['bar', 'foo']],
|
||||
['foo', 'barbazFOO'],
|
||||
['foo', 'barbazfoo'],
|
||||
['foo', 'foobazfoO'],
|
||||
['1', [2, 3, 1]],
|
||||
['1', [2, 3, '1']],
|
||||
[new Contains('foo', false), ['bar', 'foo']],
|
||||
[new Contains('foo', false), 'barbazFOO'],
|
||||
[new Contains('foo', false), 'barbazfoo'],
|
||||
[new Contains('foo', false), 'foobazfoO'],
|
||||
[new Contains('1', false), [2, 3, 1]],
|
||||
[new Contains('1', false), [2, 3, '1']],
|
||||
[new Contains('foo'), ['fool', 'foo']],
|
||||
[new Contains('foo'), 'barbazfoo'],
|
||||
[new Contains('foo'), 'foobazfoo'],
|
||||
[new Contains('1'), [2, 3, (string) 1]],
|
||||
[new Contains('1'), [2, 3, '1']],
|
||||
];
|
||||
}
|
||||
|
||||
public function providerForContainsIdentical()
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function providerForInvalidInput(): array
|
||||
{
|
||||
return [
|
||||
['foo', ['fool', 'foo']],
|
||||
['foo', 'barbazfoo'],
|
||||
['foo', 'foobazfoo'],
|
||||
['1', [2, 3, (string) 1]],
|
||||
['1', [2, 3, '1']],
|
||||
];
|
||||
}
|
||||
|
||||
public function providerForNotContains()
|
||||
{
|
||||
return [
|
||||
['foo', ''],
|
||||
['bat', ['bar', 'foo']],
|
||||
['foo', 'barfaabaz'],
|
||||
['foo', 'faabarbaz'],
|
||||
];
|
||||
}
|
||||
|
||||
public function providerForNotContainsIdentical()
|
||||
{
|
||||
return [
|
||||
['foo', ''],
|
||||
['bat', ['BAT', 'foo']],
|
||||
['bat', ['BaT', 'Batata']],
|
||||
['foo', 'barfaabaz'],
|
||||
['foo', 'barbazFOO'],
|
||||
['foo', 'faabarbaz'],
|
||||
[new Contains('foo', false), ''],
|
||||
[new Contains('bat', false), ['bar', 'foo']],
|
||||
[new Contains('foo', false), 'barfaabaz'],
|
||||
[new Contains('foo', false), 'faabarbaz'],
|
||||
[new Contains('foo', true), ''],
|
||||
[new Contains('bat', true), ['BAT', 'foo']],
|
||||
[new Contains('bat', true), ['BaT', 'Batata']],
|
||||
[new Contains('foo', true), 'barfaabaz'],
|
||||
[new Contains('foo', true), 'barbazFOO'],
|
||||
[new Contains('foo', true), 'faabarbaz'],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue