34 lines
876 B
PHP
34 lines
876 B
PHP
|
<?php
|
||
|
class FormValidator {
|
||
|
|
||
|
private array $fieldValidation;
|
||
|
|
||
|
public function __construct(array $fieldValidation) {
|
||
|
$this->fieldValidation = $fieldValidation;
|
||
|
}
|
||
|
|
||
|
public function isValid(array $req) {
|
||
|
if(!isset($req['csrf']) || $req['csrf'] !== $_SESSION['csrf']) {
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
foreach($fieldValidation as $field => $params) {
|
||
|
if($params['required'] && (!isset($req[$field]) || strlen($req[$field]) == 0)) {
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
if(isset($params['regex'])) {
|
||
|
if(!preg_match($params['regex'], $req[$field])) {
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
else if(isset($params['equals']) && $params['equals'] !== $req[$field]) {
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
}
|
||
|
?>
|