ArticleManager um LIKES erweitert...

Einführung einer dynamischen Like-Funktion für Beiträge
This commit is contained in:
2026-06-17 12:26:18 +02:00
parent 4f8d11881d
commit 3d50e6e3b3
3 changed files with 142 additions and 14 deletions
+39 -10
View File
@@ -7,13 +7,14 @@
*/
class Article
{
public $id;
public $title;
public $content;
public $author;
public $creationDate;
public $category;
public $tags;
private $id;
private $title;
private $content;
private $author;
private $creationDate;
private $category;
private $tags;
private $likes;
/**
* Konstruktor
@@ -26,7 +27,7 @@ class Article
* @param $tags string optionale Schlagworte für eine bessere Suche
* @param $creationDate string Datum der Beitragserstellung
*/
public function __construct(int $id, string $title, string $content, string $author, string $category, string $tags, string $creationDate)
public function __construct(int $id, string $title, string $content, string $author, string $category, string $tags, string $creationDate, array $likes = [])
{
$this->id = $id;
$this->title = $title;
@@ -35,6 +36,7 @@ class Article
$this->creationDate = $creationDate;
$this->category = $category;
$this->tags = $tags;
$this->likes = $likes;
}
/**
@@ -67,7 +69,7 @@ class Article
/**
* Gibt den Content eines Beitrags zurück.
* TODO: Content muss noch definiert werden.
*
* @return string
*/
public function getContent(): string
@@ -77,7 +79,7 @@ class Article
/**
* Setzt den Content eines Beitrags.
* TODO: Content muss noch definiert werden.
*
* @param $content
* @return void
*/
@@ -141,7 +143,34 @@ class Article
$this->tags = $tags;
}
/**
* Gibt alle User-IDs zurück, die diesen Beitrag geliked haben.
* @return array
*/
public function getLikes(): array
{
return $this->likes;
}
/**
* Gibt die Gesamtzahl der Likes zurück.
* @return int
*/
public function getLikeCount(): int
{
return count($this->likes);
}
/**
* Prüft, ob ein bestimmter Nutzer den Beitrag bereits geliked hat.
*
* @param string $userId
* @return bool
*/
public function hasLiked(string $userId): bool
{
return in_array($userId, $this->likes);
}
}
?>