Add inclusive messages to LengthException

This commit is contained in:
Mazen Touati 2019-03-27 15:27:46 +01:00
parent 166501804f
commit 9dca4bde60
No known key found for this signature in database
GPG key ID: A7A7927CA46B4633
2 changed files with 78 additions and 12 deletions

View file

@ -17,12 +17,15 @@ namespace Respect\Validation\Exceptions;
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
* @author Danilo Correa <danilosilva87@gmail.com>
* @author Henrique Moody <henriquemoody@gmail.com>
* @author Mazen Touati <mazen_touati@hotmail.com>
*/
final class LengthException extends ValidationException
{
public const BOTH = 'both';
public const LOWER = 'lower';
public const LOWER_INCLUSIVE = 'lower_inclusive';
public const GREATER = 'greater';
public const GREATER_INCLUSIVE = 'greater_inclusive';
public const EXACT = 'exact';
/**
@ -32,13 +35,17 @@ final class LengthException extends ValidationException
self::MODE_DEFAULT => [
self::BOTH => '{{name}} must have a length between {{minValue}} and {{maxValue}}',
self::LOWER => '{{name}} must have a length greater than {{minValue}}',
self::LOWER_INCLUSIVE => '{{name}} must have a length greater than or equal to {{minValue}}',
self::GREATER => '{{name}} must have a length lower than {{maxValue}}',
self::GREATER_INCLUSIVE => '{{name}} must have a length lower than or equal to {{maxValue}}',
self::EXACT => '{{name}} must have a length of {{maxValue}}',
],
self::MODE_NEGATIVE => [
self::BOTH => '{{name}} must not have a length between {{minValue}} and {{maxValue}}',
self::LOWER => '{{name}} must not have a length greater than {{minValue}}',
self::LOWER_INCLUSIVE => '{{name}} must not have a length greater than or equal to {{minValue}}',
self::GREATER => '{{name}} must not have a length lower than {{maxValue}}',
self::GREATER_INCLUSIVE => '{{name}} must not have a length lower than or equal to {{maxValue}}',
self::EXACT => '{{name}} must not have a length of {{maxValue}}',
],
];
@ -48,12 +55,14 @@ final class LengthException extends ValidationException
*/
protected function chooseTemplate(): string
{
$isInclusive = $this->getParam('inclusive');
if (!$this->getParam('minValue')) {
return self::GREATER;
return $isInclusive === true ? self::GREATER_INCLUSIVE : self::GREATER;
}
if (!$this->getParam('maxValue')) {
return self::LOWER;
return $isInclusive === true ? self::LOWER_INCLUSIVE : self::LOWER;
}
if ($this->getParam('minValue') == $this->getParam('maxValue')) {

View file

@ -1,5 +1,6 @@
--CREDITS--
Danilo Correa <danilosilva87@gmail.com>
Mazen Touati <mazen_touati@hotmail.com>
--FILE--
<?php
@ -12,7 +13,19 @@ use Respect\Validation\Exceptions\NestedValidationException;
use Respect\Validation\Validator as v;
try {
v::length(0, 5)->check('phpsp.org.br');
v::length(0, 5, false)->check('phpsp.org.br');
} catch (LengthException $exception) {
echo $exception->getMessage().PHP_EOL;
}
try {
v::length(0, 10)->check('phpsp.org.br');
} catch (LengthException $exception) {
echo $exception->getMessage().PHP_EOL;
}
try {
v::length(15, null, false)->check('phpsp.org.br');
} catch (LengthException $exception) {
echo $exception->getMessage().PHP_EOL;
}
@ -30,13 +43,25 @@ try {
}
try {
v::not(v::length(0, 20))->check('phpsp.org.br');
v::not(v::length(0, 15))->check('phpsp.org.br');
} catch (LengthException $exception) {
echo $exception->getMessage().PHP_EOL;
}
try {
v::not(v::length(10))->check('phpsp.org.br');
v::not(v::length(0, 20, false))->check('phpsp.org.br');
} catch (LengthException $exception) {
echo $exception->getMessage().PHP_EOL;
}
try {
v::not(v::length(10, null))->check('phpsp.org.br');
} catch (LengthException $exception) {
echo $exception->getMessage().PHP_EOL;
}
try {
v::not(v::length(5, null, false))->check('phpsp.org.br');
} catch (LengthException $exception) {
echo $exception->getMessage().PHP_EOL;
}
@ -48,7 +73,19 @@ try {
}
try {
v::length(0, 5)->assert('phpsp.org.br');
v::length(0, 5, false)->assert('phpsp.org.br');
} catch (NestedValidationException $exception) {
echo $exception->getFullMessage().PHP_EOL;
}
try {
v::length(0, 10)->assert('phpsp.org.br');
} catch (NestedValidationException $exception) {
echo $exception->getFullMessage().PHP_EOL;
}
try {
v::length(15, null, false)->assert('phpsp.org.br');
} catch (NestedValidationException $exception) {
echo $exception->getFullMessage().PHP_EOL;
}
@ -66,13 +103,25 @@ try {
}
try {
v::not(v::length(0, 20))->assert('phpsp.org.br');
v::not(v::length(0, 15))->assert('phpsp.org.br');
} catch (NestedValidationException $exception) {
echo $exception->getFullMessage().PHP_EOL;
}
try {
v::not(v::length(10))->assert('phpsp.org.br');
v::not(v::length(0, 20, false))->assert('phpsp.org.br');
} catch (NestedValidationException $exception) {
echo $exception->getFullMessage().PHP_EOL;
}
try {
v::not(v::length(10, null))->assert('phpsp.org.br');
} catch (NestedValidationException $exception) {
echo $exception->getFullMessage().PHP_EOL;
}
try {
v::not(v::length(5, null, false))->assert('phpsp.org.br');
} catch (NestedValidationException $exception) {
echo $exception->getFullMessage().PHP_EOL;
}
@ -85,14 +134,22 @@ try {
?>
--EXPECT--
"phpsp.org.br" must have a length lower than 5
"phpsp.org.br" must have a length greater than 20
"phpsp.org.br" must have a length lower than or equal to 10
"phpsp.org.br" must have a length greater than 15
"phpsp.org.br" must have a length greater than or equal to 20
"phpsp.org.br" must have a length between 5 and 10
"phpsp.org.br" must not have a length lower than or equal to 15
"phpsp.org.br" must not have a length lower than 20
"phpsp.org.br" must not have a length greater than 10
"phpsp.org.br" must not have a length greater than or equal to 10
"phpsp.org.br" must not have a length greater than 5
"phpsp.org.br" must not have a length between 5 and 20
- "phpsp.org.br" must have a length lower than 5
- "phpsp.org.br" must have a length greater than 20
- "phpsp.org.br" must have a length lower than or equal to 10
- "phpsp.org.br" must have a length greater than 15
- "phpsp.org.br" must have a length greater than or equal to 20
- "phpsp.org.br" must have a length between 5 and 10
- "phpsp.org.br" must not have a length lower than or equal to 15
- "phpsp.org.br" must not have a length lower than 20
- "phpsp.org.br" must not have a length greater than 10
- "phpsp.org.br" must not have a length greater than or equal to 10
- "phpsp.org.br" must not have a length greater than 5
- "phpsp.org.br" must not have a length between 5 and 20