2023-08-23 16:16:34 +00:00
|
|
|
<?php
|
|
|
|
class FormValidator {
|
|
|
|
|
|
|
|
private array $fieldValidation;
|
|
|
|
|
|
|
|
public function __construct(array $fieldValidation) {
|
|
|
|
$this->fieldValidation = $fieldValidation;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function isValid(array $req) {
|
2023-08-23 16:16:34 +00:00
|
|
|
if(!isset($req['csrf']) || $req['csrf'] !== $_SESSION['csrf']) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-08-23 16:16:35 +00:00
|
|
|
foreach($this->fieldValidation as $field => $params) {
|
2023-08-23 16:16:35 +00:00
|
|
|
if(isset($req[$field]) && strlen(trim($req[$field])) > 0) {
|
|
|
|
if(isset($params['regex'])) {
|
|
|
|
if(!preg_match($params['regex'], $req[$field])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(isset($params['equals']) && $params['equals'] !== $req[$field]) {
|
2023-08-23 16:16:34 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2023-08-23 16:16:35 +00:00
|
|
|
else if(isset($params['required']) && $params['required']) {
|
2023-08-23 16:16:34 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
?>
|