Something went wrong. Try again.
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677<?php
declare(strict_types=1);
namespace Wotnak\Atproto\Lexicon;
/** * Tracks validation path, options, and issues. * * @api */final class ValidationContext {
/** * @var list<string> */ private array $path = ['$'];
/** * @var list<\Wotnak\Atproto\Lexicon\ValidationViolation> */ private array $violations = [];
public function __construct( public readonly ValidationOptions $options = new ValidationOptions(), ) {}
/** * Adds a violation at the current validation path. */ public function addViolation(string $message, string $code): void { $this->violations[] = new ValidationViolation($this->path(), $message, $code); }
/** * Adds a violation at an explicit validation path. */ public function addViolationAt(string $path, string $message, string $code): void { $this->violations[] = new ValidationViolation($path, $message, $code); }
/** * Runs a callback at a nested validation path. */ public function withPath(string $segment, callable $callback): mixed { $this->path[] = $segment; try { return $callback($this); } finally { \array_pop($this->path); } }
/** * Returns the current validation path. */ public function path(): string { $segments = $this->path; $path = \array_shift($segments) ?? '$'; foreach ($segments as $segment) { $path .= \str_starts_with($segment, '[') ? $segment : '.' . $segment; } return $path; }
/** * Returns the collected validation violations. * * @return list<\Wotnak\Atproto\Lexicon\ValidationViolation> */ public function violations(): array { return $this->violations; }
}