respect-validation/library/Message/Stringifier/NameStringifier.php
Henrique Moody 8332d28acc
Do not overwrite existing names
This commit addresses the skipped tests by modifying certain core
concepts in the library. The way names work has always been somewhat
confusing, even to me. I’ve established that existing names will never
be overwritten, and if a path is already defined, we will change the
name to also include the path.

I’m not very happy with how I’m solving this problem, because the
`FirstResultStringFormatter` is changing some behaviour in the results
that seems to be something that should always happen before. But, for
the moment, I will keep it as is.
2025-12-26 14:48:23 +01:00

40 lines
809 B
PHP

<?php
declare(strict_types=1);
/*
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
namespace Respect\Validation\Message\Stringifier;
use Respect\Stringifier\Stringifier;
use Respect\Validation\Name;
use function sprintf;
final readonly class NameStringifier implements Stringifier
{
public function __construct(
private Stringifier $stringifier,
) {
}
public function stringify(mixed $raw, int $depth): string|null
{
if (!$raw instanceof Name) {
return null;
}
if ($raw->path === null) {
return $raw->value;
}
return sprintf(
'%s (<- %s)',
$this->stringifier->stringify($raw->path, $depth),
$raw->value,
);
}
}