Erstellen von Objekten verbessert

This commit is contained in:
Matthias Grief
2024-12-10 20:34:18 +01:00
committed by Eric Blommel
parent 45010810f7
commit 42132bbbe6
2 changed files with 123 additions and 23 deletions

View File

@@ -41,22 +41,57 @@ class SubjectData
public array $topics;
/**
* Erstellt ein neues Fach. Es wird noch nichts gespeichert
* Erstellt ein neues Fach
* @param string $id Ein eindeutiger Bezeichner für das Fach, darf nur A-Z, a-Z, 0-9 sowie _ und - enthalten
* @param string $displayName Der für User angezeigte Name des Faches, nur reiner Text
* @param string $description Eine kurze Beschreibung des Faches, z.B. für den Text auf der Startseite, kann HTML enthalten
* @param string $color Themenfarbe des Faches als hexcode
* @param string $icon Icon des Faches als Font-Awesome CSS-Klasse
* @param array $topics Alle Themen des Faches als TopicData Objekt
* @param array $topics Alle Themen des Faches als TopicData Object
* @return SubjectData|false Neues Fach oder false, wenn ein Fehler auftritt
*/
public function __construct(string $id, string $displayName, string $description, string $color, string $icon, array $topics)
public static function createNew(string $id, string $displayName, string $description, string $color, string $icon, array $topics): SubjectData|false
{
$this->id = $id;
$this->displayName = $displayName;
$this->description = $description;
$this->color = $color;
$this->icon = $icon;
$this->topics = $topics;
$result = new SubjectData();
if(Util::containsIllegalCharacters($id)) {
return false;
}
if(self::exists($id)) {
return false;
}
$result->id = $id;
$result->displayName = $displayName;
$result->description = $description;
$result->color = $color;
$result->icon = $icon;
$result->topics = $topics;
if(!$result->save()) {
return false;
}
return $result;
}
/**
* Prüft, ob das Thema zu den angegebenen IDs existiert
* @param string $subjectId ID des Faches
* @param string $topicId ID des Themas
* @return bool true, wenn es existiert, sonst false
*/
public static function exists(string $subjectId): bool
{
if(!is_dir(Config::getSubjectDirectory($subjectId))) {
return false;
}
return true;
}
/**
@@ -96,9 +131,12 @@ class SubjectData
*/
public static function fromName(string $subjectId): SubjectData|null
{
$result = new SubjectData();
if (Util::containsIllegalCharacters($subjectId)) {
return null;
}
$result->id = $subjectId;
$filename = Config::getSubjectDirectory($subjectId) . "properties.json";
$data = Util::parseJsonFromFile($filename);
@@ -109,20 +147,26 @@ class SubjectData
if (!isset($data->displayName)) {
return null;
}
$result->displayName = $data->displayName;
if (!isset($data->description)) {
return null;
}
$result->description = $data->description;
if (!isset($data->color)) {
return null;
}
$result->color = $data->color;
if (!isset($data->icon)) {
return null;
}
$result->icon = $data->icon;
return new SubjectData($subjectId, $data->displayName, $data->description, $data->color, $data->icon, TopicData::getAll($subjectId));
$result->topics = TopicData::getAll($subjectId);
return $result;
}
/**

View File

@@ -1,4 +1,8 @@
<?php
use exception\SubjectDoesNotExistException;
use exception\TopicAlreadyExistsException;
require_once("Config.php");
require_once("Util.php");
@@ -45,26 +49,69 @@ class TopicData
public string $article;
/**
* Erstellt ein neues Thema. Es wird noch nichts gespeichert
* Erstellt ein neues Thema
* @param string $id Innerhalb des zugehörigen Faches eindeutige ID, darf nur A-Z, a-z, 0-9 sowie - und _ enthalten
* @param string $subjectId Die eindeutige ID des zugehörigen Faches, das Fach muss schon existieren
* @param string $displayName Der für User angezeigt Name des Themas, darf nur reinen Text enthalten
* @param string $icon Das Icon des Themas als Font-Awesome CSS-Klasse
* @param string $description Eine kurze Beschreibung des Themas, z.B. für die Fachübersichtsseite, darf HTML enthalten
* @param array $relatedTopics Die IDs aller verwandten Themen als String
* @param array $files Die Dateinamen (Datei.pdf) aller downloadbarer Dateien zu diesem Thema als String
* @param array $files Die Dateinamen (Datei.pdf) aller downloadbarer Dateien zu diesem Thema als String. Die eigentlichen Dateien müssen seperat hinzugefügt werden
* @param string $article Der gesamte Erklärungstext zum Thema, enthält fertiges HTML und LATEX Formelsyntax für MathJax https://docs.mathjax.org/en/latest/basic/mathematics.html
* @return TopicData|false Neues Thema oder false, wenn ein Fehler auftritt
*/
public function __construct(string $id, string $subjectId, string $displayName, string $icon, string $description, array $relatedTopics, array $files, string $article)
public static function createNew(string $id, string $subjectId, string $displayName, string $icon, string $description, array $relatedTopics, array $files, string $article): TopicData|false
{
$this->id = $id;
$this->subjectId = $subjectId;
$this->displayName = $displayName;
$this->icon = $icon;
$this->description = $description;
$this->relatedTopics = $relatedTopics;
$this->files = $files;
$this->article = $article;
$result = new TopicData();
if(Util::containsIllegalCharacters($subjectId)) {
return false;
}
if(!SubjectData::exists($subjectId)) {
return false;
}
$result->subjectId = $subjectId;
if(Util::containsIllegalCharacters($id)) {
return false;
}
if(self::exists($subjectId, $id)) {
return false;
}
$result->id = $id;
$result->displayName = $displayName;
$result->icon = $icon;
$result->description = $description;
$result->relatedTopics = $relatedTopics;
$result->files = $files;
$result->article = $article;
if(!$result->save()) {
return false;
}
return $result;
}
/**
* Prüft, ob das Thema zu den angegebenen IDs existiert
* @param string $subjectId ID des Faches
* @param string $topicId ID des Themas
* @return bool true, wenn es existiert, sonst false
*/
public static function exists(string $subjectId, string $topicId): bool
{
if(!is_dir(Config::getTopicDirectory($subjectId, $topicId))) {
return false;
}
return true;
}
/**
@@ -105,12 +152,16 @@ class TopicData
*/
public static function fromName(string $subjectId, string $topicId): TopicData|null
{
$result = new TopicData();
if (Util::containsIllegalCharacters($subjectId)) {
return null;
}
if (Util::containsIllegalCharacters($topicId)) {
return null;
}
$result->id = $topicId;
$result->subjectId = $subjectId;
$data = Util::parseJsonFromFile(Config::getTopicDirectory($subjectId, $topicId) . "properties.json");
if (!isset($data)) {
@@ -120,19 +171,23 @@ class TopicData
if (!isset($data->displayName)) {
return null;
}
$result->displayName = $data->displayName;
if (!isset($data->icon)) {
return null;
}
$result->icon = $data->icon;
if (!isset($data->description)) {
return null;
}
$result->description = $data->description;
$relatedTopics = array();
if (isset($data->relatedTopics)) {
$relatedTopics = $data->relatedTopics;
}
$result->relatedTopics = $relatedTopics;
$files = array();
$downloadDirectory = Config::getTopicDirectory($subjectId, $topicId) . "downloads/";
@@ -146,14 +201,15 @@ class TopicData
$files[] = $fileName;
}
}
$result->files = $files;
$article = Util::readFileContent(Config::getTopicDirectory($subjectId, $topicId) . "article.html");
if (!isset($article)) {
$article = "Kein Erklärtext vorhanden";
}
$article = str_replace('$TOPICPATH', Config::getTopicDirectory($subjectId, $topicId) . "images", $article);
$result->article = str_replace('$TOPICPATH', Config::getTopicDirectory($subjectId, $topicId) . "images", $article);
return new TopicData($topicId, $subjectId, $data->displayName, $data->icon, $data->description, $relatedTopics, $files, $article);
return $result;
}
/**