mirror of
https://github.com/Respect/Validation.git
synced 2026-03-18 08:09:51 +01:00
Besides the interface's name, everything already calls this type "Rule", not "Validatable." This commit puts a stone on it and renames the interface for better naming.
61 lines
1.9 KiB
PHP
61 lines
1.9 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\Rule;
|
|
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: Rule, 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;
|
|
|
|
$fallbackMessage = 'No exception was thrown with: ' . stringify($input);
|
|
|
|
exceptionMessage(static fn() => $rule->assert($input, $template), $fallbackMessage);
|
|
exceptionFullMessage(static fn() => $rule->assert($input, $template), $fallbackMessage);
|
|
exceptionMessages(static fn() => $rule->assert($input, $template), $fallbackMessage);
|
|
echo PHP_EOL;
|
|
}
|
|
}
|