Konstruktoren zum erstellen von neuen Objekten eingebaut

This commit is contained in:
Matthias Grief
2024-12-09 12:07:17 +01:00
committed by Eric Blommel
parent b81068b701
commit ff92b4c875
2 changed files with 75 additions and 57 deletions

View File

@@ -40,6 +40,25 @@ class SubjectData
*/ */
public array $topics; public array $topics;
/**
* Erstellt ein neues Fach. Es wird noch nichts gespeichert
* @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
*/
public function __construct(string $id, string $displayName, string $description, string $color, string $icon, array $topics)
{
$this->id = $id;
$this->displayName = $displayName;
$this->description = $description;
$this->color = $color;
$this->icon = $icon;
$this->topics = $topics;
}
/** /**
* Gibt alle Fächer als SubjectData Objekt zurück * Gibt alle Fächer als SubjectData Objekt zurück
* @return array Alle Fächer als SubjectData * @return array Alle Fächer als SubjectData
@@ -77,8 +96,6 @@ class SubjectData
*/ */
public static function fromName(string $subjectId): SubjectData|null public static function fromName(string $subjectId): SubjectData|null
{ {
$result = new SubjectData();
if (Util::containsIllegalCharacters($subjectId)) { if (Util::containsIllegalCharacters($subjectId)) {
return null; return null;
} }
@@ -89,35 +106,23 @@ class SubjectData
return null; return null;
} }
$result->id = $subjectId; if (!isset($data->displayName)) {
if (isset($data->displayName)) {
$result->displayName = $data->displayName;
} else {
return null; return null;
} }
if (isset($data->description)) { if (!isset($data->description)) {
$result->description = $data->description;
} else {
return null; return null;
} }
if (isset($data->color)) { if (!isset($data->color)) {
$result->color = $data->color;
} else {
return null; return null;
} }
if (isset($data->icon)) { if (!isset($data->icon)) {
$result->icon = $data->icon;
} else {
return null; return null;
} }
$result->topics = TopicData::getAll($subjectId); return new SubjectData($subjectId, $data->displayName, $data->description, $data->color, $data->icon, TopicData::getAll($subjectId));
return $result;
} }
/** /**

View File

@@ -44,6 +44,29 @@ class TopicData
*/ */
public string $article; public string $article;
/**
* Erstellt ein neues Thema. Es wird noch nichts gespeichert
* @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 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
*/
public function __construct(string $id, string $subjectId, string $displayName, string $icon, string $description, array $relatedTopics, array $files, string $article)
{
$this->id = $id;
$this->subjectId = $subjectId;
$this->displayName = $displayName;
$this->icon = $icon;
$this->description = $description;
$this->relatedTopics = $relatedTopics;
$this->files = $files;
$this->article = $article;
}
/** /**
* Gibt alle Themen zu einem gegebenen Fach zurück * Gibt alle Themen zu einem gegebenen Fach zurück
* @param $subjectId string Die ID des Faches * @param $subjectId string Die ID des Faches
@@ -82,44 +105,36 @@ class TopicData
*/ */
public static function fromName(string $subjectId, string $topicId): TopicData|null public static function fromName(string $subjectId, string $topicId): TopicData|null
{ {
$result = new TopicData(); if (Util::containsIllegalCharacters($subjectId)) {
return null;
$subjectId = Util::removeIllegalCharacters($subjectId); }
$topicId = Util::removeIllegalCharacters($topicId); if (Util::containsIllegalCharacters($topicId)) {
return null;
}
$data = Util::parseJsonFromFile(Config::getTopicDirectory($subjectId, $topicId) . "properties.json"); $data = Util::parseJsonFromFile(Config::getTopicDirectory($subjectId, $topicId) . "properties.json");
if (!isset($data)) { if (!isset($data)) {
return null; return null;
} }
$result->id = $topicId; if (!isset($data->displayName)) {
$result->subjectId = $subjectId;
if (isset($data->displayName)) {
$result->displayName = $data->displayName;
} else {
return null; return null;
} }
if (isset($data->icon)) { if (!isset($data->icon)) {
$result->icon = $data->icon;
} else {
return null; return null;
} }
if (isset($data->description)) { if (!isset($data->description)) {
$result->description = $data->description;
} else {
return null; return null;
} }
$relatedTopics = array();
if (isset($data->relatedTopics)) { if (isset($data->relatedTopics)) {
$result->relatedTopics = $data->relatedTopics; $relatedTopics = $data->relatedTopics;
} else {
$result->relatedTopics = array();
} }
$result->files = array(); $files = array();
$downloadDirectory = Config::getTopicDirectory($subjectId, $topicId) . "downloads/"; $downloadDirectory = Config::getTopicDirectory($subjectId, $topicId) . "downloads/";
if (is_dir($downloadDirectory)) { if (is_dir($downloadDirectory)) {
$fileNames = scandir($downloadDirectory); $fileNames = scandir($downloadDirectory);
@@ -128,7 +143,7 @@ class TopicData
continue; continue;
} }
$result->files[] = $fileName; $files[] = $fileName;
} }
} }
@@ -138,9 +153,7 @@ class TopicData
} }
$article = str_replace('$TOPICPATH', Config::getTopicDirectory($subjectId, $topicId) . "images", $article); $article = str_replace('$TOPICPATH', Config::getTopicDirectory($subjectId, $topicId) . "images", $article);
$result->article = $article; return new TopicData($topicId, $subjectId, $data->displayName, $data->icon, $data->description, $relatedTopics, $files, $article);
return $result;
} }
/** /**