Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

115 wiersze
3.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 email(string $field, mixed $value, string $message = ''): self
  29. {
  30. if ((string) $value !== '' && filter_var($value, FILTER_VALIDATE_EMAIL) === false) {
  31. $this->errors[$field][] = $message ?: "{$field} must be a valid email address.";
  32. }
  33. return $this;
  34. }
  35. public function date(string $field, mixed $value, string $format = 'Y-m-d', string $message = ''): self
  36. {
  37. $str = (string) $value;
  38. if ($str === '') {
  39. return $this;
  40. }
  41. $parsed = \DateTimeImmutable::createFromFormat($format, $str);
  42. if ($parsed === false || $parsed->format($format) !== $str) {
  43. $this->errors[$field][] = $message ?: "{$field} must be a valid date ({$format}).";
  44. }
  45. return $this;
  46. }
  47. public function minLength(string $field, mixed $value, int $min, string $message = ''): self
  48. {
  49. if (strlen((string) $value) < $min) {
  50. $this->errors[$field][] = $message ?: "{$field} must be at least {$min} characters.";
  51. }
  52. return $this;
  53. }
  54. public function min(string $field, mixed $value, int|float $min, string $message = ''): self
  55. {
  56. if (!is_numeric($value) || (float) $value < $min) {
  57. $this->errors[$field][] = $message ?: "{$field} must be at least {$min}.";
  58. }
  59. return $this;
  60. }
  61. public function max(string $field, mixed $value, int|float $max, string $message = ''): self
  62. {
  63. if (!is_numeric($value) || (float) $value > $max) {
  64. $this->errors[$field][] = $message ?: "{$field} must be no more than {$max}.";
  65. }
  66. return $this;
  67. }
  68. public function in(string $field, mixed $value, array $allowed, string $message = ''): self
  69. {
  70. if (!in_array($value, $allowed, true)) {
  71. $this->errors[$field][] = $message ?: "{$field} must be one of: " . implode(', ', $allowed) . '.';
  72. }
  73. return $this;
  74. }
  75. public function passes(): bool
  76. {
  77. return empty($this->errors);
  78. }
  79. public function fails(): bool
  80. {
  81. return !$this->passes();
  82. }
  83. public function errors(): array
  84. {
  85. return $this->errors;
  86. }
  87. }

Powered by TurnKey Linux.