init Abgabe
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
class Model{
|
||||
|
||||
protected $errorArray = [];
|
||||
|
||||
public function setValues($values) {
|
||||
foreach ($values as $key => $value) {
|
||||
$method = 'set' . str_replace(' ', '', ucwords(str_replace('_', ' ', $key)));
|
||||
if (method_exists($this, $method)) {
|
||||
$this->$method($value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Automatische Validierung aller Attribute der Subklasse
|
||||
* Prüft:
|
||||
* - leere Felder
|
||||
* - minimale Länge (2)
|
||||
* - potenziell unsichere Zeichen
|
||||
*/
|
||||
protected function validate() {
|
||||
|
||||
$this->errorArray = [];
|
||||
|
||||
$reflection = new ReflectionClass($this); // Reflektiert Subklasse
|
||||
$properties = $reflection->getProperties(ReflectionProperty::IS_PROTECTED);
|
||||
|
||||
foreach ($properties as $prop) {
|
||||
$name = $prop->getName();
|
||||
$value = $this->$name;
|
||||
|
||||
// 1) Pflichtfeldprüfung
|
||||
if (empty($value)) {
|
||||
$this->errorArray[$name] = "Bitte gib einen Wert für {$name} ein.";
|
||||
continue;
|
||||
}
|
||||
|
||||
// 2) Minimale Länge prüfen (nur Strings)
|
||||
if (is_string($value) && strlen($value) < 2) {
|
||||
$this->errorArray[$name] = "Der Wert von {$name} muss mindestens 2 Zeichen lang sein.";
|
||||
}
|
||||
|
||||
// 3) Prüfen auf potenziell unsichere Zeichen (XSS / SQL-Injection)
|
||||
if ($this->containsInvalidChars($value)) {
|
||||
$this->errorArray[$name] = "Der Wert von {$name} enthält ungültige Zeichen.";
|
||||
}
|
||||
}
|
||||
|
||||
return $this->errorArray; // Fehler-Array zurückgeben
|
||||
}
|
||||
|
||||
/**
|
||||
* Prüft auf verdächtige Zeichen
|
||||
*/
|
||||
protected function containsInvalidChars($value) {
|
||||
if (!is_string($value)) return false;
|
||||
|
||||
// einfache Regeln: < > ' " ; %
|
||||
if (preg_match('/[<>\'"%;]/', $value)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user