1. <?php
  2. class FileValidator {
  3. private $rateLimit;
  4. private $rateLimit périodes;
  5. private $validatedFiles = [];
  6. private $errorFiles = [];
  7. public function __construct(int $rateLimit = 10, int $ périodes = 60) {
  8. $this->rateLimit = $rateLimit; // Maximum number of files allowed
  9. $this->rateLimit périodes = $ périodes; // Time window for the rate limit (in seconds)
  10. }
  11. /**
  12. * Validates a file.
  13. *
  14. * @param string $filePath The path to the file.
  15. * @return bool True if the file is valid, false otherwise.
  16. */
  17. public function validateFile(string $filePath): bool {
  18. // Check rate limit
  19. if ($this->isRateLimited()) {
  20. return false; // Rate limit exceeded
  21. }
  22. // Simulate file validation (replace with your actual validation logic)
  23. if ($this->isValidFile($filePath)) {
  24. $this->validatedFiles[] = $filePath;
  25. return true;
  26. } else {
  27. $this->errorFiles[] = $filePath;
  28. return false;
  29. }
  30. }
  31. /**
  32. * Checks if the rate limit has been exceeded.
  33. *
  34. * @return bool True if the rate limit has been exceeded, false otherwise.
  35. */
  36. private function isRateLimited(): bool {
  37. $now = time();
  38. // Remove expired rate limit entries
  39. $this->cleanupRateLimit($now);
  40. $count = count($this->validatedFiles);
  41. if ($count >= $this->rateLimit) {
  42. return true; // Rate limit exceeded
  43. }
  44. return false;
  45. }
  46. /**
  47. * Cleans up expired rate limit entries.
  48. *
  49. * @param int $now The current time.
  50. */
  51. private function cleanupRateLimit(int $now): void {
  52. $this->validatedFiles = array_filter($this->validatedFiles, function ($filePath) use ($now) {
  53. return $now - filemtime($filePath) < $this->rateLimit périodes;
  54. });
  55. }
  56. /**
  57. * Simulates file validation logic. Replace with your actual logic.
  58. *
  59. * @param string $filePath The path to the file.
  60. * @return bool True if the file is valid, false otherwise.
  61. */
  62. private function isValidFile(string $filePath): bool {
  63. // Replace with actual file validation logic
  64. // Example: Check file extension, size, content, etc.
  65. $fileExtension = pathinfo($filePath, PATHINFO_EXTENSION);
  66. return in_array(strtolower($fileExtension), ['txt', 'csv', 'json'], true); // Allow txt, csv, json
  67. }
  68. /**
  69. * Gets the list of validated files.
  70. *
  71. * @return array The list of validated files.
  72. */
  73. public function getValidatedFiles(): array {
  74. return $this->validatedFiles;
  75. }
  76. /**
  77. * Gets the list of error files.
  78. *
  79. * @return array The list of error files.
  80. */
  81. public function getErrorFiles(): array {
  82. return $this->errorFiles;
  83. }
  84. }
  85. ?>

Add your comment