mirror of
https://github.com/Respect/Validation.git
synced 2026-03-16 15:25:45 +01:00
Currently, the only way to handle when a validation fails is by `assert()`, which either throws an exception or doesn't. That means that every time a user wants to handle the results, they must use try-catch blocks, which may add some overhead. This commit introduces the `ResultQuery` class that wraps a `Result` object, providing an alternative to exception-based validation. This allows users to handle results directly without try-catch blocks. I’m taking a risky move here using the old method `validate()`, but I can’t think of a better name for this method.
28 lines
686 B
PHP
28 lines
686 B
PHP
<?php
|
|
|
|
/*
|
|
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
|
|
* SPDX-License-Identifier: MIT
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Respect\Validation\Test\Message;
|
|
|
|
use Respect\Validation\Message\Renderer;
|
|
use Respect\Validation\Message\StringFormatter;
|
|
use Respect\Validation\Result;
|
|
|
|
final class TestingStringFormatter implements StringFormatter
|
|
{
|
|
public function __construct(
|
|
private readonly string $prefix = '',
|
|
) {
|
|
}
|
|
|
|
/** @param array<string|int, mixed> $templates */
|
|
public function format(Result $result, Renderer $renderer, array $templates): string
|
|
{
|
|
return $this->prefix . $renderer->render($result, $templates);
|
|
}
|
|
}
|