mirror of
https://github.com/Respect/Validation.git
synced 2024-06-01 13:22:24 +02:00
Merge branch '1.1'
This commit is contained in:
commit
57509520a4
|
@ -7,12 +7,15 @@ Validates alphanumeric characters from a-Z and 0-9.
|
|||
|
||||
```php
|
||||
v::alnum()->validate('foo 123'); // true
|
||||
v::alnum()->validate('number 100%'); // false
|
||||
v::alnum('%')->validate('number 100%'); // true
|
||||
```
|
||||
|
||||
A parameter for extra characters can be used:
|
||||
Because this rule allows whitespaces by default, you can separate additional
|
||||
characters with a whitespace:
|
||||
|
||||
```php
|
||||
v::alnum('-')->validate('foo - 123'); // true
|
||||
v::alnum('- ! :')->validate('foo :- 123 !'); // true
|
||||
```
|
||||
|
||||
This validator allows whitespace, if you want to
|
||||
|
|
|
@ -72,6 +72,31 @@ class NestedValidationException extends ValidationException implements IteratorA
|
|||
return 1 === $relatedExceptions->count() && !$childException instanceof NonOmissibleException;
|
||||
}
|
||||
|
||||
private function isSkippable(ValidationException $exception)
|
||||
{
|
||||
if (!$exception instanceof self) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (1 !== $exception->getRelated()->count()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!$exception->hasCustomTemplate()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return $this->hasChildTemplate($exception);
|
||||
}
|
||||
|
||||
private function hasChildTemplate(self $exception)
|
||||
{
|
||||
$exception->getRelated()->rewind();
|
||||
$childException = $exception->getRelated()->current();
|
||||
|
||||
return $childException->getMessage() === $exception->getMessage();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return SplObjectStorage
|
||||
*/
|
||||
|
@ -95,8 +120,7 @@ class NestedValidationException extends ValidationException implements IteratorA
|
|||
|
||||
if (isset($knownDepths[$currentDepthOriginal])) {
|
||||
$currentDepth = $knownDepths[$currentDepthOriginal];
|
||||
} elseif ($currentDepthOriginal > $lastDepthOriginal
|
||||
&& ($this->hasCustomTemplate() || 1 != $exceptionIterator->count())) {
|
||||
} elseif ($currentDepthOriginal > $lastDepthOriginal) {
|
||||
++$currentDepth;
|
||||
}
|
||||
|
||||
|
@ -154,18 +178,21 @@ class NestedValidationException extends ValidationException implements IteratorA
|
|||
*/
|
||||
public function getFullMessage()
|
||||
{
|
||||
$marker = '-';
|
||||
$messages = [];
|
||||
$exceptions = $this->getIterator();
|
||||
$leveler = 1;
|
||||
|
||||
if ($this->hasCustomTemplate() || 1 != count($exceptions)) {
|
||||
$messages[] = sprintf('%s %s', $marker, $this->getMessage());
|
||||
if (!$this->isSkippable($this)) {
|
||||
$leveler = 0;
|
||||
$messages[] = sprintf('- %s', $this->getMessage());
|
||||
}
|
||||
|
||||
$exceptions = $this->getIterator();
|
||||
foreach ($exceptions as $exception) {
|
||||
$depth = $exceptions[$exception]['depth'];
|
||||
$prefix = str_repeat(' ', $depth * 2);
|
||||
$messages[] = sprintf('%s%s %s', $prefix, $marker, $exception->getMessage());
|
||||
$messages[] = sprintf(
|
||||
'%s- %s',
|
||||
str_repeat(' ', ($exceptions[$exception]['depth'] - $leveler) * 2),
|
||||
$exception->getMessage()
|
||||
);
|
||||
}
|
||||
|
||||
return implode(PHP_EOL, $messages);
|
||||
|
|
|
@ -13,6 +13,7 @@ declare(strict_types=1);
|
|||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
use Respect\Validation\Exceptions\NestedValidationException;
|
||||
use Respect\Validation\Exceptions\ValidationException;
|
||||
use Respect\Validation\Validatable;
|
||||
use function array_filter;
|
||||
|
@ -121,4 +122,21 @@ abstract class AbstractComposite extends AbstractRule
|
|||
{
|
||||
return null !== $rule->getName();
|
||||
}
|
||||
|
||||
private function setExceptionTemplate(ValidationException $exception)
|
||||
{
|
||||
if (null === $this->template || $exception->hasCustomTemplate()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$exception->setTemplate($this->template);
|
||||
|
||||
if (!$exception instanceof NestedValidationException) {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ($exception->getRelated() as $relatedException) {
|
||||
$this->setExceptionTemplate($relatedException);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -27,7 +27,7 @@ final class IntVal extends AbstractRule
|
|||
{
|
||||
public function validate($input): bool
|
||||
{
|
||||
if (is_float($input)) {
|
||||
if (is_float($input) || is_bool($input)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
|
@ -124,7 +124,7 @@ class PostalCode extends Regex
|
|||
'NF' => "/^(\d{4})$/",
|
||||
'NG' => "/^(\d{6})$/",
|
||||
'NI' => "/^(\d{7})$/",
|
||||
'NL' => "/^(\d{4}[A-Z]{2})$/",
|
||||
'NL' => "/^(\d{4} ?[A-Z]{2})$/",
|
||||
'NO' => "/^(\d{4})$/",
|
||||
'NP' => "/^(\d{5})$/",
|
||||
'NZ' => "/^(\d{4})$/",
|
||||
|
@ -136,7 +136,7 @@ class PostalCode extends Regex
|
|||
'PL' => "/^(\d{2}-\d{3})$/",
|
||||
'PM' => '/^(97500)$/',
|
||||
'PR' => "/^(\d{9})$/",
|
||||
'PT' => "/^(\d{7})$/",
|
||||
'PT' => "/^(\d{7}|\d{4}-\d{3})$/",
|
||||
'PW' => '/^(96940)$/',
|
||||
'PY' => "/^(\d{4})$/",
|
||||
'RE' => "/^((97|98)(4|7|8)\d{2})$/",
|
||||
|
|
27
tests/integration/issue-1033.phpt
Normal file
27
tests/integration/issue-1033.phpt
Normal file
|
@ -0,0 +1,27 @@
|
|||
--DESCRIPTION--
|
||||
The previous output was:
|
||||
|
||||
- All of the required rules must pass for `{ "A", "B", "B" }`
|
||||
- Each item in `{ "A", "B", "B" }` must be valid
|
||||
- "A" must equal 1
|
||||
- "B" must equal 1
|
||||
- "B" must equal 1
|
||||
--FILE--
|
||||
<?php
|
||||
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Exceptions\AllOfException;
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
try {
|
||||
v::each(v::equals(1))->assert(['A', 'B', 'B']);
|
||||
} catch (AllOfException $exception) {
|
||||
echo $exception->getFullMessage().PHP_EOL;
|
||||
}
|
||||
?>
|
||||
--EXPECTF--
|
||||
- Each item in { "A", "B", "B" } must be valid
|
||||
- "A" must be equals 1
|
||||
- "B" must be equals 1
|
||||
- "B" must be equals 1
|
16
tests/integration/issue-619.phpt
Normal file
16
tests/integration/issue-619.phpt
Normal file
|
@ -0,0 +1,16 @@
|
|||
--FILE--
|
||||
<?php
|
||||
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Exceptions\ValidationException;
|
||||
use Respect\Validation\Rules\Instance;
|
||||
|
||||
try {
|
||||
(new Instance('stdClass'))->setTemplate('invalid object')->assert('test');
|
||||
} catch (ValidationException $exception) {
|
||||
print_r($exception->getMainMessage());
|
||||
}
|
||||
?>
|
||||
--EXPECTF--
|
||||
invalid object
|
19
tests/integration/issue-805.phpt
Normal file
19
tests/integration/issue-805.phpt
Normal file
|
@ -0,0 +1,19 @@
|
|||
--FILE--
|
||||
<?php
|
||||
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Exceptions\NestedValidationException;
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
try {
|
||||
v::key('email', v::email()->setTemplate('WRONG EMAIL!!!!!!'))->assert(['email' => 'qwe']);
|
||||
} catch (NestedValidationException $exception) {
|
||||
print_r($exception->getMessages());
|
||||
}
|
||||
?>
|
||||
--EXPECTF--
|
||||
Array
|
||||
(
|
||||
[0] => WRONG EMAIL!!!!!!
|
||||
)
|
|
@ -15,4 +15,3 @@ try {
|
|||
?>
|
||||
--EXPECTF--
|
||||
- "something" is not tasty
|
||||
- "something" must be between 1 and 2
|
||||
|
|
|
@ -89,7 +89,8 @@ class AlnumTest extends TestCase
|
|||
{
|
||||
return [
|
||||
['alganet', ''],
|
||||
['alganet', 'alganet'],
|
||||
['foo :- 123 !', '- ! :'],
|
||||
['number 100%', '%'],
|
||||
['0alg-anet0', '0-9'],
|
||||
['1', ''],
|
||||
["\t", ''],
|
||||
|
@ -110,6 +111,7 @@ class AlnumTest extends TestCase
|
|||
{
|
||||
return [
|
||||
['', ''],
|
||||
['number 100%', ''],
|
||||
['@#$', ''],
|
||||
['_', ''],
|
||||
['dgç', ''],
|
||||
|
|
|
@ -56,6 +56,8 @@ final class IntValTest extends RuleTestCase
|
|||
[$rule, '1.0'],
|
||||
[$rule, 1.0],
|
||||
[$rule, ' '],
|
||||
[$rule, true],
|
||||
[$rule, false],
|
||||
[$rule, 'Foo'],
|
||||
[$rule, '1.44'],
|
||||
[$rule, 1e-5],
|
||||
|
|
|
@ -56,6 +56,8 @@ final class MaxTest extends RuleTestCase
|
|||
[new Max('now'), '+1 minute'],
|
||||
[new Max('B'), 'C'],
|
||||
[new Max(new CountableStub(3)), 4],
|
||||
[new Max(1900), '2018-01-25'],
|
||||
[new Max(10.5), '2018-01-25'],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
|
@ -79,6 +79,8 @@ final class MinTest extends RuleTestCase
|
|||
[new Min(0), -250],
|
||||
[new Min(0), -50],
|
||||
[new Min(new CountableStub(1)), 0],
|
||||
[new Min(2040), '2018-01-25'],
|
||||
[new Min(10.5), '2018-01-25'],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
|
@ -103,6 +103,10 @@ class PostalCodeTest extends TestCase
|
|||
['US', '02179'],
|
||||
['YE', ''],
|
||||
['PL', '99-300'],
|
||||
['NL', '1012 GX'],
|
||||
['NL', '1012GX'],
|
||||
['PT', '3660-606'],
|
||||
['PT', '3660606'],
|
||||
];
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue