Something went wrong. Try again.
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152<?php
declare(strict_types=1);
namespace Wotnak\Atproto\Lexicon\Schema;
use Wotnak\Atproto\Lexicon\ValidationContext;
/** * Validates arrays against item and length constraints. * * @api */final class ArraySchema extends AbstractSchema {
public function __construct( private readonly SchemaInterface $items, private readonly ?int $minLength = NULL, private readonly ?int $maxLength = NULL, ) {}
/** * Validates the value. */ public function validateValue(mixed $value, ValidationContext $context): mixed { if (!\is_array($value) || !\array_is_list($value)) { $context->addViolation('Expected array.', 'invalid_type'); return $value; } $length = \count($value); if ($this->minLength !== NULL && $length < $this->minLength) { $context->addViolation(\sprintf('Expected at least %d items.', $this->minLength), 'min_length'); } if ($this->maxLength !== NULL && $length > $this->maxLength) { $context->addViolation(\sprintf('Expected at most %d items.', $this->maxLength), 'max_length'); }
$result = $value; foreach ($value as $delta => $item) { $validated = $context->withPath('[' . $delta . ']', fn (ValidationContext $child) => $this->items->validateValue( $item, $child, )); if ($context->options->mode === 'parse' && $validated !== $item) { $result[$delta] = $validated; } } return $result; }
}