1
0
Fork 0
Manager/app/FormValidator.php

37 lines
996 B
PHP
Raw Normal View History

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