Maintain local ⭤ remote in sync with automatic AT Protocol parity for Laravel (alpha & unstable)
at main 2.2 kB view raw
1<?php 2 3namespace SocialDept\AtpParity; 4 5use Illuminate\Database\Eloquent\Model; 6use SocialDept\AtpParity\Contracts\RecordMapper; 7use SocialDept\AtpSchema\Data\Data; 8 9/** 10 * Registry for RecordMapper instances. 11 * 12 * Allows looking up mappers by Record class, Model class, or lexicon NSID. 13 */ 14class MapperRegistry 15{ 16 /** @var array<class-string<Data>, RecordMapper> */ 17 protected array $byRecord = []; 18 19 /** @var array<class-string<Model>, RecordMapper> */ 20 protected array $byModel = []; 21 22 /** @var array<string, RecordMapper> Keyed by NSID */ 23 protected array $byLexicon = []; 24 25 /** 26 * Register a mapper. 27 */ 28 public function register(RecordMapper $mapper): void 29 { 30 $recordClass = $mapper->recordClass(); 31 $modelClass = $mapper->modelClass(); 32 33 $this->byRecord[$recordClass] = $mapper; 34 $this->byModel[$modelClass] = $mapper; 35 $this->byLexicon[$mapper->lexicon()] = $mapper; 36 } 37 38 /** 39 * Get a mapper by Record class. 40 * 41 * @param class-string<Data> $recordClass 42 */ 43 public function forRecord(string $recordClass): ?RecordMapper 44 { 45 return $this->byRecord[$recordClass] ?? null; 46 } 47 48 /** 49 * Get a mapper by Model class. 50 * 51 * @param class-string<Model> $modelClass 52 */ 53 public function forModel(string $modelClass): ?RecordMapper 54 { 55 return $this->byModel[$modelClass] ?? null; 56 } 57 58 /** 59 * Get a mapper by lexicon NSID. 60 */ 61 public function forLexicon(string $nsid): ?RecordMapper 62 { 63 return $this->byLexicon[$nsid] ?? null; 64 } 65 66 /** 67 * Check if a mapper exists for the given lexicon. 68 */ 69 public function hasLexicon(string $nsid): bool 70 { 71 return isset($this->byLexicon[$nsid]); 72 } 73 74 /** 75 * Get all registered lexicon NSIDs. 76 * 77 * @return array<string> 78 */ 79 public function lexicons(): array 80 { 81 return array_keys($this->byLexicon); 82 } 83 84 /** 85 * Get all registered mappers. 86 * 87 * @return array<RecordMapper> 88 */ 89 public function all(): array 90 { 91 return array_values($this->byLexicon); 92 } 93}