Parse and validate AT Protocol Lexicons with DTO generation for Laravel
1<?php
2
3namespace SocialDept\AtpSchema\Validation\Rules;
4
5use Closure;
6use Illuminate\Contracts\Validation\ValidationRule;
7
8class Did implements ValidationRule
9{
10 /**
11 * Run the validation rule.
12 */
13 public function validate(string $attribute, mixed $value, Closure $fail): void
14 {
15 if (! is_string($value)) {
16 $fail("The {$attribute} must be a string.");
17
18 return;
19 }
20
21 // Check if Resolver package is available
22 if (class_exists('SocialDept\AtpResolver\Support\Identity')) {
23 if (! \SocialDept\AtpResolver\Support\Identity::isDid($value)) {
24 $fail("The {$attribute} is not a valid DID.");
25 }
26
27 return;
28 }
29
30 // Fallback validation if Resolver is not available
31 if (! $this->isValidDid($value)) {
32 $fail("The {$attribute} is not a valid DID.");
33 }
34 }
35
36 /**
37 * Fallback DID validation.
38 */
39 protected function isValidDid(string $value): bool
40 {
41 // DID format: did:method:method-specific-id
42 return (bool) preg_match('/^did:[a-z]+:[a-zA-Z0-9._:%-]+$/', $value);
43 }
44}