Resolve AT Protocol DIDs, handles, and schemas with intelligent caching for Laravel
at dev 1.8 kB view raw
1<?php 2 3namespace SocialDept\AtpResolver\Resolvers; 4 5use SocialDept\AtpResolver\Contracts\DidResolver; 6use SocialDept\AtpResolver\Data\DidDocument; 7use SocialDept\AtpResolver\Exceptions\DidResolutionException; 8use SocialDept\AtpResolver\Support\Concerns\ParsesDid; 9 10class DidResolverManager implements DidResolver 11{ 12 use ParsesDid; 13 14 /** 15 * @var array<string, DidResolver> 16 */ 17 protected array $resolvers = []; 18 19 /** 20 * Create a new DID resolver manager instance. 21 */ 22 public function __construct() 23 { 24 $this->registerDefaultResolvers(); 25 } 26 27 /** 28 * Resolve a DID to a DID Document. 29 * 30 * @param string $did The DID to resolve 31 */ 32 public function resolve(string $did): DidDocument 33 { 34 $method = $this->extractMethod($did); 35 36 if (! $this->supports($method)) { 37 throw DidResolutionException::unsupportedMethod($method); 38 } 39 40 return $this->resolvers[$method]->resolve($did); 41 } 42 43 /** 44 * Check if this resolver supports the given DID method. 45 * 46 * @param string $method The DID method 47 */ 48 public function supports(string $method): bool 49 { 50 return isset($this->resolvers[$method]); 51 } 52 53 /** 54 * Register a DID resolver for a specific method. 55 * 56 * @param string $method 57 * @param DidResolver $resolver 58 */ 59 public function register(string $method, DidResolver $resolver): self 60 { 61 $this->resolvers[$method] = $resolver; 62 63 return $this; 64 } 65 66 /** 67 * Register the default resolvers. 68 */ 69 protected function registerDefaultResolvers(): void 70 { 71 $this->register('plc', new PlcDidResolver()); 72 $this->register('web', new WebDidResolver()); 73 } 74}