Parse and validate AT Protocol Lexicons with DTO generation for Laravel
at dev 819 B view raw
1<?php 2 3namespace SocialDept\AtpSchema\Validation\Rules; 4 5use Closure; 6use Illuminate\Contracts\Validation\ValidationRule; 7 8class MaxGraphemes implements ValidationRule 9{ 10 /** 11 * Maximum grapheme count. 12 */ 13 protected int $max; 14 15 /** 16 * Create a new rule instance. 17 */ 18 public function __construct(int $max) 19 { 20 $this->max = $max; 21 } 22 23 /** 24 * Run the validation rule. 25 */ 26 public function validate(string $attribute, mixed $value, Closure $fail): void 27 { 28 if (! is_string($value)) { 29 $fail("The {$attribute} must be a string."); 30 31 return; 32 } 33 34 $count = grapheme_strlen($value); 35 36 if ($count > $this->max) { 37 $fail("The {$attribute} may not be greater than {$this->max} graphemes."); 38 } 39 } 40}