<?php
class FileValidator {
private $rateLimit;
private $rateLimit périodes;
private $validatedFiles = [];
private $errorFiles = [];
public function __construct(int $rateLimit = 10, int $ périodes = 60) {
$this->rateLimit = $rateLimit; // Maximum number of files allowed
$this->rateLimit périodes = $ périodes; // Time window for the rate limit (in seconds)
}
/**
* Validates a file.
*
* @param string $filePath The path to the file.
* @return bool True if the file is valid, false otherwise.
*/
public function validateFile(string $filePath): bool {
// Check rate limit
if ($this->isRateLimited()) {
return false; // Rate limit exceeded
}
// Simulate file validation (replace with your actual validation logic)
if ($this->isValidFile($filePath)) {
$this->validatedFiles[] = $filePath;
return true;
} else {
$this->errorFiles[] = $filePath;
return false;
}
}
/**
* Checks if the rate limit has been exceeded.
*
* @return bool True if the rate limit has been exceeded, false otherwise.
*/
private function isRateLimited(): bool {
$now = time();
// Remove expired rate limit entries
$this->cleanupRateLimit($now);
$count = count($this->validatedFiles);
if ($count >= $this->rateLimit) {
return true; // Rate limit exceeded
}
return false;
}
/**
* Cleans up expired rate limit entries.
*
* @param int $now The current time.
*/
private function cleanupRateLimit(int $now): void {
$this->validatedFiles = array_filter($this->validatedFiles, function ($filePath) use ($now) {
return $now - filemtime($filePath) < $this->rateLimit périodes;
});
}
/**
* Simulates file validation logic. Replace with your actual logic.
*
* @param string $filePath The path to the file.
* @return bool True if the file is valid, false otherwise.
*/
private function isValidFile(string $filePath): bool {
// Replace with actual file validation logic
// Example: Check file extension, size, content, etc.
$fileExtension = pathinfo($filePath, PATHINFO_EXTENSION);
return in_array(strtolower($fileExtension), ['txt', 'csv', 'json'], true); // Allow txt, csv, json
}
/**
* Gets the list of validated files.
*
* @return array The list of validated files.
*/
public function getValidatedFiles(): array {
return $this->validatedFiles;
}
/**
* Gets the list of error files.
*
* @return array The list of error files.
*/
public function getErrorFiles(): array {
return $this->errorFiles;
}
}
?>
Add your comment