Czy taki sposób walidacji jest okej, czy da się to jakoś uprościć?
<?php
declare(strict_types=1);
namespace vLib\app;
class UsersValidator
{
private array $patterns =
[
'uid' => '/^[A-Za-z0-9_]{8,255}$/',
'email' => '/^(?!(?:(?:\x22?\x5C[\x00-\x7E]\x22?)|(?:\x22?[^\x5C\x22]\x22?)){255,})(?!(?:(?:\x22?\x5C[\x00-\x7E]\x22?)|(?:\x22?[^\x5C\x22]\x22?)){65,}@)(?:(?:[\x21\x23-\x27\x2A\x2B\x2D\x2F-\x39\x3D\x3F\x5E-\x7E]+)|(?:\x22(?:[\x01-\x08\x0B\x0C\x0E-\x1F\x21\x23-\x5B\x5D-\x7F]|(?:\x5C[\x00-\x7F]))*\x22))(?:\.(?:(?:[\x21\x23-\x27\x2A\x2B\x2D\x2F-\x39\x3D\x3F\x5E-\x7E]+)|(?:\x22(?:[\x01-\x08\x0B\x0C\x0E-\x1F\x21\x23-\x5B\x5D-\x7F]|(?:\x5C[\x00-\x7F]))*\x22)))*@(?:(?:(?!.*[^.]{64,})(?:(?:(?:xn--)?[a-z0-9]+(?:-[a-z0-9]+)*\.){1,126}){1,}(?:(?:[a-z][a-z0-9]*)|(?:(?:xn--)[a-z0-9]+))(?:-[a-z0-9]+)*)|(?:\[(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){7})|(?:(?!(?:.*[a-f0-9][:\]]){7,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?)))|(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){5}:)|(?:(?!(?:.*[a-f0-9]:){5,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3}:)?)))?(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))(?:\.(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))){3}))\]))$/iD',
'pwd' => '/^[a-zA-Z0-9\-_]{8,64}$/',
];
public function emptyInputs(
array $data,
string $prevKey = null,
$prevIsEmpty = null
): bool {
$isEmpty = is_null($prevIsEmpty) ? false : $prevIsEmpty;
foreach ($data as $key => $dataItem) {
if (is_array($dataItem)) {
$isEmpty = $this->emptyInputs($dataItem, $key, $isEmpty);
} else if (empty($dataItem)) {
$errTitle = $prevKey . ucfirst($key) . 'Err';
Notifications::setNotification($errTitle, 'Pole jest puste.');
$isEmpty = true;
}
}
return $isEmpty;
}
public function validation(array $data): bool
{
$isCorrect = true;
if (!$this->checkData($data['uid']['content'], 'uid')) {
Notifications::setNotification('uidContentErr', 'test');
$isCorrect = false;
}
if (!$this->checkData($data['email']['content'], 'email')) {
Notifications::setNotification('emailContentErr', 'test');
$isCorrect = false;
}
if (!$this->checkData($data['pwd']['content'], 'pwd')) {
Notifications::setNotification('pwdContentErr', 'test');
$isCorrect = false;
}
if ($this->emptyInputs($data))
$isCorrect = false;
return $isCorrect;
}
private function checkData(string $value, string $pattern): bool
{
return (bool) preg_match($this->patterns[$pattern], $value);
}
}