-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.php
87 lines (78 loc) · 2.02 KB
/
utils.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
require_once "src/views/ErrorPopup.php";
function getSanitized($data): string
{
$data = trim($data);
$data = stripslashes($data);
(int) $data && $data = preg_replace('/,/', '.', $data);
return htmlspecialchars($data);
}
function isValid($values, $errors): array
{
foreach ($values as $field=>$value) {
if (empty($value)) {
$nameErr = getTextError($field);
$errors[$field] = $nameErr;
} else {
$values[$field] = getSanitized($value);
}
}
if (isset($values['salary'])) {
if (!is_numeric($values['salary']) && !empty($values['salary'])) {
$errors['salary'] = 'Nur Zahlen sind erforderlich';
}
}
return array($values, $errors);
}
function getTextError($typeError): string
{
return match ($typeError) {
'name' => 'Abteilungsname ist erforderlich',
'firstname' => 'Vorname ist erforderlich',
'lastname' => 'Nachname ist erforderlich',
'gender_id' => 'Geschlecht ist erforderlich',
'salary' => 'Gehalt ist erforderlich',
};
}
function setErrorLog($error): void
{
error_log(
"Error: "
. date("Y-m-d H:i:s")
."\n"
. " "
. "message: "
. $error->getMessage()
."\n"
. " "
. "on line: "
. $error->getLine()
."\n"
. " "
. "file: "
. $error->getFile()
."\n"
. " "
. "code: "
. $error->getCode()
."\n"
. " "
. "string: "
. $error->getTraceAsString()
."\n"
, 3, "log/errors.log");
}
function onError($error, $errorMessage = 'Fehler bei der Datenbankverbindung!'): void
{
setErrorLog($error);
$errorPage = new ErrorPopup($errorMessage);
echo $errorPage->getContent();
}
function getFieldsToSend($paramFields,$params): array
{
$fieldsToSend = [];
foreach ($paramFields as $field) {
$fieldsToSend[$field] = $params[$field];
}
return $fieldsToSend;
}