Parse and validate AT Protocol Lexicons with DTO generation for Laravel
at main 149 lines 3.2 kB view raw
1<?php 2 3namespace SocialDept\AtpSchema; 4 5use SocialDept\AtpSchema\Data\LexiconDocument; 6use SocialDept\AtpSchema\Generator\DTOGenerator; 7use SocialDept\AtpSchema\Parser\SchemaLoader; 8use SocialDept\AtpSchema\Validation\LexiconValidator; 9 10class SchemaManager 11{ 12 /** 13 * Schema loader instance. 14 */ 15 protected SchemaLoader $loader; 16 17 /** 18 * Lexicon validator instance. 19 */ 20 protected LexiconValidator $validator; 21 22 /** 23 * DTO generator instance. 24 */ 25 protected ?DTOGenerator $generator = null; 26 27 /** 28 * Create a new SchemaManager. 29 */ 30 public function __construct( 31 SchemaLoader $loader, 32 LexiconValidator $validator, 33 ?DTOGenerator $generator = null 34 ) { 35 $this->loader = $loader; 36 $this->validator = $validator; 37 $this->generator = $generator; 38 } 39 40 /** 41 * Load a schema by NSID. 42 */ 43 public function load(string $nsid): LexiconDocument 44 { 45 return $this->loader->load($nsid); 46 } 47 48 /** 49 * Find a schema by NSID (nullable). 50 */ 51 public function find(string $nsid): ?LexiconDocument 52 { 53 return $this->loader->find($nsid); 54 } 55 56 /** 57 * Get all available schemas. 58 * 59 * @return array<string> 60 */ 61 public function all(): array 62 { 63 return $this->loader->all(); 64 } 65 66 /** 67 * Check if a schema exists. 68 */ 69 public function exists(string $nsid): bool 70 { 71 return $this->loader->exists($nsid); 72 } 73 74 /** 75 * Validate data against a schema. 76 */ 77 public function validate(string $nsid, array $data): bool 78 { 79 $document = $this->load($nsid); 80 81 return $this->validator->validate($data, $document); 82 } 83 84 /** 85 * Validate data and return errors. 86 * 87 * @return array<string, array<string>> 88 */ 89 public function validateWithErrors(string $nsid, array $data): array 90 { 91 $document = $this->load($nsid); 92 93 return $this->validator->validateWithErrors($data, $document); 94 } 95 96 /** 97 * Generate DTO code from a schema. 98 */ 99 public function generate(string $nsid, ?string $outputPath = null): string 100 { 101 if ($this->generator === null) { 102 throw new \RuntimeException('Generator not available'); 103 } 104 105 $document = $this->load($nsid); 106 107 return $this->generator->generate($document); 108 } 109 110 /** 111 * Clear schema cache. 112 */ 113 public function clearCache(?string $nsid = null): void 114 { 115 $this->loader->clearCache($nsid); 116 } 117 118 /** 119 * Get the schema loader instance. 120 */ 121 public function getLoader(): SchemaLoader 122 { 123 return $this->loader; 124 } 125 126 /** 127 * Get the validator instance. 128 */ 129 public function getValidator(): LexiconValidator 130 { 131 return $this->validator; 132 } 133 134 /** 135 * Get the generator instance. 136 */ 137 public function getGenerator(): ?DTOGenerator 138 { 139 return $this->generator; 140 } 141 142 /** 143 * Set the generator instance. 144 */ 145 public function setGenerator(DTOGenerator $generator): void 146 { 147 $this->generator = $generator; 148 } 149}