Something went wrong. Try again.
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849<?php
declare(strict_types=1);
namespace Wotnak\Atproto\Lexicon;
use Wotnak\Atproto\Identifier\NsidParser;
/** * In-memory Lexicon repository. * * @api */final class InMemoryLexiconRepository implements LexiconRepositoryInterface {
/** * @var array<string, \Wotnak\Atproto\Lexicon\LexiconDocument> */ private array $documents = [];
public function __construct( private readonly NsidParser $nsidParser = new NsidParser(), ) {}
/** * Adds or replaces a Lexicon document. */ public function add(LexiconDocument $document): void { $id = $this->nsidParser->normalize($document->id); if ($document->id !== $id) { throw new \InvalidArgumentException('Lexicon document id must be a normalized NSID.'); } $this->documents[$id] = $document; }
/** * Returns the document. */ public function getDocument(string $nsid): ?LexiconDocument { $id = \explode('#', $nsid, 2)[0]; if (!$this->nsidParser->isValid($id)) { return NULL; } $id = $this->nsidParser->normalize($id); return $this->documents[$id] ?? NULL; }
}