Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748<?php
declare(strict_types=1);
namespace Wotnak\Atproto\Lexicon\Schema;
use Wotnak\Atproto\Lexicon\DataModelValidator;use Wotnak\Atproto\Lexicon\ValidationContext;
/** * Validates AT Protocol bytes objects. * * @api */final class BytesSchema extends AbstractSchema {
public function __construct( private readonly ?int $minLength = NULL, private readonly ?int $maxLength = NULL, private readonly DataModelValidator $dataModelValidator = new DataModelValidator(), ) {}
/** * Validates the value. */ public function validateValue(mixed $value, ValidationContext $context): mixed { if (!\is_array($value) || !$this->dataModelValidator->isBytesObject($value)) { $context->addViolation('Expected bytes object.', 'invalid_bytes'); return $value; } $encoded = $value['$bytes']; if (!\is_string($encoded)) { $context->addViolation('Expected bytes object.', 'invalid_bytes'); return $value; } $bytes = \base64_decode($encoded, TRUE); $length = \is_string($bytes) ? \strlen($bytes) : 0; if ($this->minLength !== NULL && $length < $this->minLength) { $context->addViolation(\sprintf('Expected at least %d bytes.', $this->minLength), 'min_length'); } if ($this->maxLength !== NULL && $length > $this->maxLength) { $context->addViolation(\sprintf('Expected at most %d bytes.', $this->maxLength), 'max_length'); } return $value; }
}