respect-validation/library/Message/Stringifier/NameStringifier.php
Henrique Moody bf9b970e24
Move important value objects to the root namespace
The `Id`, `Name`, and `Path` value objects are not only message-related
concerns, they're part of the core of the library, hence it makes sense
to place them at the root namespace.
2025-12-21 11:14:47 +01:00

40 lines
835 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 || $raw->path->isOrphan()) {
return $raw->value;
}
return sprintf(
'%s (<- %s)',
$this->stringifier->stringify($raw->path, $depth),
$raw->value,
);
}
}