Something went wrong. Try again.
Parse and validate AT Protocol Lexicons with DTO generation for Laravel
Something went wrong. Try again.
1.2 kB · 44 lines
PHP
at dev
123456789101112131415161718192021222324252627282930313233343536373839404142434445<?php
namespace SocialDept\AtpSchema\Validation\Rules;
use Closure;use Illuminate\Contracts\Validation\ValidationRule;
class Did implements ValidationRule{ /** * Run the validation rule. */ public function validate(string $attribute, mixed $value, Closure $fail): void { if (! is_string($value)) { $fail("The {$attribute} must be a string.");
return; }
// Check if Resolver package is available if (class_exists('SocialDept\AtpResolver\Support\Identity')) { if (! \SocialDept\AtpResolver\Support\Identity::isDid($value)) { $fail("The {$attribute} is not a valid DID."); }
return; }
// Fallback validation if Resolver is not available if (! $this->isValidDid($value)) { $fail("The {$attribute} is not a valid DID."); } }
/** * Fallback DID validation. */ protected function isValidDid(string $value): bool { // DID format: did:method:method-specific-id return (bool) preg_match('/^did:[a-z]+:[a-zA-Z0-9._:%-]+$/', $value); }}