Parse and validate AT Protocol Lexicons with DTO generation for Laravel
at main 2.1 kB view raw
1<?php 2 3namespace SocialDept\AtpSchema\Exceptions; 4 5class RecordValidationException extends SchemaException 6{ 7 /** 8 * Validation errors. 9 */ 10 protected array $errors = []; 11 12 /** 13 * Create exception with validation errors. 14 */ 15 public static function withErrors(string $nsid, array $errors): self 16 { 17 $instance = new static("Record validation failed for {$nsid}"); 18 $instance->errors = $errors; 19 $instance->setContext(['nsid' => $nsid, 'errors' => $errors]); 20 21 return $instance; 22 } 23 24 /** 25 * Get validation errors. 26 */ 27 public function getErrors(): array 28 { 29 return $this->errors; 30 } 31 32 /** 33 * Create exception for type mismatch. 34 */ 35 public static function typeMismatch(string $field, string $expected, string $actual): self 36 { 37 return static::withContext( 38 "Type mismatch for field {$field}: expected {$expected}, got {$actual}", 39 ['field' => $field, 'expected' => $expected, 'actual' => $actual] 40 ); 41 } 42 43 /** 44 * Create exception for constraint violation. 45 */ 46 public static function constraintViolation(string $field, string $constraint, mixed $value): self 47 { 48 return static::withContext( 49 "Constraint violation for field {$field}: {$constraint}", 50 ['field' => $field, 'constraint' => $constraint, 'value' => $value] 51 ); 52 } 53 54 /** 55 * Create exception for invalid type. 56 */ 57 public static function invalidType(string $path, string $expected, string $actual): self 58 { 59 return static::withContext( 60 "Expected type '{$expected}' at '{$path}' but got '{$actual}'", 61 ['path' => $path, 'expected' => $expected, 'actual' => $actual] 62 ); 63 } 64 65 /** 66 * Create exception for invalid value. 67 */ 68 public static function invalidValue(string $path, string $reason): self 69 { 70 return static::withContext( 71 "Invalid value at '{$path}': {$reason}", 72 ['path' => $path, 'reason' => $reason] 73 ); 74 } 75}