mirror of
https://github.com/Respect/Validation.git
synced 2024-06-01 05:12:50 +02:00
66faefd695
After many refactorings, no rules use the previous validation engine. That means we can remove the unused code from the repository and switch from the previous to the new validation engine everywhere. This commit will also soft deprecate the methods "validate()", and "check()" in all the rules and the "assert()" in all rules but the Validator itself. That means using those methods will still be allowed, but static analysis tools might complain. This is a big step toward releasing the next major version, as the code is pretty much the way it should be when I release the next version. There's some documentation to be updated, and I would like to change the behavior of a couple of rules. Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
70 lines
2.1 KiB
PHP
70 lines
2.1 KiB
PHP
<?php
|
|
|
|
/*
|
|
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
|
|
* SPDX-License-Identifier: MIT
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
use Respect\Validation\Exceptions\ValidationException;
|
|
use Respect\Validation\Validatable;
|
|
use Symfony\Component\VarExporter\VarExporter;
|
|
|
|
use function Respect\Stringifier\stringify;
|
|
|
|
function exceptionMessage(callable $callable, string $fallbackMessage = 'No exception was thrown'): void
|
|
{
|
|
try {
|
|
$callable();
|
|
echo $fallbackMessage . PHP_EOL;
|
|
} catch (ValidationException $exception) {
|
|
echo $exception->getMessage() . PHP_EOL;
|
|
}
|
|
}
|
|
|
|
function exceptionMessages(callable $callable, string $fallbackMessage = 'No exception was thrown'): void
|
|
{
|
|
try {
|
|
$callable();
|
|
echo $fallbackMessage . PHP_EOL;
|
|
} catch (ValidationException $exception) {
|
|
echo VarExporter::export($exception->getMessages()) . PHP_EOL;
|
|
}
|
|
}
|
|
|
|
function exceptionFullMessage(callable $callable, string $fallbackMessage = 'No exception was thrown'): void
|
|
{
|
|
try {
|
|
$callable();
|
|
echo $fallbackMessage . PHP_EOL;
|
|
} catch (ValidationException $exception) {
|
|
echo $exception->getFullMessage() . PHP_EOL;
|
|
}
|
|
}
|
|
|
|
/** @param array<string, array{0: Validatable, 1: mixed, 2?:string|array<string, mixed>}> $scenarios */
|
|
function run(array $scenarios): void
|
|
{
|
|
foreach ($scenarios as $description => $data) {
|
|
[$rule, $input, $template] = array_pad($data, 3, null);
|
|
echo $description . PHP_EOL;
|
|
echo str_repeat('⎺', strlen($description)) . PHP_EOL;
|
|
|
|
if (is_string($template)) {
|
|
$rule->setTemplate($template);
|
|
}
|
|
|
|
if (is_array($template)) {
|
|
$rule->setTemplates($template);
|
|
}
|
|
|
|
$fallbackMessage = 'No exception was thrown with: ' . stringify($input);
|
|
|
|
exceptionMessage(static fn() => $rule->check($input), $fallbackMessage);
|
|
exceptionFullMessage(static fn() => $rule->assert($input), $fallbackMessage);
|
|
exceptionMessages(static fn() => $rule->assert($input), $fallbackMessage);
|
|
echo PHP_EOL;
|
|
}
|
|
}
|