mirror of
https://github.com/Respect/Validation.git
synced 2024-06-08 08:42:15 +02:00
Apply contribution guidelines to "EndsWith" rule
Co-authored-by: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
parent
de53a4b4bd
commit
5f684e246f
|
@ -13,14 +13,22 @@ declare(strict_types=1);
|
|||
|
||||
namespace Respect\Validation\Exceptions;
|
||||
|
||||
class EndsWithException extends ValidationException
|
||||
/**
|
||||
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
* @author William Espindola <oi@williamespindola.com.br>
|
||||
*/
|
||||
final class EndsWithException extends ValidationException
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $defaultTemplates = [
|
||||
self::MODE_DEFAULT => [
|
||||
self::STANDARD => '{{name}} must end with ({{endValue}})',
|
||||
self::STANDARD => '{{name}} must end with {{endValue}}',
|
||||
],
|
||||
self::MODE_NEGATIVE => [
|
||||
self::STANDARD => '{{name}} must not end with ({{endValue}})',
|
||||
self::STANDARD => '{{name}} must not end with {{endValue}}',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
|
|
@ -13,17 +13,46 @@ declare(strict_types=1);
|
|||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
class EndsWith extends AbstractRule
|
||||
use function end;
|
||||
use function is_array;
|
||||
use function mb_detect_encoding;
|
||||
use function mb_strlen;
|
||||
use function mb_strripos;
|
||||
use function mb_strrpos;
|
||||
|
||||
/**
|
||||
* Validates only if the value is at the end of the input.
|
||||
*
|
||||
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
* @author Hugo Hamon <hugo.hamon@sensiolabs.com>
|
||||
* @author William Espindola <oi@williamespindola.com.br>
|
||||
*/
|
||||
final class EndsWith extends AbstractRule
|
||||
{
|
||||
/**
|
||||
* @var mixed
|
||||
*/
|
||||
public $endValue;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
public $identical;
|
||||
|
||||
public function __construct($endValue, $identical = false)
|
||||
/**
|
||||
* @param mixed $endValue
|
||||
* @param bool $identical
|
||||
*/
|
||||
public function __construct($endValue, bool $identical = false)
|
||||
{
|
||||
$this->endValue = $endValue;
|
||||
$this->identical = $identical;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function validate($input): bool
|
||||
{
|
||||
if ($this->identical) {
|
||||
|
@ -33,7 +62,7 @@ class EndsWith extends AbstractRule
|
|||
return $this->validateEquals($input);
|
||||
}
|
||||
|
||||
protected function validateEquals($input)
|
||||
private function validateEquals($input): bool
|
||||
{
|
||||
if (is_array($input)) {
|
||||
return end($input) == $this->endValue;
|
||||
|
@ -43,7 +72,7 @@ class EndsWith extends AbstractRule
|
|||
=== mb_strlen($input, $enc) - mb_strlen($this->endValue, $enc);
|
||||
}
|
||||
|
||||
protected function validateIdentical($input)
|
||||
private function validateIdentical($input): bool
|
||||
{
|
||||
if (is_array($input)) {
|
||||
return end($input) === $this->endValue;
|
||||
|
|
38
tests/integration/rules/endsWith.phpt
Normal file
38
tests/integration/rules/endsWith.phpt
Normal file
|
@ -0,0 +1,38 @@
|
|||
--FILE--
|
||||
<?php
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Exceptions\EndsWithException;
|
||||
use Respect\Validation\Exceptions\NestedValidationException;
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
try {
|
||||
v::endsWith('foo')->check('bar');
|
||||
} catch (EndsWithException $exception) {
|
||||
echo $exception->getMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::endsWith('foo'))->check(['bar', 'foo']);
|
||||
} catch (EndsWithException $exception) {
|
||||
echo $exception->getMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::endsWith('foo')->assert('');
|
||||
} catch (NestedValidationException $exception) {
|
||||
echo $exception->getFullMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::endsWith('foo'))->assert(['bar', 'foo']);
|
||||
} catch (NestedValidationException $exception) {
|
||||
echo $exception->getFullMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
?>
|
||||
--EXPECTF--
|
||||
"bar" must end with "foo"
|
||||
`{ "bar", "foo" }` must not end with "foo"
|
||||
- "" must end with "foo"
|
||||
- `{ "bar", "foo" }` must not end with "foo"
|
|
@ -13,60 +13,50 @@ 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\EndsWith
|
||||
* @covers \Respect\Validation\Exceptions\EndsWithException
|
||||
*
|
||||
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
|
||||
* @author Gabriel Caruso <carusogabriel34@gmail.com>
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
* @author William Espindola <oi@williamespindola.com.br>
|
||||
*/
|
||||
class EndsWithTest extends TestCase
|
||||
final class EndsWithTest extends RuleTestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider providerForEndsWith
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function testStringsEndingWithExpectedValueShouldPass($start, $input): void
|
||||
{
|
||||
$v = new EndsWith($start);
|
||||
self::assertTrue($v->__invoke($input));
|
||||
$v->check($input);
|
||||
$v->assert($input);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForNotEndsWith
|
||||
* @expectedException \Respect\Validation\Exceptions\EndsWithException
|
||||
*/
|
||||
public function testStringsNotEndingWithExpectedValueShouldNotPass($start, $input, $caseSensitive = false): void
|
||||
{
|
||||
$v = new EndsWith($start, $caseSensitive);
|
||||
self::assertFalse($v->__invoke($input));
|
||||
$v->assert($input);
|
||||
}
|
||||
|
||||
public function providerForEndsWith()
|
||||
public function providerForValidInput(): array
|
||||
{
|
||||
return [
|
||||
['foo', ['bar', 'foo']],
|
||||
['foo', 'barbazFOO'],
|
||||
['foo', 'barbazfoo'],
|
||||
['foo', 'foobazfoo'],
|
||||
['1', [2, 3, 1]],
|
||||
['1', [2, 3, '1'], true],
|
||||
[new EndsWith('foo'), ['bar', 'foo']],
|
||||
[new EndsWith('foo'), 'barbazFOO'],
|
||||
[new EndsWith('foo'), 'barbazfoo'],
|
||||
[new EndsWith('foo'), 'foobazfoo'],
|
||||
[new EndsWith('1'), [2, 3, 1]],
|
||||
[new EndsWith(1), [2, 3, 1]],
|
||||
[new EndsWith('1', true), [2, 3, '1']],
|
||||
];
|
||||
}
|
||||
|
||||
public function providerForNotEndsWith()
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function providerForInvalidInput(): array
|
||||
{
|
||||
return [
|
||||
['foo', ''],
|
||||
['bat', ['bar', 'foo']],
|
||||
['foo', 'barfaabaz'],
|
||||
['foo', 'barbazFOO', true],
|
||||
['foo', 'faabarbaz'],
|
||||
['foo', 'baabazfaa'],
|
||||
['foo', 'baafoofaa'],
|
||||
['1', [1, '1', 3], true],
|
||||
[new EndsWith('foo'), ''],
|
||||
[new EndsWith('bat'), ['bar', 'foo']],
|
||||
[new EndsWith('foo'), 'barfaabaz'],
|
||||
[new EndsWith('foo', true), 'barbazFOO'],
|
||||
[new EndsWith('foo'), 'faabarbaz'],
|
||||
[new EndsWith('foo'), 'baabazfaa'],
|
||||
[new EndsWith('foo'), 'baafoofaa'],
|
||||
[new EndsWith('1', true), [1, '1', 3]],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue