1
0

ExpoPushTokenRule.php 964 B

123456789101112131415161718192021222324252627282930313233
  1. <?php
  2. namespace App\Rules;
  3. use Closure;
  4. use Illuminate\Contracts\Validation\ValidationRule;
  5. class ExpoPushTokenRule implements ValidationRule
  6. {
  7. /**
  8. * Run the validation rule.
  9. *
  10. * @param \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail
  11. */
  12. public function validate(string $attribute, mixed $value, Closure $fail): void
  13. {
  14. if (! $value || empty($value)) {
  15. $fail('The :attribute must not be empty.');
  16. }
  17. if (str_starts_with($value, 'ExponentPushToken[') && mb_strlen($value) < 26) {
  18. $fail('The :attribute is not a valid push token.');
  19. }
  20. if (! str_starts_with($value, 'ExponentPushToken[') && ! str_starts_with($value, 'ExpoPushToken[')) {
  21. $fail('The :attribute is not a valid push token.');
  22. }
  23. if (! str_ends_with($value, ']')) {
  24. $fail('The :attribute is not a valid push token.');
  25. }
  26. }
  27. }