respect-validation/src/Message/Parameters/PathHandler.php
Henrique Moody 140bd36aa3
Rename library/ to src/
We've always considered renaming this directory, as it's not a common
standard to name `library` the directory where the source code of a
library it. Having it as `src/` is a common pattern we find in several
PHP libraries these days.

Acked-by: Alexandre Gomes Gaigalas <alganet@gmail.com>
2026-01-22 13:13:15 +01:00

50 lines
1.1 KiB
PHP

<?php
/*
* SPDX-License-Identifier: MIT
* SPDX-FileCopyrightText: (c) Respect Project Contributors
* SPDX-FileContributor: Henrique Moody <henriquemoody@gmail.com>
*/
declare(strict_types=1);
namespace Respect\Validation\Message\Parameters;
use Respect\Stringifier\Handler;
use Respect\Stringifier\Quoter;
use Respect\Validation\Path;
use function array_reverse;
use function implode;
final readonly class PathHandler implements Handler
{
public function __construct(
private Quoter $quoter,
) {
}
public function handle(mixed $raw, int $depth): string|null
{
if (!$raw instanceof Path) {
return null;
}
return $this->quoter->quote('.' . implode('.', array_reverse($this->getNodes($raw, []))), $depth);
}
/**
* @param array<string|int> $nodes
*
* @return non-empty-array<string|int>
*/
public function getNodes(Path $path, array $nodes): array
{
$nodes[] = $path->value;
if ($path->parent !== null) {
return $this->getNodes($path->parent, $nodes);
}
return $nodes;
}
}