1
0
Fork 0
Manager/app/FormValidator.php

41 lines
1.0 KiB
PHP
Raw Permalink Normal View History

2023-08-23 16:16:34 +00:00
<?php
declare(strict_types=1);
namespace Mcp;
2023-08-23 16:16:34 +00:00
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;
}
}