diff --git a/tasks/oop/classes/.htaccess b/tasks/oop/classes/.htaccess new file mode 100644 index 0000000..872a3ce --- /dev/null +++ b/tasks/oop/classes/.htaccess @@ -0,0 +1,3 @@ +Order deny,allow +Deny from all +Allow from 127.0.0.1 \ No newline at end of file diff --git a/tasks/oop/classes/fieldValidator/fieldValidator.php b/tasks/oop/classes/fieldValidator/fieldValidator.php new file mode 100644 index 0000000..ecdb1f6 --- /dev/null +++ b/tasks/oop/classes/fieldValidator/fieldValidator.php @@ -0,0 +1,112 @@ +name = $name; + $this->value = $value; + $this->type = $type; + $this->required = $required; + $this->minLen = $minLen; + $this->maxLen = $maxLen; + $this->error = $error; + + return $this; + } + + /* + * Fluent Style + */ + + public static function create(string $name, string $value, string $type) + { + return new FieldValidator($name, $value, $type); + } + + public function isRequired() + { + $this->required = true; + return $this; + } + + public function withMinLen(int $minLen) + { + $this->minLen = $minLen; + return $this; + } + + public function withMaxLen(int $maxLen) + { + $this->maxLen = $maxLen; + return $this; + } + + /* + * Getters and Setters + */ + + public function getError() + { + return $this->error; + } + + public function getName() + { + return $this->name; + } + + /* + * Methods + */ + + public function isValid() + { + if ($this->required && empty($this->value)) { + $this->error = "$this->name is required"; + return false; + } + + if ($this->minLen >= 0 && !empty($this->value) && strlen($this->value) < $this->minLen) { + $this->error = "$this->name requires $this->minLen characters, got " . strlen($this->value); + return false; + } + + if ($this->maxLen >= 0 && !empty($this->value) && strlen($this->value) > $this->maxLen) { + $this->error = "$this->name allows $this->maxLen characters, got " . strlen($this->value); + return false; + } + + switch ($this->type) { + case "email": + if (!filter_var($this->value, FILTER_VALIDATE_EMAIL)) { + $this->error = "Not a valid E-Mail"; + return false; + } + break; + case "number": + if (!filter_var($this->value, FILTER_VALIDATE_FLOAT) || !filter_var($this->value, FILTER_VALIDATE_INT)) { + $this->error = "Not a valid number"; + return false; + } + break; + } + + return true; + } +} diff --git a/tasks/oop/classes/formValidator/formValidator.php b/tasks/oop/classes/formValidator/formValidator.php new file mode 100644 index 0000000..463df1c --- /dev/null +++ b/tasks/oop/classes/formValidator/formValidator.php @@ -0,0 +1,61 @@ +fields = $fields; + } + + /* + * Fluent Style + */ + + public static function create(FieldValidator ...$fields) + { + return new FormValidator(...$fields); + } + + /* + * Methods + */ + + public function isValid() + { + foreach ($this->fields as $field) { + if (!$field->isValid) return false; + } + + return true; + } + + public function getErrors() + { + $errors = []; + + foreach ($this->fields as $field) { + if (!$field->isValid()) array_push($errors, $field->getError()); + } + + return $errors; + } + + public function getField(string $name) + { + $field = current(array_filter($this->fields, fn ($field) => strcmp($field->getName(), $name) === 0)); + if ($field !== false) { + return $field; + } + + return "No field named $name"; + } +} diff --git a/tasks/oop/hyperlink/hyperlink.php b/tasks/oop/classes/hyperlink/hyperlink.php similarity index 100% rename from tasks/oop/hyperlink/hyperlink.php rename to tasks/oop/classes/hyperlink/hyperlink.php diff --git a/tasks/oop/person/person.php b/tasks/oop/classes/person/person.php similarity index 100% rename from tasks/oop/person/person.php rename to tasks/oop/classes/person/person.php diff --git a/tasks/oop/fieldValidator/index.php b/tasks/oop/fieldValidator/index.php new file mode 100644 index 0000000..6c838f8 --- /dev/null +++ b/tasks/oop/fieldValidator/index.php @@ -0,0 +1,38 @@ + + + + + + + FieldValidator Test + + + + isRequired(), + FieldValidator::create("MinLen Valid", "ssss", "string")->withMinLen(4), + FieldValidator::create("MinLen Invalid", "sss", "string")->withMinLen(4), + FieldValidator::create("MaxLen Valid", "ssss", "string")->withMaxLen(4), + FieldValidator::create("MaxLen Invalid", "sssss", "string")->withMaxLen(4), + FieldValidator::create("E-Mail Valid", "khofmann@fu-berlin.de", "email"), + FieldValidator::create("E-Mail Invalid", "khofmann@fu-berlin de", "email"), + ]; + + foreach ($validators as $validator) { + echo "" . $validator->getName() . ": "; + echo $validator->isValid() ? "True" : "False" . " with " . $validator->getError(); + echo "
"; + } + ?> + + + \ No newline at end of file diff --git a/tasks/oop/formValidator/index.php b/tasks/oop/formValidator/index.php new file mode 100644 index 0000000..5695864 --- /dev/null +++ b/tasks/oop/formValidator/index.php @@ -0,0 +1,46 @@ + + + + + + + FormValidator Test + + + + isRequired(), + FieldValidator::create("MinLen Valid", "ssss", "string")->withMinLen(4), + FieldValidator::create("MinLen Invalid", "sss", "string")->withMinLen(4), + FieldValidator::create("MaxLen Valid", "ssss", "string")->withMaxLen(4), + FieldValidator::create("MaxLen Invalid", "sssss", "string")->withMaxLen(4), + FieldValidator::create("E-Mail Valid", "khofmann@fu-berlin.de", "email"), + FieldValidator::create("E-Mail Invalid", "khofmann@fu-berlin de", "email"), + ); + + foreach ($form->getErrors() as $error) { + echo "$error
"; + } + + ?> +
+
+    getField("E-Mail Valid"));
+    echo "\n\n";
+    print_r($form->getField("Name"));
+    ?>
+  
+ + + \ No newline at end of file diff --git a/tasks/oop/hyperlink/index.php b/tasks/oop/hyperlink/index.php index 409f47b..1d59132 100644 --- a/tasks/oop/hyperlink/index.php +++ b/tasks/oop/hyperlink/index.php @@ -14,7 +14,7 @@ Headers::html(); //Testdatei für die Klasse Hyperlink - include_once "./hyperlink.php"; + include_once "../classes/hyperlink/hyperlink.php"; //Objektinstanzen $link1 = new Hyperlink("https://www.google.de", "Google"); diff --git a/tasks/oop/person/index.php b/tasks/oop/person/index.php index 8e8442d..52ba450 100644 --- a/tasks/oop/person/index.php +++ b/tasks/oop/person/index.php @@ -13,7 +13,7 @@ include_once "../../../base/headers.php"; Headers::html(); - include_once "./person.php"; + include_once "../classes/person/person.php"; $person = new Person("John Doe", 170, new DateTime("01-01-1970")); echo $person;