Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

53 rindas
1.1KB

  1. <?php
  2. declare(strict_types=1);
  3. namespace Core;
  4. class Validator
  5. {
  6. protected array $errors = [];
  7. public function required(string $field, mixed $value, string $message = ''): self
  8. {
  9. if ($value === null || trim((string) $value) === '') {
  10. $this->errors[$field][] = $message ?: "{$field} is required.";
  11. }
  12. return $this;
  13. }
  14. public function maxLength(string $field, mixed $value, int $max, string $message = ''): self
  15. {
  16. if (strlen((string) $value) > $max) {
  17. $this->errors[$field][] = $message ?: "{$field} must be {$max} characters or fewer.";
  18. }
  19. return $this;
  20. }
  21. public function numeric(string $field, mixed $value, string $message = ''): self
  22. {
  23. if (!is_numeric($value)) {
  24. $this->errors[$field][] = $message ?: "{$field} must be numeric.";
  25. }
  26. return $this;
  27. }
  28. public function passes(): bool
  29. {
  30. return empty($this->errors);
  31. }
  32. public function fails(): bool
  33. {
  34. return !$this->passes();
  35. }
  36. public function errors(): array
  37. {
  38. return $this->errors;
  39. }
  40. }

Powered by TurnKey Linux.