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;
}
/**